codec.go 550 B

1234567891011121314151617181920212223242526
  1. package codec
  2. type Codec interface {
  3. Marshal(router string, dataPtr any, err error) ([]byte, error)
  4. Unmarshal([]byte) (int, *MsgPack, error)
  5. ToString(any) string
  6. }
  7. var codecsList = make(map[string]Codec)
  8. func RegisterCodec(name string, codec Codec) {
  9. if codec == nil {
  10. panic("codec: Register provide is nil")
  11. }
  12. if _, dup := codecsList[name]; dup {
  13. panic("codec: Register called twice for provide " + name)
  14. }
  15. codecsList[name] = codec
  16. }
  17. func GetCodec(name string) Codec {
  18. if v, ok := codecsList[name]; ok {
  19. return v
  20. }
  21. return nil
  22. }