multicodecRegistry.go 1.14 KB
Newer Older
1 2 3 4 5 6 7 8 9
package cidlink

import "fmt"

var (
	multicodecDecodeTable MulticodecDecodeTable
	multicodecEncodeTable MulticodecEncodeTable
)

10 11 12 13 14
func init() {
	multicodecEncodeTable = make(MulticodecEncodeTable)
	multicodecDecodeTable = make(MulticodecDecodeTable)
}

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
// RegisterMulticodecDecoder is used to register multicodec features.
// It adjusts a global registry and may only be used at program init time;
// it is meant to provide a plugin system, not a configuration mechanism.
func RegisterMulticodecDecoder(hook uint64, fn MulticodecDecoder) {
	_, exists := multicodecDecodeTable[hook]
	if exists {
		panic(fmt.Errorf("multicodec decoder already registered for %x", hook))
	}
	multicodecDecodeTable[hook] = fn
}

// RegisterMulticodecEncoder is used to register multicodec features.
// It adjusts a global registry and may only be used at program init time;
// it is meant to provide a plugin system, not a configuration mechanism.
func RegisterMulticodecEncoder(hook uint64, fn MulticodecEncoder) {
	_, exists := multicodecEncodeTable[hook]
	if exists {
		panic(fmt.Errorf("multicodec encoder already registered for %x", hook))
	}
	multicodecEncodeTable[hook] = fn
}