diff --git a/cbor.go b/cbor.go new file mode 100644 index 0000000..abd506c --- /dev/null +++ b/cbor.go @@ -0,0 +1,9 @@ +package utils + +// CBORMarshal returns the CBOR encoding of v. +type CBORMarshal func(v any) ([]byte, error) + +// CBORUnmarshal parses the CBOR-encoded data and stores the result +// in the value pointed to by v. If v is nil or not a pointer, +// Unmarshal returns an error. +type CBORUnmarshal func(data []byte, v any) error diff --git a/cbor_test.go b/cbor_test.go new file mode 100644 index 0000000..e9fc7b4 --- /dev/null +++ b/cbor_test.go @@ -0,0 +1,110 @@ +package utils + +import ( + "encoding/hex" + "testing" + + "github.com/fxamacker/cbor/v2" + "github.com/stretchr/testify/require" +) + +func Test_DefaultCBOREncoder(t *testing.T) { + t.Parallel() + + var ( + ss = &sampleStructure{ + ImportantString: "Hello World", + } + importantString = `a170696d706f7274616e745f737472696e676b48656c6c6f20576f726c64` + cborEncoder CBORMarshal = cbor.Marshal + ) + + raw, err := cborEncoder(ss) + require.NoError(t, err) + + require.Equal(t, hex.EncodeToString(raw), importantString) +} + +func Test_DefaultCBORDecoder(t *testing.T) { + t.Parallel() + + var ( + ss sampleStructure + importantString, err = hex.DecodeString("a170696d706f7274616e745f737472696e676b48656c6c6f20576f726c64") + cborDecoder CBORUnmarshal = cbor.Unmarshal + ) + if err != nil { + t.Error("Failed to decode hex string") + } + + err = cborDecoder(importantString, &ss) + require.NoError(t, err) + require.Equal(t, "Hello World", ss.ImportantString) +} + +func Test_DefaultCBOREncoderWithEmptyString(t *testing.T) { + t.Parallel() + + var ( + ss = &sampleStructure{ + ImportantString: "", + } + importantString = `a170696d706f7274616e745f737472696e6760` + cborEncoder CBORMarshal = cbor.Marshal + ) + + raw, err := cborEncoder(ss) + require.NoError(t, err) + + require.Equal(t, hex.EncodeToString(raw), importantString) +} + +func Test_DefaultCBORDecoderWithEmptyString(t *testing.T) { + t.Parallel() + + var ( + ss sampleStructure + importantString, err = hex.DecodeString("a170696d706f7274616e745f737472696e6760") + cborDecoder CBORUnmarshal = cbor.Unmarshal + ) + if err != nil { + t.Error("Failed to decode hex string") + } + + err = cborDecoder(importantString, &ss) + require.NoError(t, err) + require.Equal(t, "", ss.ImportantString) +} + +func Test_DefaultCBOREncoderWithUnitializedStruct(t *testing.T) { + t.Parallel() + + var ( + ss sampleStructure + importantString = `a170696d706f7274616e745f737472696e6760` + cborEncoder CBORMarshal = cbor.Marshal + ) + + raw, err := cborEncoder(ss) + require.NoError(t, err) + + require.Equal(t, hex.EncodeToString(raw), importantString) +} + +func Test_DefaultCBORDecoderWithUnitializedStruct(t *testing.T) { + t.Parallel() + + var ( + ss sampleStructure + emptySs sampleStructure + importantString, err = hex.DecodeString("a170696d706f7274616e745f737472696e6760") + cborDecoder CBORUnmarshal = cbor.Unmarshal + ) + if err != nil { + t.Error("Failed to decode hex string") + } + + err = cborDecoder(importantString, &ss) + require.NoError(t, err) + require.Equal(t, emptySs, ss) +} diff --git a/go.mod b/go.mod index 8e34abd..203d5bb 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/gofiber/utils/v2 go 1.22 require ( + github.com/fxamacker/cbor/v2 v2.7.0 github.com/google/uuid v1.6.0 github.com/stretchr/testify v1.9.0 ) @@ -10,5 +11,6 @@ require ( require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/x448/float16 v0.8.4 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 5697539..c6886a5 100644 --- a/go.sum +++ b/go.sum @@ -1,11 +1,15 @@ 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/fxamacker/cbor/v2 v2.7.0 h1:iM5WgngdRBanHcxugY4JySA0nk1wZorNOpTgCMedv5E= +github.com/fxamacker/cbor/v2 v2.7.0/go.mod h1:pxXPTn3joSm21Gbwsv0w9OSA2y1HFR9qXEeXQVeNoDQ= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 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/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM= +github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=