Skip to content

Commit

Permalink
Merge pull request #12 from ipfs/feat/decoder
Browse files Browse the repository at this point in the history
Remove global variable

Note that this is a breaking interface change.
  • Loading branch information
willscott committed May 30, 2023
2 parents 219032a + 38abb05 commit 25f06f8
Showing 1 changed file with 17 additions and 12 deletions.
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 {
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

0 comments on commit 25f06f8

Please sign in to comment.