From e6222bb6fce150cf2625155cd1af8031f2f11c61 Mon Sep 17 00:00:00 2001 From: imsk17 Date: Sat, 5 Oct 2024 23:47:52 +0530 Subject: [PATCH 1/5] feat(cbor): add cbor --- cbor.go | 9 +++++++++ cbor_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ go.mod | 2 ++ go.sum | 4 ++++ 4 files changed, 55 insertions(+) create mode 100644 cbor.go create mode 100644 cbor_test.go diff --git a/cbor.go b/cbor.go new file mode 100644 index 0000000..29e5e7d --- /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 InvalidUnmarshalError. +type CBORUnmarshal func(data []byte, v any) error diff --git a/cbor_test.go b/cbor_test.go new file mode 100644 index 0000000..0f06e46 --- /dev/null +++ b/cbor_test.go @@ -0,0 +1,40 @@ +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([]byte(raw)), importantString) +} + +func Test_DefaultCBORDecoder(t *testing.T) { + t.Parallel() + + var ( + ss sampleStructure + importantString, _ = hex.DecodeString("a170696d706f7274616e745f737472696e676b48656c6c6f20576f726c64") + cborDecoder CBORUnmarshal = cbor.Unmarshal + ) + + err := cborDecoder(importantString, &ss) + require.NoError(t, err) + require.Equal(t, "Hello World", ss.ImportantString) +} diff --git a/go.mod b/go.mod index 237aa95..dabeab4 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/gofiber/utils/v2 go 1.21 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= From 85e3dd9834de7fc276bd26eb3c4ef82e6306a647 Mon Sep 17 00:00:00 2001 From: imsk17 Date: Sat, 12 Oct 2024 23:39:46 +0530 Subject: [PATCH 2/5] chore(lint): fix lint issues --- cbor_test.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/cbor_test.go b/cbor_test.go index 0f06e46..193203e 100644 --- a/cbor_test.go +++ b/cbor_test.go @@ -22,19 +22,22 @@ func Test_DefaultCBOREncoder(t *testing.T) { raw, err := cborEncoder(ss) require.NoError(t, err) - require.Equal(t, hex.EncodeToString([]byte(raw)), importantString) + require.Equal(t, hex.EncodeToString(raw), importantString) } func Test_DefaultCBORDecoder(t *testing.T) { t.Parallel() var ( - ss sampleStructure - importantString, _ = hex.DecodeString("a170696d706f7274616e745f737472696e676b48656c6c6f20576f726c64") - cborDecoder CBORUnmarshal = cbor.Unmarshal + 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) + err = cborDecoder(importantString, &ss) require.NoError(t, err) require.Equal(t, "Hello World", ss.ImportantString) } From 75f24a9a18ba5412c7e485db8ed9c01cda3f44a5 Mon Sep 17 00:00:00 2001 From: imsk17 Date: Sun, 13 Oct 2024 20:22:27 +0530 Subject: [PATCH 3/5] fix(doc): CBORUnmarshal can throw different types of error --- cbor.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cbor.go b/cbor.go index 29e5e7d..abd506c 100644 --- a/cbor.go +++ b/cbor.go @@ -5,5 +5,5 @@ 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 InvalidUnmarshalError. +// Unmarshal returns an error. type CBORUnmarshal func(data []byte, v any) error From ee26b4f62373b222e43dd20a54fff91b85d90d7b Mon Sep 17 00:00:00 2001 From: imsk17 Date: Sun, 20 Oct 2024 01:42:39 +0530 Subject: [PATCH 4/5] chore(cbor): add more tests --- cbor_test.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/cbor_test.go b/cbor_test.go index 193203e..7c8bdac 100644 --- a/cbor_test.go +++ b/cbor_test.go @@ -41,3 +41,69 @@ func Test_DefaultCBORDecoder(t *testing.T) { 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 + 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, ss) +} From edcc05661337b7669ff9c0d7abce31e471e372de Mon Sep 17 00:00:00 2001 From: imsk17 Date: Sun, 20 Oct 2024 02:12:39 +0530 Subject: [PATCH 5/5] chore(lint): fix lint issues --- cbor_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cbor_test.go b/cbor_test.go index 7c8bdac..e9fc7b4 100644 --- a/cbor_test.go +++ b/cbor_test.go @@ -96,6 +96,7 @@ func Test_DefaultCBORDecoderWithUnitializedStruct(t *testing.T) { var ( ss sampleStructure + emptySs sampleStructure importantString, err = hex.DecodeString("a170696d706f7274616e745f737472696e6760") cborDecoder CBORUnmarshal = cbor.Unmarshal ) @@ -105,5 +106,5 @@ func Test_DefaultCBORDecoderWithUnitializedStruct(t *testing.T) { err = cborDecoder(importantString, &ss) require.NoError(t, err) - require.Equal(t, ss, ss) + require.Equal(t, emptySs, ss) }