-
Notifications
You must be signed in to change notification settings - Fork 0
/
tuple.go
51 lines (40 loc) · 939 Bytes
/
tuple.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package scale_codec
import (
"fmt"
"io"
)
type FieldAccess interface {
Field(int) Encodable
}
type Tuple struct {
Items []Encodable
}
func NewTuple(items ...Encodable) *Tuple {
return &Tuple{items}
}
func (t Tuple) MarshalSCALE() ([]byte, error) {
encodedTuple := make([]byte, 0)
for idx, item := range t.Items {
encodedItem, err := item.MarshalSCALE()
if err != nil {
return nil, fmt.Errorf("encoding item at index %v: %w", idx, err)
}
encodedTuple = append(encodedTuple, encodedItem...)
}
return encodedTuple, nil
}
func (t *Tuple) UnmarshalSCALE(reader io.Reader) error {
for idx, item := range t.Items {
err := item.UnmarshalSCALE(reader)
if err != nil {
return fmt.Errorf("encoding item at index %v: %w", idx, err)
}
}
return nil
}
func (t *Tuple) FieldAccess(at int) Encodable {
if at > len(t.Items)-1 {
panic(fmt.Sprintf("cannot access tuple item at: %d", at))
}
return t.Items[at]
}