lang, engine: Add a facility for resources to export constants
Since we focus on safety, it would be nice to reduce the chance of any
runtime errors if we made a typo for a resource parameter. With this
patch, each resource can export constants into the global namespace so
that typos would cause a compile error.
Of course in the future if we had a more advanced type system, then we
could support precise types for each individual resource param, but in
an attempt to keep things simple, we'll leave that for another day. It
would add complexity too if we ever wanted to store a parameter
externally.
Lastly, we might consider adding "special case" parsing so that directly
specified fields would parse intelligently. For example, we could allow:
file "/tmp/hello" {
state => exists, # magic sugar!
}
This isn't supported for now, but if it works after all the other parser
changes have been made, it might be something to consider.
This commit is contained in:
149
lang/util.go
149
lang/util.go
@@ -18,9 +18,13 @@
|
||||
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"
|
||||
)
|
||||
@@ -43,6 +47,7 @@ func FuncPrefixToFunctionsScope(prefix string) map[string]interfaces.Expr {
|
||||
|
||||
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 {
|
||||
@@ -66,3 +71,147 @@ func FuncPrefixToFunctionsScope(prefix string) map[string]interfaces.Expr {
|
||||
}
|
||||
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())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user