Skip to content

Commit 668ed5a

Browse files
authored
refactor: merge IndefList into List (#102)
The data.List type will generate a definite-length or indefinite-length list depending on the number of items in the list Signed-off-by: Aurora Gaffney <[email protected]>
1 parent 56516b8 commit 668ed5a

File tree

3 files changed

+12
-48
lines changed

3 files changed

+12
-48
lines changed

data/data.go

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -121,24 +121,8 @@ func (l List) String() string {
121121

122122
// NewList creates a new List variant.
123123
func NewList(items ...PlutusData) PlutusData {
124-
return &List{items}
125-
}
126-
127-
// IndefList
128-
129-
type IndefList struct {
130-
Items []PlutusData
131-
}
132-
133-
func (IndefList) isPlutusData() {}
134-
135-
func (l IndefList) String() string {
136-
return fmt.Sprintf("IndefList%v", l.Items)
137-
}
138-
139-
// NewIndefList creates a new IndefList
140-
func NewIndefList(items ...PlutusData) PlutusData {
141-
return &IndefList{
142-
Items: items,
124+
if items == nil {
125+
items = make([]PlutusData, 0)
143126
}
127+
return &List{items}
144128
}

data/data_test.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ var testDefs = []struct {
2424
NewInteger(big.NewInt(123)),
2525
NewInteger(big.NewInt(456)),
2626
),
27-
CborHex: "82187b1901c8",
27+
CborHex: "9f187b1901c8ff",
28+
},
29+
{
30+
Data: NewList(),
31+
CborHex: "80",
2832
},
2933
{
3034
Data: NewConstr(
@@ -42,19 +46,8 @@ var testDefs = []struct {
4246
NewInteger(big.NewInt(1)),
4347
NewInteger(big.NewInt(2)),
4448
),
45-
CborHex: "820102",
49+
CborHex: "9f0102ff",
4650
},
47-
// TODO: figure out how to not fail this
48-
// It works fine for encode, but we don't have a good way to capture an indef-length list on decode
49-
/*
50-
{
51-
Data: NewIndefList(
52-
NewInteger(big.NewInt(1)),
53-
NewInteger(big.NewInt(2)),
54-
),
55-
CborHex: "9f0102ff",
56-
},
57-
*/
5851
}
5952

6053
func TestPlutusDataEncode(t *testing.T) {

data/encode.go

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ func encodeToRaw(pd PlutusData) (any, error) {
3232
return encodeByteString(v)
3333
case *List:
3434
return encodeList(v)
35-
case *IndefList:
36-
return encodeIndefList(v)
3735
default:
3836
return nil, fmt.Errorf("unknown PlutusData type: %T", pd)
3937
}
@@ -158,21 +156,10 @@ func encodeByteString(bs *ByteString) (any, error) {
158156

159157
// encodeList encodes a List to CBOR array format.
160158
func encodeList(l *List) (any, error) {
161-
result := make([]any, len(l.Items))
162-
163-
for i, item := range l.Items {
164-
encoded, err := encodeToRaw(item)
165-
if err != nil {
166-
return nil, fmt.Errorf("failed to encode list item %d: %w", i, err)
167-
}
168-
result[i] = encoded
159+
if len(l.Items) == 0 {
160+
ret := make([]any, 0)
161+
return ret, nil
169162
}
170-
171-
return result, nil
172-
}
173-
174-
// encodeIndefList encodes an IndefList to CBOR format
175-
func encodeIndefList(l *IndefList) (any, error) {
176163
tmpData := []byte{
177164
// Start an indefinite-length list
178165
0x9F,

0 commit comments

Comments
 (0)