-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype.go
93 lines (76 loc) · 1.86 KB
/
type.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
package golist
import "github.com/emylincon/golist/core"
// List types
const (
TypeListInt ListType = "golist.List[]int"
TypeListInt32 ListType = "golist.List[]int32"
TypeListInt64 ListType = "golist.List[]int64"
TypeListFloat32 ListType = "golist.List[]float32"
TypeListFloat64 ListType = "golist.List[]float64"
TypeListString ListType = "golist.List[]string"
TypeListUnknown ListType = "golist.List[]unknown"
)
// ListType is a string representation of list type
type ListType string
// Type returns string representation of the list.
func (arr *List) Type() ListType {
switch arr.list.(type) {
case []int:
return TypeListInt
case []int32:
return TypeListInt32
case []int64:
return TypeListInt64
case []float32:
return TypeListFloat32
case []float64:
return TypeListFloat64
case []string:
return TypeListString
default:
return TypeListUnknown
}
}
// ConvertTo converts list to a new type
func (arr *List) ConvertTo(t ListType) (*List, error) {
switch t {
case TypeListInt:
list, err := core.ConvertToInt(arr.list)
if err != nil {
return &List{}, err
}
return NewList(list), nil
case TypeListInt32:
list, err := core.ConvertToInt32(arr.list)
if err != nil {
return &List{}, err
}
return NewList(list), nil
case TypeListInt64:
list, err := core.ConvertToInt64(arr.list)
if err != nil {
return &List{}, err
}
return NewList(list), nil
case TypeListFloat32:
list, err := core.ConvertToFloat32(arr.list)
if err != nil {
return &List{}, err
}
return NewList(list), nil
case TypeListFloat64:
list, err := core.ConvertToFloat64(arr.list)
if err != nil {
return &List{}, err
}
return NewList(list), nil
case TypeListString:
list, err := core.ConvertToString(arr.list)
if err != nil {
return &List{}, err
}
return NewList(list), nil
default:
return &List{}, ErrTypeNotsupported
}
}