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

Remove global variable #12

Merged
merged 1 commit into from
May 30, 2023
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
29 changes: 17 additions & 12 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,36 @@ type codecConverter struct {
converter NodeConverter
}

var codecTable = map[uint64]codecConverter{}

// RegisterCodec registers a specialized prototype & converter for a specific codec
func RegisterCodec(codec uint64, prototype ipld.NodePrototype, converter NodeConverter) {
codecTable[codec] = codecConverter{prototype, converter}
type Decoder struct {
codecTable map[uint64]codecConverter
linkSystemBase ipld.LinkSystem
}

var linkSystemBase ipld.LinkSystem
func NewDecoder() *Decoder {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rvagg @willscott WDYT about having this take a linksystem (or at least the relevant parts for building a linksystem like the decoder and reifier) instead of being stuck with the go-ipld-prime default registry? Could be a functional option if you'd prefer NewDecoder() to continue to work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a different linksystem you need for functionality beyond the transformations exposed by the codec registry here?

would a Decoder.SetLinkSystem(ls) method work?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well it's not just the transformations it's also the passthrough for non-transformed codecs to the go-ipld-prime defaults. I'd like to control those instead of being reliant on whatever gets autoloaded by some package I imported.

Sure a setter would work too. It seems mostly like a stylistic choice compared to a constructor argument so 🤷‍♀️

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding a method isn't a breaking interface change, so would be v0.2.1 - i guess it doesn't matter too much :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#14

lsb := cidlink.DefaultLinkSystem()
lsb.TrustedStorage = true
return &Decoder{
codecTable: map[uint64]codecConverter{},
linkSystemBase: lsb,
}
}

func init() {
linkSystemBase = cidlink.DefaultLinkSystem()
linkSystemBase.TrustedStorage = true
// RegisterCodec registers a specialized prototype & converter for a specific codec
func (d *Decoder) RegisterCodec(codec uint64, prototype ipld.NodePrototype, converter NodeConverter) {
d.codecTable[codec] = codecConverter{prototype, converter}
}

// DecodeNode builds a UniversalNode from a block
func DecodeNode(ctx context.Context, b blocks.Block) (UniversalNode, error) {
func (d *Decoder) DecodeNode(ctx context.Context, b blocks.Block) (UniversalNode, error) {
c := b.Cid()
link := cidlink.Link{Cid: c}
lsys := linkSystemBase
lsys := d.linkSystemBase
lsys.StorageReadOpener = func(lnkCtx ipld.LinkContext, lnk ipld.Link) (io.Reader, error) {
return bytes.NewBuffer(b.RawData()), nil
}

var prototype ipld.NodePrototype = basicnode.Prototype.Any
converter, hasConverter := codecTable[c.Prefix().Codec]
converter, hasConverter := d.codecTable[c.Prefix().Codec]
if hasConverter {
prototype = converter.prototype
}
Expand Down