Skip to content

Commit 5c90121

Browse files
authored
fix: decode map with duplicate keys (#162)
Signed-off-by: Aurora Gaffney <[email protected]>
1 parent c956eba commit 5c90121

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

data/data_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,31 @@ var testDefs = []struct {
224224
// {_ h'01': 1, h'02': 2}
225225
CborHex: "bf410101410202ff",
226226
},
227+
// Map with duplicate keys
228+
{
229+
Data: NewMapDefIndef(
230+
false,
231+
[][2]PlutusData{
232+
{
233+
NewByteString([]byte("6Key")),
234+
NewByteString([]byte("1")),
235+
},
236+
{
237+
NewByteString([]byte("5Key")),
238+
NewByteString([]byte("7")),
239+
},
240+
{
241+
NewByteString([]byte("6Key")),
242+
NewByteString(nil),
243+
},
244+
{
245+
NewByteString([]byte("5Key")),
246+
NewByteString(nil),
247+
},
248+
},
249+
),
250+
CborHex: "a444364b6579413144354b6579413744364b65794044354b657940",
251+
},
227252
}
228253

229254
func TestPlutusDataEncode(t *testing.T) {

data/decode.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,10 @@ func decodeCborRawMap(data []byte) (any, error) {
105105
// The below is a hack to work around our CBOR library not supporting preserving key
106106
// order when decoding a map. We decode our map to determine its length, create a dummy
107107
// list the same length as our map to determine the header size, and then decode each
108-
// key/value pair individually
108+
// key/value pair individually. We use a pointer for the key to keep duplicates to get
109+
// an accurate count for decoding
109110
useIndef := (data[0] & CborIndefFlag) == CborIndefFlag
110-
var tmpData map[RawMessageStr]RawMessageStr
111+
var tmpData map[*RawMessageStr]RawMessageStr
111112
if err := cborUnmarshal(data, &tmpData); err != nil {
112113
return nil, err
113114
}

0 commit comments

Comments
 (0)