encoding package

This commit is contained in:
Felipe M 2024-11-23 19:41:21 +01:00
parent 8b8447213f
commit ef80892aa5
Signed by: fmartingr
GPG key ID: CCFBC5637D4000A8
5 changed files with 93 additions and 1 deletions

32
encoding/json.go Normal file
View file

@ -0,0 +1,32 @@
package encoding
import (
"encoding/json"
"io"
"git.nakama.town/fmartingr/gotoolkit/model"
)
var (
_ model.Encoder = &JSONEncoding{}
_ model.Decoder = &JSONEncoding{}
)
type JSONEncoding struct{}
func (e *JSONEncoding) Encode(v any) ([]byte, error) {
return json.Marshal(v)
}
func (e *JSONEncoding) Decode(input []byte, output any) error {
return json.Unmarshal(input, output)
}
func (e *JSONEncoding) DecodeReader(input io.Reader, output any) error {
decoder := json.NewDecoder(input)
return decoder.Decode(output)
}
func NewJSONEncoding() *JSONEncoding {
return &JSONEncoding{}
}

32
encoding/toml.go Normal file
View file

@ -0,0 +1,32 @@
package encoding
import (
"io"
"git.nakama.town/fmartingr/gotoolkit/model"
"github.com/pelletier/go-toml/v2"
)
var (
_ model.Encoder = &TOMLEncoding{}
_ model.Decoder = &TOMLEncoding{}
)
type TOMLEncoding struct{}
func (e *TOMLEncoding) Encode(v any) ([]byte, error) {
return toml.Marshal(v)
}
func (e *TOMLEncoding) Decode(input []byte, output any) error {
return toml.Unmarshal(input, output)
}
func (e *TOMLEncoding) DecodeReader(input io.Reader, output any) error {
decoder := toml.NewDecoder(input)
return decoder.Decode(output)
}
func NewTOMLEncoding() *TOMLEncoding {
return &TOMLEncoding{}
}