Cleanup go.mod, document things better
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
package encoder
|
||||
|
||||
// EncodeValue is a helper that handles conversion of values
|
||||
// byte slices and strings are converted directly, everything else is marshalled
|
||||
func EncodeValue(encoder Encoder, val any) ([]byte, error) {
|
||||
var v []byte
|
||||
|
||||
@ -14,6 +16,8 @@ func EncodeValue(encoder Encoder, val any) ([]byte, error) {
|
||||
return v, nil
|
||||
}
|
||||
|
||||
// DecodeValue is a helper that handles decoding of values
|
||||
// byte slices and strings can be assigned directly
|
||||
func DecodeValue(encoder Encoder, b []byte, v any) error {
|
||||
switch v := v.(type) {
|
||||
case *[]byte:
|
||||
|
@ -10,23 +10,29 @@ var (
|
||||
XML xmlEncoder
|
||||
JSON jsonEncoder
|
||||
MsgPack msgPackEncoder
|
||||
|
||||
encoders = map[string]Encoder{
|
||||
"json": JSON,
|
||||
"xml": XML,
|
||||
"msgpack": MsgPack,
|
||||
}
|
||||
)
|
||||
|
||||
func EncoderFrom(name string) Encoder {
|
||||
var enc Encoder
|
||||
|
||||
switch name {
|
||||
case "json":
|
||||
enc = JSON
|
||||
case "xml":
|
||||
enc = XML
|
||||
default: // Default is also msgpack
|
||||
enc = MsgPack
|
||||
}
|
||||
|
||||
return enc
|
||||
// Register allows you to register your own encoders
|
||||
func Register(name string, enc Encoder) {
|
||||
encoders[name] = enc
|
||||
}
|
||||
|
||||
// From will return an encoder for the specified key
|
||||
func From(name string) Encoder {
|
||||
if enc, ok := encoders[name]; ok {
|
||||
return enc
|
||||
}
|
||||
|
||||
return MsgPack
|
||||
}
|
||||
|
||||
// Encoder is the base interface for encoding/decoding values
|
||||
type Encoder interface {
|
||||
Unmarshal(b []byte, dest any) error
|
||||
Marshal(value any) ([]byte, error)
|
||||
|
Reference in New Issue
Block a user