Skip to content
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

Thread bindnode options through convenience calls #572

Merged
merged 1 commit into from
Sep 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions codecHelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@
// Note that not all features of IPLD Schemas can be inferred from golang types alone.
// For example, to use union types, the schema parameter will be required.
// Similarly, to use most kinds of non-default representation strategy, the schema parameter is needed in order to convey that intention.
func Marshal(encFn Encoder, bind interface{}, typ schema.Type) ([]byte, error) {
n := bindnode.Wrap(bind, typ)
func Marshal(encFn Encoder, bind interface{}, typ schema.Type, opts ...bindnode.Option) ([]byte, error) {
n := bindnode.Wrap(bind, typ, opts...)
return Encode(n.Representation(), encFn)
}

// MarshalStreaming is like Marshal, but emits output to an io.Writer.
func MarshalStreaming(wr io.Writer, encFn Encoder, bind interface{}, typ schema.Type) error {
n := bindnode.Wrap(bind, typ)
func MarshalStreaming(wr io.Writer, encFn Encoder, bind interface{}, typ schema.Type, opts ...bindnode.Option) error {
n := bindnode.Wrap(bind, typ, opts...)

Check warning on line 118 in codecHelpers.go

View check run for this annotation

Codecov / codecov/patch

codecHelpers.go#L117-L118

Added lines #L117 - L118 were not covered by tests
return EncodeStreaming(wr, n.Representation(), encFn)
}

Expand Down Expand Up @@ -146,14 +146,14 @@
// and a Node will still be returned based on that type.
// bindnode.Unwrap can be used on that Node and will still return something
// of the same golang type as the typed nil that was given as the 'bind' parameter.
func Unmarshal(b []byte, decFn Decoder, bind interface{}, typ schema.Type) (Node, error) {
return UnmarshalStreaming(bytes.NewReader(b), decFn, bind, typ)
func Unmarshal(b []byte, decFn Decoder, bind interface{}, typ schema.Type, opts ...bindnode.Option) (Node, error) {
return UnmarshalStreaming(bytes.NewReader(b), decFn, bind, typ, opts...)
}

// UnmarshalStreaming is like Unmarshal, but works on an io.Reader for input.
func UnmarshalStreaming(r io.Reader, decFn Decoder, bind interface{}, typ schema.Type) (Node, error) {
func UnmarshalStreaming(r io.Reader, decFn Decoder, bind interface{}, typ schema.Type, opts ...bindnode.Option) (Node, error) {
// Decode is fairly straightforward.
np := bindnode.Prototype(bind, typ)
np := bindnode.Prototype(bind, typ, opts...)
n, err := DecodeStreamingUsingPrototype(r, decFn, np.Representation())
if err != nil {
return nil, err
Expand All @@ -165,6 +165,6 @@
}
// ... and we also have to re-bind a new node to the 'bind' value,
// because probably the user will be surprised if mutating 'bind' doesn't affect the Node later.
n = bindnode.Wrap(bind, typ)
n = bindnode.Wrap(bind, typ, opts...)
return n, err
}
Loading