-
-
Notifications
You must be signed in to change notification settings - Fork 306
/
util.go
217 lines (186 loc) · 5.61 KB
/
util.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
// Mgmt
// Copyright (C) 2013-2021+ 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 lang
import (
"fmt"
"strings"
"github.com/purpleidea/mgmt/lang/funcs"
"github.com/purpleidea/mgmt/lang/funcs/simple"
"github.com/purpleidea/mgmt/lang/funcs/simplepoly"
"github.com/purpleidea/mgmt/lang/funcs/vars"
"github.com/purpleidea/mgmt/lang/interfaces"
"github.com/purpleidea/mgmt/lang/types"
)
// FuncPrefixToFunctionsScope is a helper function to return the functions
// portion of the scope from a function prefix lookup. Basically this wraps the
// implementation in the Func interface in the *ExprFunc struct.
func FuncPrefixToFunctionsScope(prefix string) map[string]interfaces.Expr {
fns := funcs.LookupPrefix(prefix) // map[string]func() interfaces.Func
exprs := make(map[string]interfaces.Expr)
for name, f := range fns {
x := f() // inspect
// We can pass in Fns []*types.FuncValue for the simple and
// simplepoly API's and avoid the double wrapping from the
// simple/simplepoly API's to the main function api and back.
if st, ok := x.(*simple.WrappedFunc); simple.DirectInterface && ok {
fn := &ExprFunc{
Title: name,
Values: []*types.FuncValue{st.Fn}, // just one!
}
// XXX: should we run fn.SetType(st.Fn.Type()) ?
exprs[name] = fn
continue
} else if st, ok := x.(*simplepoly.WrappedFunc); simplepoly.DirectInterface && ok {
fn := &ExprFunc{
Title: name,
Values: st.Fns,
}
exprs[name] = fn
continue
}
fn := &ExprFunc{
Title: name,
// We need to pass in the constructor function, because
// we'll need more than one copy of this function if it
// is used in more than one place so we can build more.
Function: f, // func() interfaces.Func
}
exprs[name] = fn
}
return exprs
}
// VarPrefixToVariablesScope is a helper function to return the variables
// portion of the scope from a variable prefix lookup. Basically this is useful
// to pull out a portion of the variables we've defined by API.
func VarPrefixToVariablesScope(prefix string) map[string]interfaces.Expr {
fns := vars.LookupPrefix(prefix) // map[string]func() interfaces.Var
exprs := make(map[string]interfaces.Expr)
for name, f := range fns {
x := f() // inspect
expr, err := ValueToExpr(x)
if err != nil {
panic(fmt.Sprintf("could not build expr: %+v", err))
}
exprs[name] = expr
}
return exprs
}
// MergeExprMaps merges the two maps of Expr's, and errors if any overwriting
// would occur. If any prefix string is specified, that is added to the keys of
// the second "extra" map before doing the merge. This doesn't change the input
// maps.
func MergeExprMaps(m, extra map[string]interfaces.Expr, prefix ...string) (map[string]interfaces.Expr, error) {
p := strings.Join(prefix, "") // hack to have prefix be optional
result := map[string]interfaces.Expr{}
for k, v := range m {
result[k] = v // copy
}
for k, v := range extra {
name := p + k
if _, exists := result[name]; exists {
return nil, fmt.Errorf("duplicate variable: %s", name)
}
result[name] = v
}
return result, nil
}
// ValueToExpr converts a Value into the equivalent Expr.
// FIXME: Add some tests for this function.
func ValueToExpr(val types.Value) (interfaces.Expr, error) {
var expr interfaces.Expr
switch x := val.(type) {
case *types.BoolValue:
expr = &ExprBool{
V: x.Bool(),
}
case *types.StrValue:
expr = &ExprStr{
V: x.Str(),
}
case *types.IntValue:
expr = &ExprInt{
V: x.Int(),
}
case *types.FloatValue:
expr = &ExprFloat{
V: x.Float(),
}
case *types.ListValue:
exprs := []interfaces.Expr{}
for _, v := range x.List() {
e, err := ValueToExpr(v)
if err != nil {
return nil, err
}
exprs = append(exprs, e)
}
expr = &ExprList{
Elements: exprs,
}
case *types.MapValue:
kvs := []*ExprMapKV{}
for k, v := range x.Map() {
kx, err := ValueToExpr(k)
if err != nil {
return nil, err
}
vx, err := ValueToExpr(v)
if err != nil {
return nil, err
}
kv := &ExprMapKV{
Key: kx,
Val: vx,
}
kvs = append(kvs, kv)
}
expr = &ExprMap{
KVs: kvs,
}
case *types.StructValue:
fields := []*ExprStructField{}
for k, v := range x.Struct() {
fx, err := ValueToExpr(v)
if err != nil {
return nil, err
}
field := &ExprStructField{
Name: k,
Value: fx,
}
fields = append(fields, field)
}
expr = &ExprStruct{
Fields: fields,
}
case *types.FuncValue:
// TODO: this particular case is particularly untested!
expr = &ExprFunc{
Title: "<func from ValueToExpr>", // TODO: change this?
// TODO: symmetrically, it would have used x.Func() here
Values: []*types.FuncValue{
x, // just one!
},
}
case *types.VariantValue:
// TODO: should this be allowed, or should we unwrap them?
return nil, fmt.Errorf("variant values are not supported")
default:
return nil, fmt.Errorf("unknown type (%T) for value: %+v", val, val)
}
return expr, expr.SetType(val.Type())
}