From 72c4431cc3849299909e6733b5ec0c4dd1c06a88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Wei=C3=9Fe?= Date: Thu, 18 Jan 2024 15:19:04 +0100 Subject: [PATCH] Add back byte slice decoding fallback MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Daniel Weiße --- internal/encoding/encoding.go | 8 +++++++- internal/encoding/encoding_test.go | 10 ++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/internal/encoding/encoding.go b/internal/encoding/encoding.go index 8c173f51b9..fba7f33dad 100644 --- a/internal/encoding/encoding.go +++ b/internal/encoding/encoding.go @@ -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) } diff --git a/internal/encoding/encoding_test.go b/internal/encoding/encoding_test.go index 3e14914ebb..0c6e4a1304 100644 --- a/internal/encoding/encoding_test.go +++ b/internal/encoding/encoding_test.go @@ -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 {