-
Notifications
You must be signed in to change notification settings - Fork 24
/
apply.go
350 lines (327 loc) · 10.3 KB
/
apply.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Copyright (c) 2019, The Emergent Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package params
import (
"fmt"
"log"
"reflect"
"strconv"
"strings"
"github.com/goki/gi/gi"
"github.com/goki/ki/kit"
)
// TargetType returns the first part of the path, indicating what type of
// object the params apply to. Uses the first item in the map (which is random)
// everything in the map must have the same target.
func (pr *Params) TargetType() string {
for pt := range *pr {
return strings.Split(pt, ".")[0]
}
return ""
}
// Path returns the second part of the path after the target type,
// indicating the path to the specific parameter being set.
func (pr *Params) Path(path string) string {
return strings.Join(strings.Split(path, ".")[1:], ".")
}
// Apply applies all parameter values to given object.
// Object must already be the appropriate target type based on
// the first element of the path (see TargetType method).
// If setMsg is true, then it will log a confirmation that the parameter
// was set (it always prints an error message if it fails to set the
// parameter at given path, and returns error if so).
func (pr *Params) Apply(obj interface{}, setMsg bool) error {
objNm := ""
if stylr, has := obj.(Styler); has {
objNm = stylr.Name()
if styob, has := obj.(StylerObj); has {
obj = styob.Object()
}
} else if lblr, has := obj.(gi.Labeler); has {
objNm = lblr.Label()
}
var rerr error
for pt, v := range *pr {
path := pr.Path(pt)
if hv, ok := obj.(Hypers); ok {
if cv, has := hv[pt]; has { // full path
cv["Val"] = v
} else {
hv[pt] = HyperVals{"Val": v}
}
continue
}
err := SetParam(obj, path, v)
if err == nil {
if setMsg {
log.Printf("%v Set param path: %v to value: %v\n", objNm, pt, v)
}
} else {
rerr = err // could accumulate but..
}
}
return rerr
}
///////////////////////////////////////////////////////////////////////
// Hypers
// TargetType returns the first part of the path, indicating what type of
// object the params apply to. Uses the first item in the map (which is random)
// everything in the map must have the same target.
func (pr *Hypers) TargetType() string {
for pt := range *pr {
return strings.Split(pt, ".")[0]
}
return ""
}
// Path returns the second part of the path after the target type,
// indicating the path to the specific parameter being set.
func (pr *Hypers) Path(path string) string {
return strings.Join(strings.Split(path, ".")[1:], ".")
}
// Apply applies all parameter values to given object.
// Object must already be the appropriate target type based on
// the first element of the path (see TargetType method).
// If setMsg is true, then it will log a confirmation that the parameter
// was set (it always prints an error message if it fails to set the
// parameter at given path, and returns error if so).
func (pr *Hypers) Apply(obj interface{}, setMsg bool) error {
objNm := ""
if stylr, has := obj.(Styler); has {
objNm = stylr.Name()
if styob, has := obj.(StylerObj); has {
obj = styob.Object()
}
} else if lblr, has := obj.(gi.Labeler); has {
objNm = lblr.Label()
}
if hv, ok := obj.(Hypers); ok {
hv.CopyFrom(*pr)
return nil
}
var rerr error
for pt, v := range *pr {
path := pr.Path(pt)
val, ok := v["Val"]
if !ok {
continue
}
err := SetParam(obj, path, val)
if err == nil {
if setMsg {
log.Printf("%v Set hypers path: %v to value: %v\n", objNm, pt, v)
}
} else {
rerr = err // could accumulate but..
}
}
return rerr
}
///////////////////////////////////////////////////////////////////////
// Sel
// Apply checks if Sel selector applies to this object according to (.Class, #Name, Type)
// using the params.Styler interface, and returns false if it does not.
// The TargetType of the Params is always tested against the obj's type name first.
// If it does apply, or is not a Styler, then the Params values are set.
// If setMsg is true, then a message is printed to confirm each parameter that is set.
// It always prints a message if a parameter fails to be set, and returns an error.
func (ps *Sel) Apply(obj interface{}, setMsg bool) (bool, error) {
if !ps.TargetTypeMatch(obj) {
return false, nil
}
if !ps.SelMatch(obj) {
return false, nil
}
errp := ps.Params.Apply(obj, setMsg)
errh := ps.Hypers.Apply(obj, setMsg)
if errp != nil {
return true, errp
}
return true, errh
}
// TargetTypeMatch return true if target type applies to object
func (ps *Sel) TargetTypeMatch(obj interface{}) bool {
trg := ps.Params.TargetType()
if stylr, has := obj.(Styler); has {
tnm := stylr.TypeName()
if tnm == trg {
return true
}
}
trgh := ps.Hypers.TargetType()
if stylr, has := obj.(Styler); has {
tnm := stylr.TypeName()
if tnm == trgh {
return true
}
}
tnm := kit.NonPtrType(reflect.TypeOf(obj)).Name()
return tnm == trg || tnm == trgh
}
// SelMatch returns true if Sel selector matches the target object properties
func (ps *Sel) SelMatch(obj interface{}) bool {
stylr, has := obj.(Styler)
if !has {
return true // default match if no styler..
}
if styob, has := obj.(StylerObj); has {
obj = styob.Object()
}
gotyp := kit.NonPtrType(reflect.TypeOf(obj)).Name()
return SelMatch(ps.Sel, stylr.Name(), stylr.Class(), stylr.TypeName(), gotyp)
}
// SelMatch returns true if Sel selector matches the target object properties
func SelMatch(sel string, name, cls, styp, gotyp string) bool {
if sel == "" {
return false
}
if sel[0] == '.' { // class
return ClassMatch(sel[1:], cls)
}
if sel[0] == '#' { // name
return name == sel[1:]
}
return styp == sel || gotyp == sel // type
}
// ClassMatch returns true if given class names -- handles space-separated multiple class names
func ClassMatch(sel, cls string) bool {
clss := strings.Split(cls, " ")
for _, cl := range clss {
if strings.TrimSpace(cl) == sel {
return true
}
}
return false
}
///////////////////////////////////////////////////////////////////////
// Sheet
// Apply applies entire sheet to given object, using param.Sel's in order
// see param.Sel.Apply() for details.
// returns true if any Sel's applied, and error if any errors.
// If setMsg is true, then a message is printed to confirm each parameter that is set.
// It always prints a message if a parameter fails to be set, and returns an error.
func (ps *Sheet) Apply(obj interface{}, setMsg bool) (bool, error) {
applied := false
var rerr error
for _, sl := range *ps {
app, err := sl.Apply(obj, setMsg)
if app {
applied = true
}
if err != nil {
rerr = err
}
}
return applied, rerr
}
///////////////////////////////////////////////////////////////////////
// Core Find / Set / Get Param
// FindParam parses the path and recursively tries to find the parameter pointed to
// by the path (dot-delimited field names).
// Returns error if not found, and always also emits error messages --
// the target type should already have been identified and this should only
// be called when there is an expectation of the path working.
func FindParam(val reflect.Value, path string) (reflect.Value, error) {
npv := kit.NonPtrValue(val)
if npv.Kind() != reflect.Struct {
err := fmt.Errorf("params.FindParam: object is not a struct: %v kind: %v -- params must be on structs, path: %v\n", npv.String(), npv.Kind(), path)
log.Println(err)
return npv, err
}
paths := strings.Split(path, ".")
fnm := paths[0]
fld := npv.FieldByName(fnm)
if !fld.IsValid() {
err := fmt.Errorf("params.FindParam: could not find Field named: %v in struct: %v kind: %v, path: %v\n", fnm, npv.String(), npv.Kind(), path)
log.Println(err)
return fld, err
}
if len(paths) == 1 {
return fld.Addr(), nil
}
return FindParam(fld.Addr(), strings.Join(paths[1:], ".")) // need addr
}
// SetParam sets parameter at given path on given object to given value
// converts the string param val as appropriate for target type.
// returns error if path not found or cannot set (always logged).
func SetParam(obj interface{}, path string, val string) error {
npv := kit.NonPtrValue(reflect.ValueOf(obj))
if npv.Kind() == reflect.Map { // only for string maps
npv.SetMapIndex(reflect.ValueOf(path), reflect.ValueOf(val))
return nil
}
fld, err := FindParam(reflect.ValueOf(obj), path)
if err != nil {
return err
}
npf := kit.NonPtrValue(fld)
switch npf.Kind() {
case reflect.String:
npf.SetString(val)
case reflect.Float64, reflect.Float32:
r, err := strconv.ParseFloat(val, 64)
if err != nil {
log.Println(err)
return err
}
npf.SetFloat(r)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
r, err := strconv.ParseInt(val, 0, 64)
if err != nil {
enerr := kit.SetEnumValueFromString(fld, val)
if enerr != nil {
log.Println(err)
return err
}
} else {
npf.SetInt(r)
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
r, err := strconv.ParseInt(val, 0, 64)
if err != nil {
log.Println(err)
return err
}
npf.SetUint(uint64(r))
case reflect.Bool:
r, err := strconv.ParseBool(val)
if err != nil {
log.Println(err)
return err
}
npf.SetBool(r)
default:
err := fmt.Errorf("params.SetParam: field is not of a numeric type -- only numeric types supported. value: %v, kind: %v, path: %v\n", npf.String(), npf.Kind(), path)
log.Println(err)
return err
}
return nil
}
// GetParam gets parameter value at given path on given object.
// converts target type to float64.
// returns error if path not found or target is not a numeric type (always logged).
func GetParam(obj interface{}, path string) (float64, error) {
fld, err := FindParam(reflect.ValueOf(obj), path)
if err != nil {
return 0, err
}
npf := kit.NonPtrValue(fld)
switch npf.Kind() {
case reflect.Float64, reflect.Float32:
return npf.Float(), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return float64(npf.Int()), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return float64(npf.Uint()), nil
case reflect.Bool:
if npf.Bool() {
return 1, nil
} else {
return 0, nil
}
default:
err := fmt.Errorf("params.GetParam: field is not of a numeric type -- only numeric types supported. value: %v, kind: %v, path: %v\n", npf.String(), npf.Kind(), path)
log.Println(err)
return 0, err
}
}