Skip to content

Commit a09d32b

Browse files
authored
feat: support for cloning PlutusData (#133)
Fixes #132 Signed-off-by: Aurora Gaffney <[email protected]>
1 parent c31a8be commit a09d32b

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

data/data.go

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (p *PlutusDataWrapper) MarshalCBOR() ([]byte, error) {
2828

2929
type PlutusData interface {
3030
isPlutusData()
31-
31+
Clone() PlutusData
3232
fmt.Stringer
3333
}
3434

@@ -42,6 +42,19 @@ type Constr struct {
4242

4343
func (Constr) isPlutusData() {}
4444

45+
func (c Constr) Clone() PlutusData {
46+
tmpFields := make([]PlutusData, len(c.Fields))
47+
for i, field := range c.Fields {
48+
tmpFields[i] = field.Clone()
49+
}
50+
var tmpIndef *bool
51+
if c.useIndef != nil {
52+
tmpIndef = new(bool)
53+
*tmpIndef = *c.useIndef
54+
}
55+
return &Constr{Tag: c.Tag, Fields: tmpFields, useIndef: tmpIndef}
56+
}
57+
4558
func (c Constr) String() string {
4659
return fmt.Sprintf("Constr{tag: %d, fields: %v}", c.Tag, c.Fields)
4760
}
@@ -69,6 +82,22 @@ type Map struct {
6982

7083
func (Map) isPlutusData() {}
7184

85+
func (m Map) Clone() PlutusData {
86+
tmpPairs := make([][2]PlutusData, len(m.Pairs))
87+
for i, pair := range m.Pairs {
88+
tmpPairs[i] = [2]PlutusData{
89+
pair[0].Clone(),
90+
pair[1].Clone(),
91+
}
92+
}
93+
var tmpIndef *bool
94+
if m.useIndef != nil {
95+
tmpIndef = new(bool)
96+
*tmpIndef = *m.useIndef
97+
}
98+
return &Map{Pairs: tmpPairs, useIndef: tmpIndef}
99+
}
100+
72101
func (m Map) String() string {
73102
return fmt.Sprintf("Map%v", m.Pairs)
74103
}
@@ -95,6 +124,11 @@ type Integer struct {
95124

96125
func (Integer) isPlutusData() {}
97126

127+
func (i Integer) Clone() PlutusData {
128+
tmpVal := new(big.Int).Set(i.Inner)
129+
return &Integer{tmpVal}
130+
}
131+
98132
func (i Integer) String() string {
99133
return fmt.Sprintf("Integer(%s)", i.Inner.String())
100134
}
@@ -113,6 +147,12 @@ type ByteString struct {
113147

114148
func (ByteString) isPlutusData() {}
115149

150+
func (b ByteString) Clone() PlutusData {
151+
tmpVal := make([]byte, len(b.Inner))
152+
copy(tmpVal, b.Inner)
153+
return &ByteString{tmpVal}
154+
}
155+
116156
func (b ByteString) String() string {
117157
return fmt.Sprintf("ByteString(%x)", b.Inner)
118158
}
@@ -133,6 +173,19 @@ type List struct {
133173

134174
func (List) isPlutusData() {}
135175

176+
func (l List) Clone() PlutusData {
177+
tmpItems := make([]PlutusData, len(l.Items))
178+
for i, item := range l.Items {
179+
tmpItems[i] = item.Clone()
180+
}
181+
var tmpIndef *bool
182+
if l.useIndef != nil {
183+
tmpIndef = new(bool)
184+
*tmpIndef = *l.useIndef
185+
}
186+
return &List{Items: tmpItems, useIndef: tmpIndef}
187+
}
188+
136189
func (l List) String() string {
137190
return fmt.Sprintf("List%v", l.Items)
138191
}

0 commit comments

Comments
 (0)