github.com/ipld/go-ipld-prime@v0.21.0/multicodec/defaultRegistry.go (about) 1 package multicodec 2 3 import ( 4 "github.com/ipld/go-ipld-prime/codec" 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 codec.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 codec.Encoder) { 44 DefaultRegistry.RegisterEncoder(indicator, encodeFunc) 45 } 46 47 // LookupEncoder yields a codec.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) (codec.Encoder, error) { 53 return DefaultRegistry.LookupEncoder(indicator) 54 } 55 56 // ListEncoders returns a list of multicodec indicators for which a codec.Encoder is registered. 57 // The list is in no particular order. 58 // It is a shortcut to the ListEncoders method on the global DefaultRegistry. 59 // 60 // Be judicious about trying to use this function outside of debugging. 61 // Because the global default registry is global and easily modified, 62 // and can be changed by any of the transitive dependencies of your program, 63 // its contents are not particularly stable. 64 // In particular, it is not recommended to make any behaviors of your program conditional 65 // based on information returned by this function -- if your program needs conditional 66 // behavior based on registred codecs, you may want to consider taking more explicit control 67 // and using your own non-default registry. 68 func ListEncoders() []uint64 { 69 return DefaultRegistry.ListEncoders() 70 } 71 72 // RegisterDecoder updates the global DefaultRegistry a map a multicodec indicator number to the given codec.Decoder function. 73 // The decoder functions registered can be subsequently looked up using LookupDecoder. 74 // It is a shortcut to the RegisterDecoder method on the global DefaultRegistry. 75 // 76 // Packages which implement an IPLD codec and have a multicodec number associated with them 77 // are encouraged to register themselves in this map at package init time. 78 // (Doing this at package init time ensures the default global registry is populated 79 // without causing race conditions for application code.) 80 // 81 // No effort is made to detect conflicting registrations in this map. 82 // If your dependency tree is such that this becomes a problem, 83 // there are two ways to address this: 84 // If RegisterDecoder is called with the same indicator code more than once, the last call wins. 85 // In practice, this means that if an application has a strong opinion about what implementation for a certain codec, 86 // then this can be done by making a Register call with that effect at init time in the application's main package. 87 // This should have the desired effect because the root of the import tree has its init time effect last. 88 // Alternatively, one can just avoid use of this registry entirely: 89 // do this by making a LinkSystem that uses a custom DecoderChooser function. 90 func RegisterDecoder(indicator uint64, decodeFunc codec.Decoder) { 91 DefaultRegistry.RegisterDecoder(indicator, decodeFunc) 92 } 93 94 // LookupDecoder yields a codec.Decoder function matching a multicodec indicator code number. 95 // It is a shortcut to the LookupDecoder method on the global DefaultRegistry. 96 // 97 // To be available from this lookup function, an decoder must have been registered 98 // for this indicator number by an earlier call to the RegisterDecoder function. 99 func LookupDecoder(indicator uint64) (codec.Decoder, error) { 100 return DefaultRegistry.LookupDecoder(indicator) 101 } 102 103 // ListDecoders returns a list of multicodec indicators for which a codec.Decoder is registered. 104 // The list is in no particular order. 105 // It is a shortcut to the ListDecoders method on the global DefaultRegistry. 106 // 107 // Be judicious about trying to use this function outside of debugging. 108 // Because the global default registry is global and easily modified, 109 // and can be changed by any of the transitive dependencies of your program, 110 // its contents are not particularly stable. 111 // In particular, it is not recommended to make any behaviors of your program conditional 112 // based on information returned by this function -- if your program needs conditional 113 // behavior based on registred codecs, you may want to consider taking more explicit control 114 // and using your own non-default registry. 115 func ListDecoders() []uint64 { 116 return DefaultRegistry.ListDecoders() 117 }