registry.go 4.06 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
package multicodec

import (
	"fmt"

	"github.com/ipld/go-ipld-prime"
)

// Registry is a structure for storing mappings of multicodec indicator numbers to ipld.Encoder and ipld.Decoder functions.
//
// The most typical usage of this structure is in combination with an ipld.LinkSystem.
// For example, a linksystem using CIDs and a custom multicodec registry can be constructed
// using cidlink.LinkSystemUsingMulticodecRegistry.
//
// Registry includes no mutexing.  If using Registry in a concurrent context, you must handle synchronization yourself.
// (Typically, it is recommended to do initialization earlier in a program, before fanning out goroutines;
// this avoids the need for mutexing overhead.)
//
// go-ipld-prime also has a default registry, which has the same methods as this structure, but are at package scope.
// Some systems, like cidlink.DefaultLinkSystem, will use this default registry.
// However, this default registry is global to the entire program.
// This Registry type is for helping if you wish to make your own registry which does not share that global state.
//
// Multicodec indicator numbers are specified in
// https://github.com/multiformats/multicodec/blob/master/table.csv .
// You should not use indicator numbers which are not specified in that table
// (however, there is nothing in this implementation that will attempt to stop you, either; please behave).
type Registry struct {
	encoders map[uint64]ipld.Encoder
	decoders map[uint64]ipld.Decoder
}

func (r *Registry) ensureInit() {
	if r.encoders != nil {
		return
	}
	r.encoders = make(map[uint64]ipld.Encoder)
	r.decoders = make(map[uint64]ipld.Decoder)
}

// RegisterEncoder updates a simple map of multicodec indicator number to ipld.Encoder function.
// The encoder functions registered can be subsequently looked up using LookupEncoder.
func (r *Registry) RegisterEncoder(indicator uint64, encodeFunc ipld.Encoder) {
	r.ensureInit()
	if encodeFunc == nil {
		panic("not sensible to attempt to register a nil function")
	}
	r.encoders[indicator] = encodeFunc
}

// LookupEncoder yields an ipld.Encoder function matching a multicodec indicator code number.
//
// To be available from this lookup function, an encoder must have been registered
// for this indicator number by an earlier call to the RegisterEncoder function.
func (r *Registry) LookupEncoder(indicator uint64) (ipld.Encoder, error) {
	encodeFunc, exists := r.encoders[indicator]
	if !exists {
		return nil, fmt.Errorf("no encoder registered for multicodec code %d (0x%x)", indicator, indicator)
	}
	return encodeFunc, nil
}

63 64 65 66 67 68 69 70 71 72
// ListEncoders returns a list of multicodec indicators for which an ipld.Encoder is registered.
// The list is in no particular order.
func (r *Registry) ListEncoders() []uint64 {
	encoders := make([]uint64, 0, len(r.encoders))
	for e := range r.encoders {
		encoders = append(encoders, e)
	}
	return encoders
}

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
// RegisterDecoder updates a simple map of multicodec indicator number to ipld.Decoder function.
// The decoder functions registered can be subsequently looked up using LookupDecoder.
func (r *Registry) RegisterDecoder(indicator uint64, decodeFunc ipld.Decoder) {
	r.ensureInit()
	if decodeFunc == nil {
		panic("not sensible to attempt to register a nil function")
	}
	r.decoders[indicator] = decodeFunc
}

// LookupDecoder yields an ipld.Decoder function matching a multicodec indicator code number.
//
// To be available from this lookup function, an decoder must have been registered
// for this indicator number by an earlier call to the RegisterDecoder function.
func (r *Registry) LookupDecoder(indicator uint64) (ipld.Decoder, error) {
	decodeFunc, exists := r.decoders[indicator]
	if !exists {
		return nil, fmt.Errorf("no decoder registered for multicodec code %d (0x%x)", indicator, indicator)
	}
	return decodeFunc, nil
}
94 95 96 97 98 99 100 101 102 103

// ListDecoders returns a list of multicodec indicators for which an ipld.Decoder is registered.
// The list is in no particular order.
func (r *Registry) ListDecoders() []uint64 {
	decoders := make([]uint64, 0, len(r.decoders))
	for d := range r.decoders {
		decoders = append(decoders, d)
	}
	return decoders
}