-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstate.go
308 lines (270 loc) · 7.31 KB
/
state.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package structology
import (
"fmt"
"github.com/viant/xunsafe"
"reflect"
"unsafe"
)
type (
//StateType represents a state type
StateType struct {
isPtr bool
rType reflect.Type
selectors Selectors
marker *Marker
}
//State represents a state
State struct {
stateType *StateType
value interface{}
valuePtr interface{}
ptr unsafe.Pointer
}
)
// Lookup returns a selector
func (s *StateType) Lookup(name string) *Selector {
return s.selectors.Lookup(name)
}
func (s *StateType) RootSelectors() []*Selector {
return s.selectors.Root
}
// MatchByTag matches selector by tag name
func (s *StateType) MatchByTag(tagName string) []*Selector {
var result = make([]*Selector, 0)
s.selectors.Each(func(key string, selector *Selector) {
tag := selector.leaf().field.Tag
if tag == "" {
return
}
if _, ok := tag.Lookup(tagName); ok {
result = append(result, selector)
}
})
return result
}
// Type returns state underlying reflect type
func (s *StateType) Type() reflect.Type {
return s.rType
}
func (s *StateType) IsDefined() bool {
return len(s.selectors.Items) > 0
}
// Type returns state type
func (s *State) Type() *StateType {
return s.stateType
}
func (s *State) HasMarker() bool {
return s.stateType.HasMarker()
}
func (s *State) MarkerHolder() interface{} {
if !s.HasMarker() {
return nil
}
return s.stateType.marker.holder.Value(s.ptr)
}
func (s *State) EnsureMarker() {
if !s.HasMarker() || s.stateType.marker.holder.Kind() == reflect.Struct {
return
}
if s.stateType.marker.holder.IsNil(s.ptr) {
v := reflect.New(s.stateType.marker.holder.Type.Elem()).Interface()
s.stateType.marker.holder.SetValue(s.ptr, v)
}
}
// Marker returns marker
func (s *StateType) Marker() *Marker {
return s.marker
}
func (s *StateType) HasMarker() bool {
if s.marker == nil || s.marker.holder == nil {
return false
}
return true
}
// Pointer returns state actual value pointer
func (s *State) Pointer() unsafe.Pointer {
return s.ptr
}
// State return state value
func (s *State) State() interface{} {
return s.value
}
// Sync syncs value ptr ot value if out of sync
func (s *State) Sync() {
if s.valuePtr != nil && s.valuePtr != s.value {
s.value = reflect.ValueOf(s.valuePtr).Elem().Interface()
s.ptr = xunsafe.AsPointer(s.value)
}
}
// SyncPointer syncs value ptr to value
func (s *State) SyncPointer() {
s.valuePtr = reflect.NewAt(s.stateType.rType, s.ptr).Interface()
}
// StatePtr return state value
func (s *State) StatePtr() interface{} {
return s.valuePtr
}
// SetValue set state value
func (s *State) SetValue(aPath string, value interface{}, pathOptions ...PathOption) error {
selector, err := s.Selector(aPath)
if err != nil {
return err
}
return selector.SetValue(s.ptr, value, pathOptions...)
}
// SetPrimitive sets primitive value
func (s *State) SetPrimitive(aPath string, value interface{}, pathOptions ...PathOption) error {
selector, err := s.Selector(aPath)
if err != nil {
return err
}
return selector.Set(s.ptr, value, pathOptions...)
}
// SetString set string for supplied state path
func (s *State) SetString(aPath string, value string, pathOptions ...PathOption) error {
selector, err := s.Selector(aPath)
if err != nil {
return err
}
selector.SetString(s.ptr, value, pathOptions...)
return nil
}
// SetInt set int for supplied path
func (s *State) SetInt(aPath string, value int, pathOptions ...PathOption) error {
selector, err := s.Selector(aPath)
if err != nil {
return err
}
selector.SetInt(s.ptr, value, pathOptions...)
return nil
}
// SetBool set bool for supplied state path
func (s *State) SetBool(aPath string, value bool, pathOptions ...PathOption) error {
selector, err := s.Selector(aPath)
if err != nil {
return err
}
selector.SetBool(s.ptr, value, pathOptions...)
return nil
}
// SetFloat64 set float for supplied state path
func (s *State) SetFloat64(aPath string, value float64, pathOptions ...PathOption) error {
selector, err := s.Selector(aPath)
if err != nil {
return err
}
selector.SetFloat64(s.ptr, value, pathOptions...)
return nil
}
//SetFloat43 set float for supplied state path
func (s *State) SetFloat32(aPath string, value float32, pathOptions ...PathOption) error {
selector, err := s.Selector(aPath)
if err != nil {
return err
}
selector.SetFloat32(s.ptr, value, pathOptions...)
return nil
}
// Value returns a value for supplied path
func (s *State) Value(aPath string, pathOptions ...PathOption) (interface{}, error) {
selector, err := s.Selector(aPath)
if err != nil {
return nil, err
}
return selector.Value(s.ptr, pathOptions...), nil
}
// Values returns a values for supplied path
func (s *State) Values(aPath string, pathOptions ...PathOption) ([]interface{}, error) {
selector, err := s.Selector(aPath)
if err != nil {
return nil, err
}
return selector.Values(s.ptr, pathOptions...), nil
}
// Bool returns a value for supplied path
func (s *State) Bool(aPath string, pathOptions ...PathOption) (bool, error) {
selector, err := s.Selector(aPath)
if err != nil {
return false, err
}
return selector.Bool(s.ptr, pathOptions...), nil
}
// Bool returns a value for supplied path
func (s *State) String(aPath string, pathOptions ...PathOption) (string, error) {
selector, err := s.Selector(aPath)
if err != nil {
return "", err
}
return selector.String(s.ptr, pathOptions...), nil
}
func (s *State) Float64(aPath string, pathOptions ...PathOption) (float64, error) {
selector, err := s.Selector(aPath)
if err != nil {
return 0.0, err
}
return selector.Float64(s.ptr, pathOptions...), nil
}
// Selector returns a state selector for supplied path
func (s *State) Selector(aPath string) (*Selector, error) {
index, ok := s.stateType.selectors.Map[aPath]
if !ok {
return nil, fmt.Errorf("failed to lookup path %v at %s", aPath, s.stateType.rType.String())
}
return s.stateType.selectors.Items[index], nil
}
// MergeFrom merge stage with from state
func (s *State) MergeFrom(from *State) error {
sType := s.Type()
for _, aSel := range sType.selectors.Root {
value, err := from.Value(aSel.Name())
if err != nil {
return err
}
if err = aSel.Set(s.ptr, value); err != nil {
return err
}
}
return nil
}
// NewStateType creates a state type
func NewStateType(rType reflect.Type, opts ...SelectorOption) *StateType {
ret := &StateType{rType: rType, isPtr: rType.Kind() == reflect.Ptr}
ret.selectors, ret.marker = NewSelectors(rType, opts...)
return ret
}
// WithValue creates a state with value
func (t *StateType) WithValue(value interface{}) *State {
//TODO assignable assertion
sType := reflect.TypeOf(value)
isPtr := sType.Kind() == reflect.Ptr
ret := &State{stateType: t, value: value, ptr: xunsafe.AsPointer(value)}
if isPtr {
ret.valuePtr = value
} else {
ret.valuePtr = reflect.NewAt(sType, ret.ptr).Interface()
}
return ret
}
// NewState creates a state
func (t *StateType) NewState() *State {
var valuePtr reflect.Value
if t.isPtr {
valuePtr = reflect.New(t.rType.Elem())
} else {
valuePtr = reflect.New(t.rType)
}
if t.rType.Kind() == reflect.Slice {
sliceValue := reflect.MakeSlice(t.rType, 0, 1)
valuePtr.Elem().Set(sliceValue)
}
ret := &State{stateType: t}
if t.isPtr {
ret.value = valuePtr.Interface()
ret.valuePtr = ret.value
} else {
ret.valuePtr = valuePtr.Interface()
ret.value = valuePtr.Elem().Interface()
}
ret.ptr = xunsafe.AsPointer(ret.value)
return ret
}