-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from imsk17/feat/cbor-encoding
feat(cbor): Add support for CBOR encoding
- Loading branch information
Showing
4 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters