go-libp2p-peerstore/pb/custom.go
Steven Allen 0b5b389640 dep: switch to core (#80)
* dep: switch to core

Specifically, remove go-libp2p-{crypto,peer} deps.

* catch a few more usages of deprecated packages.
2019-05-31 14:51:16 +01:00

111 lines
2.6 KiB
Go

package pstore_pb
import (
"encoding/json"
proto "github.com/gogo/protobuf/proto"
peer "github.com/libp2p/go-libp2p-core/peer"
pt "github.com/libp2p/go-libp2p-core/test"
ma "github.com/multiformats/go-multiaddr"
)
// customGogoType aggregates the interfaces that custom Gogo types need to implement.
// it is only used for type assertions.
type customGogoType interface {
proto.Marshaler
proto.Unmarshaler
json.Marshaler
json.Unmarshaler
proto.Sizer
}
// ProtoAddr is a custom type used by gogo to serde raw peer IDs into the peer.ID type, and back.
type ProtoPeerID struct {
peer.ID
}
var _ customGogoType = (*ProtoPeerID)(nil)
func (id ProtoPeerID) Marshal() ([]byte, error) {
return []byte(id.ID), nil
}
func (id ProtoPeerID) MarshalTo(data []byte) (n int, err error) {
return copy(data, []byte(id.ID)), nil
}
func (id ProtoPeerID) MarshalJSON() ([]byte, error) {
m, _ := id.Marshal()
return json.Marshal(m)
}
func (id *ProtoPeerID) Unmarshal(data []byte) (err error) {
id.ID = peer.ID(string(data))
return nil
}
func (id *ProtoPeerID) UnmarshalJSON(data []byte) error {
var v []byte
err := json.Unmarshal(data, &v)
if err != nil {
return err
}
return id.Unmarshal(v)
}
func (id ProtoPeerID) Size() int {
return len([]byte(id.ID))
}
// ProtoAddr is a custom type used by gogo to serde raw multiaddresses into the ma.Multiaddr type, and back.
type ProtoAddr struct {
ma.Multiaddr
}
var _ customGogoType = (*ProtoAddr)(nil)
func (a ProtoAddr) Marshal() ([]byte, error) {
return a.Bytes(), nil
}
func (a ProtoAddr) MarshalTo(data []byte) (n int, err error) {
return copy(data, a.Bytes()), nil
}
func (a ProtoAddr) MarshalJSON() ([]byte, error) {
m, _ := a.Marshal()
return json.Marshal(m)
}
func (a *ProtoAddr) Unmarshal(data []byte) (err error) {
a.Multiaddr, err = ma.NewMultiaddrBytes(data)
return err
}
func (a *ProtoAddr) UnmarshalJSON(data []byte) error {
v := new([]byte)
err := json.Unmarshal(data, v)
if err != nil {
return err
}
return a.Unmarshal(*v)
}
func (a ProtoAddr) Size() int {
return len(a.Bytes())
}
// NewPopulatedProtoAddr generates a populated instance of the custom gogo type ProtoAddr.
// It is required by gogo-generated tests.
func NewPopulatedProtoAddr(r randyPstore) *ProtoAddr {
a, _ := ma.NewMultiaddr("/ip4/123.123.123.123/tcp/7001")
return &ProtoAddr{Multiaddr: a}
}
// NewPopulatedProtoPeerID generates a populated instance of the custom gogo type ProtoPeerID.
// It is required by gogo-generated tests.
func NewPopulatedProtoPeerID(r randyPstore) *ProtoPeerID {
id, _ := pt.RandPeerID()
return &ProtoPeerID{ID: id}
}