|
| 1 | +package multicodec |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/ipld/go-ipld-prime" |
| 5 | +) |
| 6 | + |
| 7 | +// DefaultRegistry is a multicodec.Registry instance which is global to the program, |
| 8 | +// and is used as a default set of codecs. |
| 9 | +// |
| 10 | +// Some systems (for example, cidlink.DefaultLinkSystem) will use this default registry, |
| 11 | +// which makes it easier to write programs that pass fewer explicit arguments around. |
| 12 | +// However, these are *only* for default behaviors; |
| 13 | +// variations of functions which allow explicit non-default options should always be available |
| 14 | +// (for example, cidlink also has other LinkSystem constructor functions which accept an explicit multicodec.Registry, |
| 15 | +// and the LookupEncoder and LookupDecoder functions in any LinkSystem can be replaced). |
| 16 | +// |
| 17 | +// Since this registry is global, mind that there are also some necessary tradeoffs and limitations: |
| 18 | +// It can be difficult to control exactly what's present in this global registry |
| 19 | +// (Libraries may register codecs in this registry as a side-effect of importing, so even transitive dependencies can affect its content!). |
| 20 | +// Also, this registry is only considered safe to modify at package init time. |
| 21 | +// If these are concerns for your program, you can create your own multicodec.Registry values, |
| 22 | +// and eschew using the global default. |
| 23 | +var DefaultRegistry = Registry{} |
| 24 | + |
| 25 | +// RegisterEncoder updates the global DefaultRegistry to map a multicodec indicator number to the given ipld.Encoder function. |
| 26 | +// The encoder functions registered can be subsequently looked up using LookupEncoder. |
| 27 | +// It is a shortcut to the RegisterEncoder method on the global DefaultRegistry. |
| 28 | +// |
| 29 | +// Packages which implement an IPLD codec and have a multicodec number associated with them |
| 30 | +// are encouraged to register themselves at package init time using this function. |
| 31 | +// (Doing this at package init time ensures the default global registry is populated |
| 32 | +// without causing race conditions for application code.) |
| 33 | +// |
| 34 | +// No effort is made to detect conflicting registrations in this map. |
| 35 | +// If your dependency tree is such that this becomes a problem, |
| 36 | +// there are two ways to address this: |
| 37 | +// If RegisterEncoder is called with the same indicator code more than once, the last call wins. |
| 38 | +// In practice, this means that if an application has a strong opinion about what implementation for a certain codec, |
| 39 | +// then this can be done by making a Register call with that effect at init time in the application's main package. |
| 40 | +// This should have the desired effect because the root of the import tree has its init time effect last. |
| 41 | +// Alternatively, one can just avoid use of this registry entirely: |
| 42 | +// do this by making a LinkSystem that uses a custom EncoderChooser function. |
| 43 | +func RegisterEncoder(indicator uint64, encodeFunc ipld.Encoder) { |
| 44 | + DefaultRegistry.RegisterEncoder(indicator, encodeFunc) |
| 45 | +} |
| 46 | + |
| 47 | +// LookupEncoder yields an ipld.Encoder function matching a multicodec indicator code number. |
| 48 | +// It is a shortcut to the LookupEncoder method on the global DefaultRegistry. |
| 49 | +// |
| 50 | +// To be available from this lookup function, an encoder must have been registered |
| 51 | +// for this indicator number by an earlier call to the RegisterEncoder function. |
| 52 | +func LookupEncoder(indicator uint64) (ipld.Encoder, error) { |
| 53 | + return DefaultRegistry.LookupEncoder(indicator) |
| 54 | +} |
| 55 | + |
| 56 | +// RegisterDecoder updates the global DefaultRegistry a map a multicodec indicator number to the given ipld.Decoder function. |
| 57 | +// The decoder functions registered can be subsequently looked up using LookupDecoder. |
| 58 | +// It is a shortcut to the RegisterDecoder method on the global DefaultRegistry. |
| 59 | +// |
| 60 | +// Packages which implement an IPLD codec and have a multicodec number associated with them |
| 61 | +// are encouraged to register themselves in this map at package init time. |
| 62 | +// (Doing this at package init time ensures the default global registry is populated |
| 63 | +// without causing race conditions for application code.) |
| 64 | +// |
| 65 | +// No effort is made to detect conflicting registrations in this map. |
| 66 | +// If your dependency tree is such that this becomes a problem, |
| 67 | +// there are two ways to address this: |
| 68 | +// If RegisterDecoder is called with the same indicator code more than once, the last call wins. |
| 69 | +// In practice, this means that if an application has a strong opinion about what implementation for a certain codec, |
| 70 | +// then this can be done by making a Register call with that effect at init time in the application's main package. |
| 71 | +// This should have the desired effect because the root of the import tree has its init time effect last. |
| 72 | +// Alternatively, one can just avoid use of this registry entirely: |
| 73 | +// do this by making a LinkSystem that uses a custom DecoderChooser function. |
| 74 | +func RegisterDecoder(indicator uint64, decodeFunc ipld.Decoder) { |
| 75 | + DefaultRegistry.RegisterDecoder(indicator, decodeFunc) |
| 76 | +} |
| 77 | + |
| 78 | +// LookupDecoder yields an ipld.Decoder function matching a multicodec indicator code number. |
| 79 | +// It is a shortcut to the LookupDecoder method on the global DefaultRegistry. |
| 80 | +// |
| 81 | +// To be available from this lookup function, an decoder must have been registered |
| 82 | +// for this indicator number by an earlier call to the RegisterDecoder function. |
| 83 | +func LookupDecoder(indicator uint64) (ipld.Decoder, error) { |
| 84 | + return DefaultRegistry.LookupDecoder(indicator) |
| 85 | +} |
0 commit comments