Skip to content

Commit

Permalink
feat(cbor): add cbor
Browse files Browse the repository at this point in the history
  • Loading branch information
imsk17 committed Oct 5, 2024
1 parent cefa447 commit e6222bb
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
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 InvalidUnmarshalError.
type CBORUnmarshal func(data []byte, v any) error
40 changes: 40 additions & 0 deletions cbor_test.go
Original file line number Diff line number Diff line change
@@ -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)

Check failure on line 25 in cbor_test.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary conversion (unconvert)

Check failure on line 25 in cbor_test.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary conversion (unconvert)
}

func Test_DefaultCBORDecoder(t *testing.T) {
t.Parallel()

var (
ss sampleStructure
importantString, _ = hex.DecodeString("a170696d706f7274616e745f737472696e676b48656c6c6f20576f726c64")

Check failure on line 33 in cbor_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `hex.DecodeString` is not checked (errcheck)

Check failure on line 33 in cbor_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `hex.DecodeString` is not checked (errcheck)
cborDecoder CBORUnmarshal = cbor.Unmarshal
)

err := cborDecoder(importantString, &ss)
require.NoError(t, err)
require.Equal(t, "Hello World", ss.ImportantString)
}
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

0 comments on commit e6222bb

Please sign in to comment.