-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Decode/encode CBOR maps with int keys to Go struct
This simplifies decoding COSE to struct since COSE uses map with integer keys. Decoding to struct is 42.125% faster than to map[int]interface{} and 42.105% less B/op. This also benefits WebAuthn since it uses COSE. Closes: #15 Struct field name is treated as integer if it has "keyasint" option in its format string. The format string must specify an integer as its field name. ``` type T struct { F1 int `cbor:"1,keyasint"` F2 int `cbor:"2"` F3 int `cbor:"-1,keyasint"` } v := T{F1: 1, F2: 2, F3: 3} ``` Marshal encodes v as {1: 1, "2": 2, -1: 3}. Note that field F2 is encoded as "2", not 2, because "keyasint" is not specified. Benchmarks of decoding COSE to map[int]interface{} vs. struct: ``` BenchmarkDecodeCOSE/COSE_to_Go_map[int]interface_{}-2 1000000 1600 ns/op 456 B/op 13 allocs/op BenchmarkDecodeCOSE/COSE_to_Go_cbor_test.coseKey-2 2000000 926 ns/op 264 B/op 13 allocs/op ```
- Loading branch information
Showing
6 changed files
with
171 additions
and
31 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
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
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
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