MCPcopy
hub / github.com/purpleidea/mgmt / FuncPrefixToFunctionsScope

Function FuncPrefixToFunctionsScope

lang/ast/util.go:49–103  ·  view source on GitHub ↗

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.

(prefix string)

Source from the content-addressed store, hash-verified

47// portion of the scope from a function prefix lookup. Basically this wraps the
48// implementation in the Func interface in the *ExprFunc struct.
49func FuncPrefixToFunctionsScope(prefix string) map[string]interfaces.Expr {
50 fns := funcs.LookupPrefix(prefix) // map[string]func() interfaces.Func
51 exprs := make(map[string]interfaces.Expr)
52 for name, f := range fns {
53
54 x := f() // inspect
55 // We can pass in Fns []*types.FuncValue for the simple and
56 // simplepoly API's and avoid the double wrapping from the
57 // simple/simplepoly API's to the main function api and back.
58 if st, ok := x.(*simple.WrappedFunc); simple.DirectInterface && ok {
59 fn := &ExprFunc{
60 Title: name,
61
62 Values: []*types.FuncValue{st.Fn}, // just one!
63 }
64 // XXX: should we run fn.SetType(st.Fn.Type()) ?
65 exprs[name] = fn
66 continue
67 }
68
69 //if st, ok := x.(*simplepoly.WrappedFunc); simplepoly.DirectInterface && ok {
70 // fn := &ExprFunc{
71 // Title: name,
72
73 // Values: st.Fns,
74 // }
75 // exprs[name] = fn
76 // continue
77 //}
78
79 fn := &ExprFunc{
80 Title: name,
81 // We need to pass in the constructor function, because
82 // we'll need more than one copy of this function if it
83 // is used in more than one place so we can build more.
84 Function: f, // func() interfaces.Func
85 }
86 exprs[name] = fn
87 }
88
89 // Wrap every Expr in ExprPoly, so that the function can be used with
90 // different types. Those functions are all builtins, so they don't need
91 // to access the surrounding scope.
92 exprPolys := make(map[string]interfaces.Expr)
93 for name, expr := range exprs {
94 exprPolys[name] = &ExprPoly{
95 Definition: &ExprTopLevel{
96 Definition: expr,
97 CapturedScope: interfaces.EmptyScope(),
98 },
99 }
100 }
101
102 return exprPolys
103}
104
105// VarPrefixToVariablesScope is a helper function to return the variables
106// portion of the scope from a variable prefix lookup. Basically this is useful

Callers 7

TestAstFunc1Function · 0.92
TestAstFunc2Function · 0.92
TestAstFunc3Function · 0.92
InitMethod · 0.92
TestUnification1Function · 0.92
CliMethod · 0.92
importSystemScopeMethod · 0.85

Calls 2

LookupPrefixFunction · 0.92
EmptyScopeFunction · 0.92

Tested by 4

TestAstFunc1Function · 0.74
TestAstFunc2Function · 0.74
TestAstFunc3Function · 0.74
TestUnification1Function · 0.74