-
Notifications
You must be signed in to change notification settings - Fork 0
/
datainput.go
127 lines (106 loc) · 2.57 KB
/
datainput.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package ergo
/*
#include "ergo.h"
*/
import "C"
import (
"iter"
"runtime"
)
// DataInput represent inputs that are used to enrich script context, but won't be spent by the transaction
type DataInput interface {
// BoxId returns the BoxId of the DataInput
BoxId() BoxId
pointer() C.DataInputPtr
}
type dataInput struct {
p C.DataInputPtr
}
func newDataInput(d *dataInput) DataInput {
runtime.SetFinalizer(d, finalizeDataInput)
return d
}
// NewDataInput create DataInput from BoxId
func NewDataInput(boxId BoxId) DataInput {
var p C.DataInputPtr
C.ergo_lib_data_input_new(boxId.pointer(), &p)
d := &dataInput{p: p}
return newDataInput(d)
}
func (d *dataInput) BoxId() BoxId {
var p C.BoxIdPtr
C.ergo_lib_data_input_box_id(d.p, &p)
bi := &boxId{p: p}
return newBoxId(bi)
}
func (d *dataInput) pointer() C.DataInputPtr {
return d.p
}
func finalizeDataInput(d *dataInput) {
C.ergo_lib_data_input_delete(d.p)
}
// DataInputs an ordered collection if DataInput
type DataInputs interface {
// Len returns the length of the collection
Len() int
// Get returns the Input at the provided index if it exists
Get(index int) (DataInput, error)
// Add adds provided DataInput to the end of the collection
Add(dataInput DataInput)
// All returns an iterator over all DataInput inside the collection
All() iter.Seq2[int, DataInput]
pointer() C.DataInputsPtr
}
type dataInputs struct {
p C.DataInputsPtr
}
func newDataInputs(d *dataInputs) DataInputs {
runtime.SetFinalizer(d, finalizeDataInputs)
return d
}
// NewDataInputs creates an empty DataInputs collection
func NewDataInputs() DataInputs {
var p C.DataInputsPtr
C.ergo_lib_data_inputs_new(&p)
d := &dataInputs{p: p}
return newDataInputs(d)
}
func (d *dataInputs) Len() int {
res := C.ergo_lib_data_inputs_len(d.p)
return int(res)
}
func (d *dataInputs) Get(index int) (DataInput, error) {
var p C.DataInputPtr
res := C.ergo_lib_data_inputs_get(d.p, C.uintptr_t(index), &p)
err := newError(res.error)
if err.isError() {
return nil, err.error()
}
if res.is_some {
di := &dataInput{p: p}
return newDataInput(di), nil
}
return nil, nil
}
func (d *dataInputs) Add(dataInput DataInput) {
C.ergo_lib_data_inputs_add(dataInput.pointer(), d.p)
}
func (d *dataInputs) All() iter.Seq2[int, DataInput] {
return func(yield func(int, DataInput) bool) {
for i := 0; i < d.Len(); i++ {
tk, err := d.Get(i)
if err != nil {
return
}
if !yield(i, tk) {
return
}
}
}
}
func (d *dataInputs) pointer() C.DataInputsPtr {
return d.p
}
func finalizeDataInputs(d *dataInputs) {
C.ergo_lib_data_inputs_delete(d.p)
}