-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathignite_types.go
More file actions
177 lines (142 loc) · 3.66 KB
/
ignite_types.go
File metadata and controls
177 lines (142 loc) · 3.66 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
package ignite
import (
"fmt"
"time"
)
type Time int64
type Date int64
func NewTime(val time.Time) Time {
utcVal := val.UTC()
return Time(utcVal.Unix()*1000 + int64(utcVal.Nanosecond())/int64(time.Millisecond))
}
func (t Time) Time() time.Time {
return time.Unix(int64(t)/1000, (int64(t)%1000)*int64(time.Millisecond))
}
func (t Time) String() string {
return t.Time().Format("15:04:05.000")
}
func NewDate(val time.Time) Date {
utcVal := val.UTC()
return Date(utcVal.Unix()*1000 + int64(utcVal.Nanosecond())/int64(time.Millisecond))
}
func (d Date) Time() time.Time {
return time.Unix(int64(d)/1000, (int64(d)%1000)*int64(time.Millisecond))
}
func (d Date) String() string {
return d.Time().String()
}
type CollectionKind = int8
const (
UserSet CollectionKind = iota - 1
UserCollection
ArrayList
LinkedList
HashSet
LinkedHashSet
SingletonList
HashMap CollectionKind = 1
LinkedHashMap CollectionKind = 2
)
type Collection struct {
kind CollectionKind
values []interface{}
}
func (col *Collection) Kind() CollectionKind {
return col.kind
}
func (col *Collection) Values() []interface{} {
return col.values
}
func (col *Collection) Size() int {
return len(col.values)
}
func NewUserCollection[T any](values ...T) Collection {
return newIgniteCollection(UserCollection, values...)
}
func NewArrayList[T any](values ...T) Collection {
return newIgniteCollection(ArrayList, values...)
}
func NewLinkedList[T any](values ...T) Collection {
return newIgniteCollection(LinkedList, values...)
}
func NewSingletonList[T any](value T) Collection {
return newIgniteCollection(SingletonList, value)
}
func NewHashSet[T any](values ...T) Collection {
return newIgniteCollection(HashSet, values...)
}
func NewLinkedHashSet[T any](values ...T) Collection {
return newIgniteCollection(LinkedHashSet, values...)
}
func newIgniteCollection[T any](kind CollectionKind, values ...T) Collection {
values0 := make([]interface{}, 0, len(values))
for _, value := range values {
values0 = append(values0, value)
}
return Collection{
kind: kind,
values: values0,
}
}
type Map struct {
kind CollectionKind
entries []KeyValue
}
func (m *Map) Kind() CollectionKind {
return m.kind
}
func (m *Map) Entries() []KeyValue {
return m.entries
}
func (m *Map) Size() int {
return len(m.entries)
}
func NewUserMap(entries ...KeyValue) Map {
return newIgniteMap(UserCollection, entries...)
}
func ToUserMap[K comparable, V any](m map[K]V) Map {
return toIgniteMap(UserCollection, m)
}
func NewHashMap(entries ...KeyValue) Map {
return newIgniteMap(HashMap, entries...)
}
func ToHashMap[K comparable, V any](m map[K]V) Map {
return toIgniteMap(HashMap, m)
}
func NewLinkedHashMap(entries ...KeyValue) Map {
return newIgniteMap(LinkedHashMap, entries...)
}
func ToLinkedHashMap[K comparable, V any](m map[K]V) Map {
return toIgniteMap(LinkedHashMap, m)
}
func ToMap[K comparable, V any](igniteMap Map) (map[K]V, error) {
ret := make(map[K]V, len(igniteMap.entries))
for _, entry := range igniteMap.entries {
var key K
var val V
var ok bool
if key, ok = entry.Key.(K); !ok {
return nil, fmt.Errorf("invalid key type: %T", entry.Key)
}
if entry.Value != nil {
if val, ok = entry.Value.(V); !ok {
return nil, fmt.Errorf("invalid value type: %T", entry.Value)
}
}
ret[key] = val
}
return ret, nil
}
func toIgniteMap[K comparable, V any](kind CollectionKind, m map[K]V) Map {
entries := make([]KeyValue, 0, len(m))
for k, v := range m {
entries = append(entries, KeyValue{Key: k, Value: v})
}
return newIgniteMap(kind, entries...)
}
func newIgniteMap(kind CollectionKind, entries ...KeyValue) Map {
return Map{
kind: kind,
entries: entries,
}
}