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
Protobuf and JSON (un-)marshalling methods for peer.ID #41
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,45 @@ | ||
// 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 | ||
} | ||
|
||
// 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 { | ||
return len([]byte(id)) | ||
} | ||
|
||
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") | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This pulls protobuf into quite a few deps. Given that it's only for a type assertion, I'd rather just leave it out for now.
Once we merge all the "core" deps into a single package, we can do stuff like this without complicating our lives quite as much.
If you think we should keep it, we'll have to add it to the
package.json
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 SGTM, will comment out the assertions to keep them as docs.