forked from purpleidea/mgmt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
structlookup_polyfunc.go
282 lines (242 loc) · 8.5 KB
/
structlookup_polyfunc.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
// Mgmt
// Copyright (C) 2013-2018+ James Shubin and the project contributors
// Written by James Shubin <james@shubin.ca> and the project contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package funcs
import (
"fmt"
"github.com/purpleidea/mgmt/lang/interfaces"
"github.com/purpleidea/mgmt/lang/types"
errwrap "github.com/pkg/errors"
)
const (
// StructLookupFuncName is the name this function is registered as. This
// starts with an underscore so that it cannot be used from the lexer.
// XXX: change to _structlookup and add syntax in the lexer/parser
StructLookupFuncName = "structlookup"
)
func init() {
Register(StructLookupFuncName, func() interfaces.Func { return &StructLookupPolyFunc{} }) // must register the func and name
}
// StructLookupPolyFunc is a key map lookup function.
type StructLookupPolyFunc struct {
Type *types.Type // Kind == Struct, that is used as the struct we lookup
Out *types.Type // type of field we're extracting
init *interfaces.Init
last types.Value // last value received to use for diff
field string
result types.Value // last calculated output
closeChan chan struct{}
}
// Polymorphisms returns the list of possible function signatures available for
// this static polymorphic function. It relies on type and value hints to limit
// the number of returned possibilities.
func (obj *StructLookupPolyFunc) Polymorphisms(partialType *types.Type, partialValues []types.Value) ([]*types.Type, error) {
// TODO: return `variant` as arg for now -- maybe there's a better way?
variant := []*types.Type{types.NewType("func(struct variant, field str) variant")}
if partialType == nil {
return variant, nil
}
var typ *types.Type // struct type of the first argument
var out *types.Type // type of the field
// TODO: if partialValue[0] exists, check it matches the type we expect
ord := partialType.Ord
if partialType.Map != nil {
if len(ord) != 2 {
return nil, fmt.Errorf("must have exactly two args in structlookup func")
}
if tStruct, exists := partialType.Map[ord[0]]; exists && tStruct != nil {
if tStruct.Kind != types.KindStruct {
return nil, fmt.Errorf("first arg for structlookup must be a struct")
}
if !tStruct.HasVariant() {
typ = tStruct // found
}
}
if tField, exists := partialType.Map[ord[1]]; exists && tField != nil {
if tField.Cmp(types.TypeStr) != nil {
return nil, fmt.Errorf("second arg for structlookup must be a string")
}
}
if len(partialValues) == 2 && partialValues[1] != nil {
if types.TypeStr.Cmp(partialValues[1].Type()) != nil {
return nil, fmt.Errorf("second value must be an str")
}
structType, exists := partialType.Map[ord[0]]
if !exists {
return nil, fmt.Errorf("missing struct field")
}
if structType != nil {
field := partialValues[1].Str()
fieldType, exists := structType.Map[field]
if !exists {
return nil, fmt.Errorf("field: `%s` does not exist in struct", field)
}
if fieldType != nil {
if partialType.Out != nil && fieldType.Cmp(partialType.Out) != nil {
return nil, fmt.Errorf("field `%s` must have same type as return type", field)
}
out = fieldType // found!
}
}
}
if tOut := partialType.Out; tOut != nil {
// TODO: we could check that at least one of the types
// in struct.Map was our type, but not very useful...
}
}
typFunc := &types.Type{
Kind: types.KindFunc, // function type
Map: make(map[string]*types.Type),
Ord: []string{"struct", "field"},
Out: out,
}
typFunc.Map["struct"] = typ
typFunc.Map["field"] = types.TypeStr
// set variant instead of nil
if typFunc.Map["struct"] == nil {
typFunc.Map["struct"] = types.TypeVariant
}
if out == nil {
typFunc.Out = types.TypeVariant
}
return []*types.Type{typFunc}, nil
}
// Build is run to turn the polymorphic, undetermined function, into the
// specific statically typed version. It is usually run after Unify completes,
// and must be run before Info() and any of the other Func interface methods are
// used. This function is idempotent, as long as the arg isn't changed between
// runs.
func (obj *StructLookupPolyFunc) Build(typ *types.Type) error {
// typ is the KindFunc signature we're trying to build...
if typ.Kind != types.KindFunc {
return fmt.Errorf("input type must be of kind func")
}
if len(typ.Ord) != 2 {
return fmt.Errorf("the structlookup function needs exactly two args")
}
if typ.Out == nil {
return fmt.Errorf("return type of function must be specified")
}
if typ.Map == nil {
return fmt.Errorf("invalid input type")
}
tStruct, exists := typ.Map[typ.Ord[0]]
if !exists || tStruct == nil {
return fmt.Errorf("first arg must be specified")
}
tField, exists := typ.Map[typ.Ord[1]]
if !exists || tField == nil {
return fmt.Errorf("second arg must be specified")
}
if err := tField.Cmp(types.TypeStr); err != nil {
return errwrap.Wrapf(err, "field must be an str")
}
// NOTE: We actually don't know which field this is, only its type! we
// could have cached the discovered field during Polymorphisms(), but it
// turns out it's not actually necessary for us to know it to build the
// struct.
obj.Type = tStruct // struct type
obj.Out = typ.Out // type of return value
return nil
}
// Validate tells us if the input struct takes a valid form.
func (obj *StructLookupPolyFunc) Validate() error {
if obj.Type == nil { // build must be run first
return fmt.Errorf("type is still unspecified")
}
if obj.Type.Kind != types.KindStruct {
return fmt.Errorf("type must be a kind of struct")
}
if obj.Out == nil {
return fmt.Errorf("return type must be specified")
}
for _, t := range obj.Type.Map {
if obj.Out.Cmp(t) == nil {
return nil // found at least one match
}
}
return fmt.Errorf("return type is not in the list of available struct fields")
}
// Info returns some static info about itself. Build must be called before this
// will return correct data.
func (obj *StructLookupPolyFunc) Info() *interfaces.Info {
typ := types.NewType(fmt.Sprintf("func(struct %s, field str) %s", obj.Type.String(), obj.Out.String()))
return &interfaces.Info{
Pure: true,
Memo: false,
Sig: typ, // func kind
Err: obj.Validate(),
}
}
// Init runs some startup code for this function.
func (obj *StructLookupPolyFunc) Init(init *interfaces.Init) error {
obj.init = init
obj.closeChan = make(chan struct{})
return nil
}
// Stream returns the changing values that this func has over time.
func (obj *StructLookupPolyFunc) Stream() error {
defer close(obj.init.Output) // the sender closes
for {
select {
case input, ok := <-obj.init.Input:
if !ok {
return nil // can't output any more
}
//if err := input.Type().Cmp(obj.Info().Sig.Input); err != nil {
// return errwrap.Wrapf(err, "wrong function input")
//}
if obj.last != nil && input.Cmp(obj.last) == nil {
continue // value didn't change, skip it
}
obj.last = input // store for next
st := (input.Struct()["struct"]).(*types.StructValue)
field := input.Struct()["field"].Str()
if field == "" {
return fmt.Errorf("received empty field")
}
result, exists := st.Lookup(field)
if !exists {
return fmt.Errorf("could not lookup field: `%s` in struct", field)
}
if obj.field == "" {
obj.field = field // store first field
}
if field != obj.field {
return fmt.Errorf("input field changed from: `%s`, to: `%s`", obj.field, field)
}
// if previous input was `2 + 4`, but now it
// changed to `1 + 5`, the result is still the
// same, so we can skip sending an update...
if obj.result != nil && result.Cmp(obj.result) == nil {
continue // result didn't change
}
obj.result = result // store new result
case <-obj.closeChan:
return nil
}
select {
case obj.init.Output <- obj.result: // send
case <-obj.closeChan:
return nil
}
}
}
// Close runs some shutdown code for this function and turns off the stream.
func (obj *StructLookupPolyFunc) Close() error {
close(obj.closeChan)
return nil
}