Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cbor): Add support for CBOR encoding #94

Merged
merged 5 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions cbor.go
Original file line number Diff line number Diff line change
@@ -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
110 changes: 110 additions & 0 deletions cbor_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
gaby marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ 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
)

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
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
Loading