This repository has been archived by the owner on Dec 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make peer.ID implement PB and JSON (un-)marshalling interfaces.
- Loading branch information
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// This file contains Protobuf and JSON serialization/deserialization methods for peer IDs. | ||
package peer | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/golang/protobuf/proto" | ||
) | ||
|
||
var _ proto.Marshaler = (*ID)(nil) | ||
var _ proto.Unmarshaler = (*ID)(nil) | ||
var _ json.Marshaler = (*ID)(nil) | ||
var _ proto.Unmarshaler = (*ID)(nil) | ||
|
||
func (id ID) Marshal() ([]byte, error) { | ||
return []byte(id), nil | ||
} | ||
|
||
func (id ID) MarshalTo(data []byte) (n int, err error) { | ||
return copy(data, []byte(id)), nil | ||
} | ||
|
||
func (id *ID) Unmarshal(data []byte) (err error) { | ||
*id, err = IDFromBytes(data) | ||
return err | ||
} | ||
|
||
func (id ID) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(IDB58Encode(id)) | ||
} | ||
|
||
func (id *ID) UnmarshalJSON(data []byte) (err error) { | ||
var v string | ||
if err = json.Unmarshal(data, &v); err != nil { | ||
return err | ||
} | ||
*id, err = IDB58Decode(v) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package peer_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/libp2p/go-libp2p-peer" | ||
"github.com/libp2p/go-libp2p-peer/test" | ||
) | ||
|
||
func TestPeerSerdePB(t *testing.T) { | ||
id, err := testutil.RandPeerID() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
b, err := id.Marshal() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
var id2 peer.ID | ||
if err = id2.Unmarshal(b); err != nil { | ||
t.Fatal(err) | ||
} | ||
if id != id2 { | ||
t.Error("expected equal ids in circular serde test") | ||
} | ||
} | ||
|
||
func TestPeerSerdeJSON(t *testing.T) { | ||
id, err := testutil.RandPeerID() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
b, err := id.MarshalJSON() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
var id2 peer.ID | ||
if err = id2.UnmarshalJSON(b); err != nil { | ||
t.Fatal(err) | ||
} | ||
if id != id2 { | ||
t.Error("expected equal ids in circular serde test") | ||
} | ||
} |