-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindexable.go
More file actions
59 lines (48 loc) · 1.13 KB
/
indexable.go
File metadata and controls
59 lines (48 loc) · 1.13 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
package core
import (
"reflect"
)
type Indexable interface {
Index(int) any
Len() int
Append(any) error
AppendValue(reflect.Value) error
GetType() reflect.Type
GetData() reflect.Value
}
type IndexableImpl struct {
Data reflect.Value
}
func (i *IndexableImpl) Index(index int) any {
return i.Data.Index(index).Interface()
}
func (i *IndexableImpl) Len() int {
return i.Data.Len()
}
func (i *IndexableImpl) AppendValue(value reflect.Value) error {
if i.Data.Kind() == reflect.Slice {
s, err := ConvertValue(nil, value, i.Data.Type().Elem())
if err != nil {
return err
}
i.Data = reflect.Append(i.Data, reflect.ValueOf(s))
} else if i.Data.Kind() == reflect.String {
s, err := ConvertToString(nil, value)
if err != nil {
return err
}
i.Data = reflect.ValueOf(i.Data.String() + s)
} else {
return CreateErr(nil, nil, "cannot append to type %v", i.Data.Kind())
}
return nil
}
func (i *IndexableImpl) Append(value any) error {
return i.AppendValue(reflect.ValueOf(value))
}
func (i *IndexableImpl) GetType() reflect.Type {
return i.Data.Type()
}
func (i *IndexableImpl) GetData() reflect.Value {
return i.Data
}