-
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.
- Loading branch information
Showing
4 changed files
with
55 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 InvalidUnmarshalError. | ||
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,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
|
||
} | ||
|
||
func Test_DefaultCBORDecoder(t *testing.T) { | ||
t.Parallel() | ||
|
||
var ( | ||
ss sampleStructure | ||
importantString, _ = hex.DecodeString("a170696d706f7274616e745f737472696e676b48656c6c6f20576f726c64") | ||
Check failure on line 33 in cbor_test.go
|
||
cborDecoder CBORUnmarshal = cbor.Unmarshal | ||
) | ||
|
||
err := cborDecoder(importantString, &ss) | ||
require.NoError(t, err) | ||
require.Equal(t, "Hello World", ss.ImportantString) | ||
} |
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