Skip to content
This repository was archived by the owner on Dec 7, 2019. It is now read-only.

Commit

Permalink
Merge pull request #44 from libp2p/marshalers
Browse files Browse the repository at this point in the history
Let ID implement encoding.Binary[Un]Marshaler and encoding.Text[Un]Marshaler
  • Loading branch information
hsanjuan authored Feb 25, 2019
2 parents 9a193a6 + c8b1a8b commit 9b0c59c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gx/lastpubver
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.1.1: QmTu65MVbemtUxJEWgsTtzv9Zv9P8rvmqNA4eG9TrTRGYc
3.1.2: QmYVXrKrKHDC9FobgmcmshCDyWwdrfwfanNQN4oxJ9Fk3h
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
"license": "MIT",
"name": "go-libp2p-peer",
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
"version": "3.1.1"
"version": "3.1.2"
}

31 changes: 31 additions & 0 deletions peer_serde.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package peer

import (
"encoding"
"encoding/json"
)

Expand All @@ -11,10 +12,20 @@ import (
var _ json.Marshaler = (*ID)(nil)
var _ json.Unmarshaler = (*ID)(nil)

var _ encoding.BinaryMarshaler = (*ID)(nil)
var _ encoding.BinaryUnmarshaler = (*ID)(nil)
var _ encoding.TextMarshaler = (*ID)(nil)
var _ encoding.TextUnmarshaler = (*ID)(nil)

func (id ID) Marshal() ([]byte, error) {
return []byte(id), nil
}

// BinaryMarshal returns the byte representation of the peer ID.
func (id ID) MarshalBinary() ([]byte, error) {
return id.Marshal()
}

func (id ID) MarshalTo(data []byte) (n int, err error) {
return copy(data, []byte(id)), nil
}
Expand All @@ -24,6 +35,11 @@ func (id *ID) Unmarshal(data []byte) (err error) {
return err
}

// BinaryUnmarshal sets the ID from its binary representation.
func (id *ID) UnmarshalBinary(data []byte) error {
return id.Unmarshal(data)
}

// Implements Gogo's proto.Sizer, but we omit the compile-time assertion to avoid introducing a hard
// dependency on gogo.
func (id ID) Size() int {
Expand All @@ -42,3 +58,18 @@ func (id *ID) UnmarshalJSON(data []byte) (err error) {
*id, err = IDB58Decode(v)
return err
}

// TextMarshal returns the text encoding of the ID.
func (id ID) MarshalText() ([]byte, error) {
return []byte(IDB58Encode(id)), nil
}

// TextUnmarshal restores the ID from its text encoding.
func (id *ID) UnmarshalText(data []byte) error {
pid, err := IDB58Decode(string(data))
if err != nil {
return err
}
*id = pid
return nil
}
38 changes: 38 additions & 0 deletions peer_serde_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,41 @@ func TestPeerSerdeJSON(t *testing.T) {
t.Error("expected equal ids in circular serde test")
}
}

func TestBinaryMarshaler(t *testing.T) {
id, err := testutil.RandPeerID()
if err != nil {
t.Fatal(err)
}
b, err := id.MarshalBinary()
if err != nil {
t.Fatal(err)
}

var id2 peer.ID
if err = id2.UnmarshalBinary(b); err != nil {
t.Fatal(err)
}
if id != id2 {
t.Error("expected equal ids in circular serde test")
}
}

func TestTextMarshaler(t *testing.T) {
id, err := testutil.RandPeerID()
if err != nil {
t.Fatal(err)
}
b, err := id.MarshalText()
if err != nil {
t.Fatal(err)
}

var id2 peer.ID
if err = id2.UnmarshalText(b); err != nil {
t.Fatal(err)
}
if id != id2 {
t.Error("expected equal ids in circular serde test")
}
}

0 comments on commit 9b0c59c

Please sign in to comment.