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.

45 lines
1.4 KiB

2 years ago
package encoding
2 years ago
import (
2 years ago
"fmt"
2 years ago
"strings"
)
2 years ago
2 years ago
// Codec defines the interface Transport uses to encode and decode messages. Note
// that implementations of this interface must be thread safe; a Codec's
// methods can be called from concurrent goroutines.
2 years ago
type Codec interface {
2 years ago
// Marshal returns the wire format of v.
Marshal(v interface{}) ([]byte, error)
// Unmarshal parses the wire format into v.
Unmarshal(data []byte, v interface{}) error
// Name returns the name of the Codec implementation. The returned string
// will be used as part of content type in transmission. The result must be
// static; the result cannot change between calls.
2 years ago
Name() string
}
var registeredCodecs = make(map[string]Codec)
2 years ago
// RegisterCodec registers the provided Codec for use with all Transport clients and
// servers.
2 years ago
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()")
}
2 years ago
contentSubtype := strings.ToLower(codec.Name())
2 years ago
fmt.Println("reg", contentSubtype)
2 years ago
registeredCodecs[contentSubtype] = codec
2 years ago
}
2 years ago
// GetCodec gets a registered Codec by content-subtype, or nil if no Codec is
// registered for the content-subtype.
//
// The content-subtype is expected to be lowercase.
func GetCodec(contentSubtype string) Codec {
return registeredCodecs[contentSubtype]
2 years ago
}