Cleanup go.mod, document things better
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing

This commit is contained in:
2023-02-04 20:31:09 -05:00
parent 37776d4257
commit 28a5d131a6
8 changed files with 41 additions and 31 deletions

View File

@ -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:

View File

@ -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)