-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor registry and encoding #50
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
460fcc8
feat(encoding): add library to handle encoding
hannahhoward 49d5684
feat(registry): add simple registry
hannahhoward 302c5ec
feat(datatransfer): incorporate registry
hannahhoward ea00968
feat(message): move decoding inside messages
hannahhoward 2e95a7d
feat(message): convert to cbg.Deferred
hannahhoward 4269dc6
refactor(graphsync): remove utils
hannahhoward b6f5f31
refactor(registry): move types to root
hannahhoward 3a5b05d
refactor(datatransfer): minor refactors and cleanups
hannahhoward 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
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,122 @@ | ||
package encoding | ||
|
||
import ( | ||
"bytes" | ||
"reflect" | ||
|
||
cbor "github.com/ipfs/go-ipld-cbor" | ||
"github.com/ipld/go-ipld-prime" | ||
"github.com/ipld/go-ipld-prime/codec/dagcbor" | ||
cborgen "github.com/whyrusleeping/cbor-gen" | ||
"golang.org/x/xerrors" | ||
) | ||
|
||
// Encodable is an object that can be written to CBOR and decoded back | ||
type Encodable interface{} | ||
|
||
// Encode encodes an encodable to CBOR, using the best available path for | ||
// writing to CBOR | ||
func Encode(value Encodable) ([]byte, error) { | ||
if cbgEncodable, ok := value.(cborgen.CBORMarshaler); ok { | ||
buf := new(bytes.Buffer) | ||
err := cbgEncodable.MarshalCBOR(buf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return buf.Bytes(), nil | ||
} | ||
if ipldEncodable, ok := value.(ipld.Node); ok { | ||
buf := new(bytes.Buffer) | ||
err := dagcbor.Encoder(ipldEncodable, buf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return buf.Bytes(), nil | ||
} | ||
return cbor.DumpObject(value) | ||
} | ||
|
||
// Decoder is CBOR decoder for a given encodable type | ||
type Decoder interface { | ||
DecodeFromCbor([]byte) (Encodable, error) | ||
} | ||
|
||
// NewDecoder creates a new Decoder that will decode into new instances of the given | ||
// object type. It will use the decoding that is optimal for that type | ||
// It returns error if it's not possible to setup a decoder for this type | ||
func NewDecoder(decodeType Encodable) (Decoder, error) { | ||
// check if type is ipld.Node, if so, just use style | ||
if ipldDecodable, ok := decodeType.(ipld.Node); ok { | ||
return &ipldDecoder{ipldDecodable.Style()}, nil | ||
} | ||
// check if type is a pointer, as we need that to make new copies | ||
// for cborgen types & regular IPLD types | ||
decodeReflectType := reflect.TypeOf(decodeType) | ||
if decodeReflectType.Kind() != reflect.Ptr { | ||
return nil, xerrors.New("type must be a pointer") | ||
} | ||
// check if type is a cbor-gen type | ||
if _, ok := decodeType.(cborgen.CBORUnmarshaler); ok { | ||
return &cbgDecoder{decodeReflectType}, nil | ||
} | ||
// type does is neither ipld-prime nor cbor-gen, so we need to see if it | ||
// can rountrip with oldschool ipld-format | ||
encoded, err := cbor.DumpObject(decodeType) | ||
if err != nil { | ||
return nil, xerrors.New("Object type did not encode") | ||
} | ||
newDecodable := reflect.New(decodeReflectType.Elem()).Interface() | ||
if err := cbor.DecodeInto(encoded, newDecodable); err != nil { | ||
return nil, xerrors.New("Object type did not decode") | ||
} | ||
return &defaultDecoder{decodeReflectType}, nil | ||
} | ||
|
||
type ipldDecoder struct { | ||
style ipld.NodeStyle | ||
} | ||
|
||
func (decoder *ipldDecoder) DecodeFromCbor(encoded []byte) (Encodable, error) { | ||
builder := decoder.style.NewBuilder() | ||
buf := bytes.NewReader(encoded) | ||
err := dagcbor.Decoder(builder, buf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return builder.Build(), nil | ||
} | ||
|
||
type cbgDecoder struct { | ||
cbgType reflect.Type | ||
} | ||
|
||
func (decoder *cbgDecoder) DecodeFromCbor(encoded []byte) (Encodable, error) { | ||
decodedValue := reflect.New(decoder.cbgType.Elem()) | ||
decoded, ok := decodedValue.Interface().(cborgen.CBORUnmarshaler) | ||
if !ok || reflect.ValueOf(decoded).IsNil() { | ||
return nil, xerrors.New("problem instantiating decoded value") | ||
} | ||
buf := bytes.NewReader(encoded) | ||
err := decoded.UnmarshalCBOR(buf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return decoded, nil | ||
} | ||
|
||
type defaultDecoder struct { | ||
ptrType reflect.Type | ||
} | ||
|
||
func (decoder *defaultDecoder) DecodeFromCbor(encoded []byte) (Encodable, error) { | ||
decodedValue := reflect.New(decoder.ptrType.Elem()) | ||
decoded, ok := decodedValue.Interface().(Encodable) | ||
if !ok || reflect.ValueOf(decoded).IsNil() { | ||
return nil, xerrors.New("problem instantiating decoded value") | ||
} | ||
err := cbor.DecodeInto(encoded, decoded) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return decoded, nil | ||
} |
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,37 @@ | ||
package encoding_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/filecoin-project/go-data-transfer/encoding" | ||
"github.com/filecoin-project/go-data-transfer/encoding/testdata" | ||
) | ||
|
||
func TestRoundTrip(t *testing.T) { | ||
testCases := map[string]struct { | ||
val encoding.Encodable | ||
}{ | ||
"can encode/decode IPLD prime types": { | ||
val: testdata.Prime, | ||
}, | ||
"can encode/decode cbor-gen types": { | ||
val: testdata.Cbg, | ||
}, | ||
"can encode/decode old ipld format types": { | ||
val: testdata.Standard, | ||
}, | ||
} | ||
for testCase, data := range testCases { | ||
t.Run(testCase, func(t *testing.T) { | ||
encoded, err := encoding.Encode(data.val) | ||
require.NoError(t, err) | ||
decoder, err := encoding.NewDecoder(data.val) | ||
require.NoError(t, err) | ||
decoded, err := decoder.DecodeFromCbor(encoded) | ||
require.NoError(t, err) | ||
require.Equal(t, data.val, decoded) | ||
}) | ||
} | ||
} |
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,37 @@ | ||
package testdata | ||
|
||
import ( | ||
cbor "github.com/ipfs/go-ipld-cbor" | ||
"github.com/ipld/go-ipld-prime/fluent" | ||
basicnode "github.com/ipld/go-ipld-prime/node/basic" | ||
) | ||
|
||
// Prime = an instance of an ipld prime piece of data | ||
var Prime = fluent.MustBuildMap(basicnode.Style.Map, 2, func(na fluent.MapAssembler) { | ||
nva := na.AssembleEntry("X") | ||
nva.AssignInt(100) | ||
nva = na.AssembleEntry("Y") | ||
nva.AssignString("appleSauce") | ||
}) | ||
|
||
type standardType struct { | ||
X int | ||
Y string | ||
} | ||
|
||
func init() { | ||
cbor.RegisterCborType(standardType{}) | ||
} | ||
|
||
// Standard = an instance that is neither ipld prime nor cbor | ||
var Standard *standardType = &standardType{X: 100, Y: "appleSauce"} | ||
|
||
//go:generate cbor-gen-for cbgType | ||
|
||
type cbgType struct { | ||
X uint64 | ||
Y string | ||
} | ||
|
||
// Cbg = an instance of a cbor-gen type | ||
var Cbg *cbgType = &cbgType{X: 100, Y: "appleSauce"} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Oops, something went wrong.
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.
❤️
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.
@shannonwells now that you've turned me on to them... I am all about those table tests.