Skip to content

Commit

Permalink
Add back byte slice decoding fallback
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Weiße <[email protected]>
  • Loading branch information
daniel-weisse committed Jan 18, 2024
1 parent a28c239 commit 72c4431
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
8 changes: 7 additions & 1 deletion internal/encoding/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ func (h HexBytes) MarshalJSON() ([]byte, error) {
func (h *HexBytes) UnmarshalYAML(unmarshal func(any) error) error {
var hexString string
if err := unmarshal(&hexString); err != nil {
return err
// compatibility mode for old state file format:
// fall back to unmarshalling as a byte slice for backwards compatibility
var oldHexBytes []byte
if err := unmarshal(&oldHexBytes); err != nil {
return fmt.Errorf("unmarshalling hex bytes: %w", err)
}
hexString = hex.EncodeToString(oldHexBytes)
}
return h.unmarshal(hexString)
}
Expand Down
10 changes: 10 additions & 0 deletions internal/encoding/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ func TestUnmarshalHexBytes(t *testing.T) {
jsonString: "\"\"",
expected: nil,
},
"byte slice compat": {
yamlString: "[0xab, 0xcd, 0xef]",
jsonString: "\"abcdef\"", // no backwards compatibility since we never used this format for json
expected: []byte{0xab, 0xcd, 0xef},
},
"byte slice compat 2": {
yamlString: "[00, 12, 34]",
jsonString: "\"000c22\"", // no backwards compatibility since we never used this format for json
expected: []byte{0x00, 0x0c, 0x22},
},
}

for name, tc := range testCases {
Expand Down

0 comments on commit 72c4431

Please sign in to comment.