-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.go
181 lines (139 loc) · 3.71 KB
/
list.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
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
package python
import (
"reflect"
cpy3 "go.nhat.io/cpy/v3"
)
// IsList returns true if the object is a tuple.
func IsList(o PyObjector) bool {
return o.PyObject().Type() == cpy3.List
}
// ListObject is a generic Python tuple.
type ListObject cpy3.PyObject
// DecRef decreases the reference count of the object.
func (o *ListObject) DecRef() {
if o == nil {
return
}
(*cpy3.PyObject)(o).DecRef()
}
// PyObject returns the underlying PyObject.
func (o *ListObject) PyObject() *cpy3.PyObject {
return (*cpy3.PyObject)(o)
}
// Length returns the length of the tuple.
func (o *ListObject) Length() int {
return (*cpy3.PyObject)(o).Length()
}
// Set sets the item at index to value.
func (o *ListObject) Set(index int, value any) {
defer MustSuccess()
cpy3.PyList_SetItem((*cpy3.PyObject)(o), index, toPyObject(value))
}
// Get returns the item at index.
func (o *ListObject) Get(index int) *Object {
item := cpy3.PyList_GetItem((*cpy3.PyObject)(o), index)
MustSuccess()
return NewObject(item)
}
// AsObject returns the tuple as Object.
func (o *ListObject) AsObject() *Object {
return (*Object)(o)
}
// AsTuple converts a list to a tuple.
func (o *ListObject) AsTuple() *TupleObject {
return (*TupleObject)(cpy3.PyList_AsTuple((*cpy3.PyObject)(o)))
}
// String returns the string representation of the object.
func (o *ListObject) String() string {
return asString((*cpy3.PyObject)(o))
}
// NewListObject creates a new tuple.
func NewListObject(length int) *ListObject {
return (*ListObject)(cpy3.PyList_New(length))
}
// List is a generic Python tuple.
type List[T any] struct {
obj *ListObject
}
// UnmarshalPyObject unmarshals a Python object to a list.
func (l *List[T]) UnmarshalPyObject(o *Object) error {
if !IsList(o) && !IsTuple(o) {
return &UnmarshalTypeError{Value: TypeName(o), Type: reflect.TypeOf(l)}
}
if IsTuple(o) {
o = (*Object)((*TupleObject)(o).AsList())
}
l.obj = (*ListObject)(o)
return nil
}
// DecRef decreases the reference count of the object.
func (l *List[T]) DecRef() {
if l == nil {
return
}
l.obj.DecRef()
}
// PyObject returns the underlying PyObject.
func (l *List[T]) PyObject() *cpy3.PyObject {
return l.obj.PyObject()
}
// Length returns the length of the tuple.
func (l *List[T]) Length() int {
return l.obj.Length()
}
// Set sets the item at index to value.
func (l *List[T]) Set(index int, value T) {
l.obj.Set(index, value)
}
// Get returns the item at index.
func (l *List[T]) Get(index int) T {
o := l.obj.Get(index)
return MustUnmarshalAs[T](o)
}
// AsObject returns the tuple as Object.
func (l *List[T]) AsObject() *Object {
return l.obj.AsObject()
}
// AsTuple converts a list to a tuple.
func (l *List[T]) AsTuple() *Tuple[T] {
return &Tuple[T]{
obj: l.obj.AsTuple(),
}
}
// AsSlice converts a tuple to a slice.
func (l *List[T]) AsSlice() []T {
length := l.Length()
slice := make([]T, length)
for i := range length {
slice[i] = l.Get(i)
}
return slice
}
// String returns the string representation of the object.
func (l *List[T]) String() string {
return l.obj.String()
}
// AnyList is a Python tuple.
type AnyList = List[any]
// NewList creates a new tuple.
func NewList(length int) *AnyList {
return NewListForType[any](length)
}
// NewListForType creates a new tuple for a given type.
func NewListForType[T any](length int) *List[T] {
return &List[T]{
obj: NewListObject(length),
}
}
// NewListFromValues converts a slice of any data type to a tuple.
func NewListFromValues[T any](values ...T) *List[T] {
tuple := NewListForType[T](len(values))
for i, v := range values {
tuple.Set(i, v)
}
return tuple
}
// NewListFromAny converts a slice of any to a tuple.
func NewListFromAny(values ...any) *AnyList {
return NewListFromValues(values...)
}