-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.go
73 lines (58 loc) · 1.22 KB
/
interface.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
package synth
import "reflect"
type Interface struct {
Inputs map[string]Input
Controls map[string]Control
Params map[string]Param
Outputs map[string]Output
}
func NewInterface() *Interface {
return &Interface{
Inputs: map[string]Input{},
Controls: map[string]Control{},
Params: map[string]Param{},
Outputs: map[string]Output{},
}
}
func (ifc *Interface) Extract(b Block) {
ifc.extract(reflect.ValueOf(b))
}
func (ifc *Interface) extract(v reflect.Value) {
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
if v.Kind() != reflect.Struct {
return
}
vt := v.Type()
for i := 0; i < vt.NumField(); i++ {
sf := vt.Field(i)
if !sf.IsExported() {
continue
}
fv := v.Field(i)
if !fv.CanInterface() {
continue
}
switch vv := fv.Interface().(type) {
case Control:
ifc.Controls[sf.Name] = vv
case Input:
ifc.Inputs[sf.Name] = vv
case Param:
ifc.Params[sf.Name] = vv
case Output:
ifc.Outputs[sf.Name] = vv
default:
// try to extract from a possible embedded struct
ifc.extract(fv)
}
}
}
func (ifc *Interface) ParamVals() map[string]any {
paramVals := map[string]any{}
for name, param := range ifc.Params {
paramVals[name] = param.GetValue()
}
return paramVals
}