encoding package
This commit is contained in:
parent
8b8447213f
commit
ef80892aa5
5 changed files with 93 additions and 1 deletions
32
encoding/json.go
Normal file
32
encoding/json.go
Normal 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
32
encoding/toml.go
Normal 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{}
|
||||
}
|
5
go.mod
5
go.mod
|
@ -2,7 +2,10 @@ module git.nakama.town/fmartingr/gotoolkit
|
|||
|
||||
go 1.23.3
|
||||
|
||||
require modernc.org/sqlite v1.34.1
|
||||
require (
|
||||
github.com/pelletier/go-toml/v2 v2.2.3
|
||||
modernc.org/sqlite v1.34.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
|
|
8
go.sum
8
go.sum
|
@ -1,3 +1,5 @@
|
|||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||
github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd h1:gbpYu9NMq8jhDVbvlGkMFWCjLFlqqEZjEmObmhUy6Vo=
|
||||
|
@ -10,10 +12,14 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
|
|||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
|
||||
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
|
||||
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
|
||||
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic=
|
||||
golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
|
@ -21,6 +27,8 @@ golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
|
|||
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/tools v0.19.0 h1:tfGCXNR1OsFG+sVdLAitlpjAvD/I6dHDKnYrpEZUHkw=
|
||||
golang.org/x/tools v0.19.0/go.mod h1:qoJWxmGSIBmAeriMx19ogtrEPrGtDbPK634QFIcLAhc=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
modernc.org/cc/v4 v4.21.4 h1:3Be/Rdo1fpr8GrQ7IVw9OHtplU4gWbb+wNgeoBMmGLQ=
|
||||
modernc.org/cc/v4 v4.21.4/go.mod h1:HM7VJTZbUCR3rV8EYBi9wxnJ0ZBRiGE5OeGXNA0IsLQ=
|
||||
modernc.org/ccgo/v4 v4.19.2 h1:lwQZgvboKD0jBwdaeVCTouxhxAyN6iawF3STraAal8Y=
|
||||
|
|
17
model/encoding.go
Normal file
17
model/encoding.go
Normal file
|
@ -0,0 +1,17 @@
|
|||
package model
|
||||
|
||||
import "io"
|
||||
|
||||
type Decoder interface {
|
||||
Decode([]byte, any) error
|
||||
DecodeReader(io.Reader, any) error
|
||||
}
|
||||
|
||||
type Encoder interface {
|
||||
Encode(any) ([]byte, error)
|
||||
}
|
||||
|
||||
type EncoderDecoder interface {
|
||||
Encoder
|
||||
Decoder
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue