forked from cerc-io/ipld-eth-server
Update vendor directory and make necessary code changes
Fixes for new geth version
This commit is contained in:
+33
@@ -0,0 +1,33 @@
|
||||
package encoding
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
refmt "github.com/polydawn/refmt"
|
||||
"github.com/polydawn/refmt/obj/atlas"
|
||||
)
|
||||
|
||||
// PooledCloner is a thread-safe pooled object cloner.
|
||||
type PooledCloner struct {
|
||||
pool sync.Pool
|
||||
}
|
||||
|
||||
// NewPooledCloner returns a PooledCloner with the given atlas. Do not copy
|
||||
// after use.
|
||||
func NewPooledCloner(atl atlas.Atlas) PooledCloner {
|
||||
return PooledCloner{
|
||||
pool: sync.Pool{
|
||||
New: func() interface{} {
|
||||
return refmt.NewCloner(atl)
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Clone clones a into b using a cloner from the pool.
|
||||
func (p *PooledCloner) Clone(a, b interface{}) error {
|
||||
c := p.pool.Get().(refmt.Cloner)
|
||||
err := c.Clone(a, b)
|
||||
p.pool.Put(c)
|
||||
return err
|
||||
}
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
package encoding
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"sync"
|
||||
|
||||
cbor "github.com/polydawn/refmt/cbor"
|
||||
"github.com/polydawn/refmt/obj/atlas"
|
||||
)
|
||||
|
||||
type proxyWriter struct {
|
||||
w io.Writer
|
||||
}
|
||||
|
||||
func (w *proxyWriter) Write(b []byte) (int, error) {
|
||||
return w.w.Write(b)
|
||||
}
|
||||
|
||||
// Marshaller is a reusbale CBOR marshaller.
|
||||
type Marshaller struct {
|
||||
marshal *cbor.Marshaller
|
||||
writer proxyWriter
|
||||
}
|
||||
|
||||
// NewMarshallerAtlased constructs a new cbor Marshaller using the given atlas.
|
||||
func NewMarshallerAtlased(atl atlas.Atlas) *Marshaller {
|
||||
m := new(Marshaller)
|
||||
m.marshal = cbor.NewMarshallerAtlased(&m.writer, atl)
|
||||
return m
|
||||
}
|
||||
|
||||
// Encode encodes the given object to the given writer.
|
||||
func (m *Marshaller) Encode(obj interface{}, w io.Writer) error {
|
||||
m.writer.w = w
|
||||
err := m.marshal.Marshal(obj)
|
||||
m.writer.w = nil
|
||||
return err
|
||||
}
|
||||
|
||||
// Marshal marshels the given object to a byte slice.
|
||||
func (m *Marshaller) Marshal(obj interface{}) ([]byte, error) {
|
||||
var buf bytes.Buffer
|
||||
if err := m.Encode(obj, &buf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
// PooledMarshaller is a thread-safe pooled CBOR marshaller.
|
||||
type PooledMarshaller struct {
|
||||
pool sync.Pool
|
||||
}
|
||||
|
||||
// NewPooledMarshaller returns a PooledMarshaller with the given atlas. Do not
|
||||
// copy after use.
|
||||
func NewPooledMarshaller(atl atlas.Atlas) PooledMarshaller {
|
||||
return PooledMarshaller{
|
||||
pool: sync.Pool{
|
||||
New: func() interface{} {
|
||||
return NewMarshallerAtlased(atl)
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Marshal marshals the passed object using the pool of marshallers.
|
||||
func (p *PooledMarshaller) Marshal(obj interface{}) ([]byte, error) {
|
||||
m := p.pool.Get().(*Marshaller)
|
||||
bts, err := m.Marshal(obj)
|
||||
p.pool.Put(m)
|
||||
return bts, err
|
||||
}
|
||||
|
||||
// Encode encodes the passed object to the given writer using the pool of
|
||||
// marshallers.
|
||||
func (p *PooledMarshaller) Encode(obj interface{}, w io.Writer) error {
|
||||
m := p.pool.Get().(*Marshaller)
|
||||
err := m.Encode(obj, w)
|
||||
p.pool.Put(m)
|
||||
return err
|
||||
}
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
package encoding
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"sync"
|
||||
|
||||
cbor "github.com/polydawn/refmt/cbor"
|
||||
"github.com/polydawn/refmt/obj/atlas"
|
||||
)
|
||||
|
||||
type proxyReader struct {
|
||||
r io.Reader
|
||||
}
|
||||
|
||||
func (r *proxyReader) Read(b []byte) (int, error) {
|
||||
return r.r.Read(b)
|
||||
}
|
||||
|
||||
// Unmarshaller is a reusable CBOR unmarshaller.
|
||||
type Unmarshaller struct {
|
||||
unmarshal *cbor.Unmarshaller
|
||||
reader proxyReader
|
||||
}
|
||||
|
||||
// NewUnmarshallerAtlased creates a new reusable unmarshaller.
|
||||
func NewUnmarshallerAtlased(atl atlas.Atlas) *Unmarshaller {
|
||||
m := new(Unmarshaller)
|
||||
m.unmarshal = cbor.NewUnmarshallerAtlased(cbor.DecodeOptions{CoerceUndefToNull: true}, &m.reader, atl)
|
||||
return m
|
||||
}
|
||||
|
||||
// Decode reads a CBOR object from the given reader and decodes it into the
|
||||
// given object.
|
||||
func (m *Unmarshaller) Decode(r io.Reader, obj interface{}) error {
|
||||
m.reader.r = r
|
||||
err := m.unmarshal.Unmarshal(obj)
|
||||
m.reader.r = nil
|
||||
return err
|
||||
}
|
||||
|
||||
// Unmarshal unmarshals the given CBOR byte slice into the given object.
|
||||
func (m *Unmarshaller) Unmarshal(b []byte, obj interface{}) error {
|
||||
return m.Decode(bytes.NewReader(b), obj)
|
||||
}
|
||||
|
||||
// PooledUnmarshaller is a thread-safe pooled CBOR unmarshaller.
|
||||
type PooledUnmarshaller struct {
|
||||
pool sync.Pool
|
||||
}
|
||||
|
||||
// NewPooledUnmarshaller returns a PooledUnmarshaller with the given atlas. Do
|
||||
// not copy after use.
|
||||
func NewPooledUnmarshaller(atl atlas.Atlas) PooledUnmarshaller {
|
||||
return PooledUnmarshaller{
|
||||
pool: sync.Pool{
|
||||
New: func() interface{} {
|
||||
return NewUnmarshallerAtlased(atl)
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Decode decodes an object from the passed reader into the given object using
|
||||
// the pool of unmarshallers.
|
||||
func (p *PooledUnmarshaller) Decode(r io.Reader, obj interface{}) error {
|
||||
u := p.pool.Get().(*Unmarshaller)
|
||||
err := u.Decode(r, obj)
|
||||
p.pool.Put(u)
|
||||
return err
|
||||
}
|
||||
|
||||
// Unmarshal unmarshals the passed object using the pool of unmarshallers.
|
||||
func (p *PooledUnmarshaller) Unmarshal(b []byte, obj interface{}) error {
|
||||
u := p.pool.Get().(*Unmarshaller)
|
||||
err := u.Unmarshal(b, obj)
|
||||
p.pool.Put(u)
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user