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

Function init

lang/funcs/operators/operators.go:58–587  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

56)
57
58func init() {
59 info := &simple.Info{
60 Pure: true,
61 Memo: true,
62 Fast: true,
63 Spec: true,
64 }
65 RegisterOperator("+", &simple.Scaffold{
66 I: info,
67 T: types.NewType("func(?1, ?1) ?1"),
68 C: simple.TypeMatch([]string{
69 "func(str, str) str", // concatenation
70 "func(int, int) int", // addition
71 "func(float, float) float", // floating-point addition
72 }),
73 F: func(ctx context.Context, input []types.Value) (types.Value, error) {
74 if l := len(input); l != 2 { // catch programming bugs
75 return nil, fmt.Errorf("invalid len %d", l)
76 }
77 // Both args should be the same type for this function.
78 if err := input[0].Type().Cmp(input[1].Type()); err != nil {
79 // This check is required because of speculation.
80 // Perhaps a bug in our earlier speculation code?
81 return nil, errwrap.Wrapf(err, "invalid arg types")
82 // TODO: speculation error instead?
83 //return nil, funcs.ErrCantSpeculate
84 }
85
86 switch k := input[0].Type().Kind; k {
87 case types.KindStr:
88 return &types.StrValue{
89 V: input[0].Str() + input[1].Str(),
90 }, nil
91
92 case types.KindInt:
93 // FIXME: check for overflow?
94 return &types.IntValue{
95 V: input[0].Int() + input[1].Int(),
96 }, nil
97
98 case types.KindFloat:
99 return &types.FloatValue{
100 V: input[0].Float() + input[1].Float(),
101 }, nil
102
103 default:
104 return nil, fmt.Errorf("unsupported kind: %+v", k)
105 }
106 },
107 })
108
109 RegisterOperator("-", &simple.Scaffold{
110 I: info,
111 T: types.NewType("func(?1, ?1) ?1"),
112 C: simple.TypeMatch([]string{
113 "func(int, int) int", // subtraction
114 "func(float, float) float", // floating-point subtraction
115 }),

Callers

nothing calls this directly

Calls 13

NewTypeFunction · 0.92
TypeMatchFunction · 0.92
WrapfFunction · 0.92
IsComparableKindFunction · 0.92
IterFunction · 0.92
RegisterFunction · 0.92
RegisterOperatorFunction · 0.85
CmpMethod · 0.65
TypeMethod · 0.65
StrMethod · 0.65
IntMethod · 0.65
FloatMethod · 0.65

Tested by

no test coverage detected