-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtuple.go
More file actions
187 lines (143 loc) · 3.8 KB
/
tuple.go
File metadata and controls
187 lines (143 loc) · 3.8 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package python
import (
"reflect"
cpy3 "go.nhat.io/cpy/v3"
)
// IsTuple returns true if the object is a tuple.
func IsTuple(o PyObjector) bool {
return o.PyObject().Type() == cpy3.Tuple
}
// TupleObject is a generic Python tuple.
type TupleObject cpy3.PyObject
// DecRef decreases the reference count of the object.
func (o *TupleObject) DecRef() {
if o == nil {
return
}
(*cpy3.PyObject)(o).DecRef()
}
// PyObject returns the underlying PyObject.
func (o *TupleObject) PyObject() *cpy3.PyObject {
return (*cpy3.PyObject)(o)
}
// Length returns the length of the tuple.
func (o *TupleObject) Length() int {
return (*cpy3.PyObject)(o).Length()
}
// Set sets the item at index to value.
func (o *TupleObject) Set(index int, value any) {
defer MustSuccess()
cpy3.PyTuple_SetItem((*cpy3.PyObject)(o), index, toPyObject(value))
}
// Get returns the item at index.
func (o *TupleObject) Get(index int) *Object {
item := cpy3.PyTuple_GetItem((*cpy3.PyObject)(o), index)
MustSuccess()
return NewObject(item)
}
// AsObject returns the tuple as Object.
func (o *TupleObject) AsObject() *Object {
return (*Object)(o)
}
// AsList converts a tuple to a list.
func (o *TupleObject) AsList() *ListObject {
l := (*ListObject)(cpy3.PyList_New(o.Length()))
for i := range o.Length() {
l.Set(i, o.Get(i))
}
return l
}
// String returns the string representation of the object.
func (o *TupleObject) String() string {
return asString((*cpy3.PyObject)(o))
}
// NewTupleObject creates a new tuple.
func NewTupleObject(length int) *TupleObject {
return (*TupleObject)(cpy3.PyTuple_New(length))
}
// Tuple is a generic Python tuple.
type Tuple[T any] struct {
obj *TupleObject
}
// UnmarshalPyObject unmarshals a Python object to the tuple.
func (t *Tuple[T]) UnmarshalPyObject(o *Object) error {
if !IsList(o) && !IsTuple(o) {
return &UnmarshalTypeError{Value: TypeName(o), Type: reflect.TypeOf(t)}
}
if IsList(o) {
o = (*Object)((*ListObject)(o).AsTuple())
}
t.obj = (*TupleObject)(o)
return nil
}
// DecRef decreases the reference count of the object.
func (t *Tuple[T]) DecRef() {
if t == nil {
return
}
t.obj.DecRef()
}
// PyObject returns the underlying PyObject.
func (t *Tuple[T]) PyObject() *cpy3.PyObject {
return t.obj.PyObject()
}
// Length returns the length of the tuple.
func (t *Tuple[T]) Length() int {
return t.obj.Length()
}
// Set sets the item at index to value.
func (t *Tuple[T]) Set(index int, value T) {
t.obj.Set(index, value)
}
// Get returns the item at index.
func (t *Tuple[T]) Get(index int) T {
o := t.obj.Get(index)
return MustUnmarshalAs[T](o)
}
// AsObject returns the tuple as Object.
func (t *Tuple[T]) AsObject() *Object {
return t.obj.AsObject()
}
// AsList converts a tuple to a list.
func (t *Tuple[T]) AsList() *List[T] {
return &List[T]{
obj: t.obj.AsList(),
}
}
// AsSlice converts a tuple to a slice.
func (t *Tuple[T]) AsSlice() []T {
length := t.Length()
slice := make([]T, length)
for i := range length {
slice[i] = t.Get(i)
}
return slice
}
// String returns the string representation of the object.
func (t *Tuple[T]) String() string {
return t.obj.String()
}
// AnyTuple is a Python tuple.
type AnyTuple = Tuple[any]
// NewTuple creates a new tuple.
func NewTuple(length int) *AnyTuple {
return NewTupleForType[any](length)
}
// NewTupleForType creates a new tuple for a given type.
func NewTupleForType[T any](length int) *Tuple[T] {
return &Tuple[T]{
obj: NewTupleObject(length),
}
}
// NewTupleFromValues converts a slice of any data type to a tuple.
func NewTupleFromValues[T any](values ...T) *Tuple[T] {
tuple := NewTupleForType[T](len(values))
for i, v := range values {
tuple.Set(i, v)
}
return tuple
}
// NewTupleFromAny converts a slice of any to a tuple.
func NewTupleFromAny(values ...any) *AnyTuple {
return NewTupleFromValues(values...)
}