You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
26 lines
557 B
26 lines
557 B
2 years ago
|
package encoding
|
||
|
|
||
|
import "strings"
|
||
|
|
||
|
type Codec interface {
|
||
|
Marshal(any) ([]byte, error)
|
||
|
Unmarshal([]byte, any) error
|
||
|
Name() string
|
||
|
}
|
||
|
|
||
|
var registeredCodecs = make(map[string]Codec)
|
||
|
|
||
|
func RegisterCodec(codec Codec) {
|
||
|
if codec == nil {
|
||
|
panic("cannot register a nil Codec")
|
||
|
}
|
||
|
if codec.Name() == "" {
|
||
|
panic("cannot register Codec with empty string result for Name()")
|
||
|
}
|
||
|
contentSubType := strings.ToLower(codec.Name())
|
||
|
registeredCodecs[contentSubType] = codec
|
||
|
}
|
||
|
func GetCodec(contentSubType string) Codec {
|
||
|
return registeredCodecs[contentSubType]
|
||
|
}
|