lang: core: Shorten functions with wrapper

This demonstrates how to write a function with the wrapper. Note that
you must not include Init if you're not calling the nested wrapper
function.
This commit is contained in:
James Shubin
2024-08-18 18:29:01 -04:00
parent 654e376be7
commit 6f3c3c318b
2 changed files with 73 additions and 125 deletions

View File

@@ -35,6 +35,7 @@ import (
"math" "math"
"github.com/purpleidea/mgmt/lang/funcs" "github.com/purpleidea/mgmt/lang/funcs"
"github.com/purpleidea/mgmt/lang/funcs/wrapped"
"github.com/purpleidea/mgmt/lang/interfaces" "github.com/purpleidea/mgmt/lang/interfaces"
"github.com/purpleidea/mgmt/lang/types" "github.com/purpleidea/mgmt/lang/types"
"github.com/purpleidea/mgmt/util/errwrap" "github.com/purpleidea/mgmt/util/errwrap"
@@ -63,6 +64,8 @@ var _ interfaces.BuildableFunc = &ListLookupFunc{} // ensure it meets this expec
// omitted, then this function errors if the list index is not present. Passing // omitted, then this function errors if the list index is not present. Passing
// a negative index will always result in an error. // a negative index will always result in an error.
type ListLookupFunc struct { type ListLookupFunc struct {
*wrapped.Func // *wrapped.Func as a type alias to pull in the base impl.
Type *types.Type // Kind == List, that is used as the list we lookup in Type *types.Type // Kind == List, that is used as the list we lookup in
hasDefault *bool // does this function take a third arg? hasDefault *bool // does this function take a third arg?
@@ -197,7 +200,18 @@ func (obj *ListLookupFunc) Build(typ *types.Type) (*types.Type, error) {
} }
} }
obj.Func = &wrapped.Func{
Name: obj.String(),
Type: typ, // .Copy(),
}
obj.Type = tList // list type obj.Type = tList // list type
fn := &types.FuncValue{
T: typ,
V: obj.Function, // implementation
}
obj.Fn = fn // inside wrapper.Func
//return obj.Fn.T, nil
return obj.sig(), nil return obj.sig(), nil
} }
@@ -213,6 +227,33 @@ func (obj *ListLookupFunc) Copy() interfaces.Func {
} }
} }
// Function is the actual implementation here. This is used in lieu of the
// Stream function which we'd have these contents within.
func (obj *ListLookupFunc) Function(ctx context.Context, input []types.Value) (types.Value, error) {
l := (input[0]).(*types.ListValue)
index := input[1].Int()
//zero := l.Type().New() // the zero value
// TODO: should we handle overflow by returning zero?
if index > math.MaxInt { // max int size varies by arch
return nil, fmt.Errorf("list index overflow, got: %d, max is: %d", index, math.MaxInt)
}
if index < 0 {
return nil, fmt.Errorf("list index was negative, got: %d", index)
}
// negative index values are "not found" here!
val, exists := l.Lookup(int(index))
if exists {
return val, nil
}
if len(input) == 3 { // default value since lookup is missing
return input[2], nil
}
return nil, fmt.Errorf("list index not present, got: %d, len is: %d", index, len(l.List()))
}
// Validate tells us if the input struct takes a valid form. // Validate tells us if the input struct takes a valid form.
func (obj *ListLookupFunc) Validate() error { func (obj *ListLookupFunc) Validate() error {
if obj.Type == nil { // build must be run first if obj.Type == nil { // build must be run first
@@ -237,70 +278,3 @@ func (obj *ListLookupFunc) Info() *interfaces.Info {
Err: obj.Validate(), Err: obj.Validate(),
} }
} }
// Init runs some startup code for this function.
func (obj *ListLookupFunc) Init(init *interfaces.Init) error {
obj.init = init
return nil
}
// Stream returns the changing values that this func has over time.
func (obj *ListLookupFunc) Stream(ctx context.Context) 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
l := (input.Struct()[listLookupArgNameList]).(*types.ListValue)
index := input.Struct()[listLookupArgNameIndex].Int()
//zero := l.Type().New() // the zero value
// TODO: should we handle overflow by returning zero?
if index > math.MaxInt { // max int size varies by arch
return fmt.Errorf("list index overflow, got: %d, max is: %d", index, math.MaxInt)
}
if index < 0 {
return fmt.Errorf("list index was negative, got: %d", index)
}
// negative index values are "not found" here!
var result types.Value
val, exists := l.Lookup(int(index))
if exists {
result = val
} else if *obj.hasDefault {
result = input.Struct()[listLookupArgNameDefault]
} else {
return fmt.Errorf("list index not present, got: %d, len is: %d", index, len(l.List()))
}
// 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 <-ctx.Done():
return nil
}
select {
case obj.init.Output <- obj.result: // send
case <-ctx.Done():
return nil
}
}
}

View File

@@ -34,6 +34,7 @@ import (
"fmt" "fmt"
"github.com/purpleidea/mgmt/lang/funcs" "github.com/purpleidea/mgmt/lang/funcs"
"github.com/purpleidea/mgmt/lang/funcs/wrapped"
"github.com/purpleidea/mgmt/lang/interfaces" "github.com/purpleidea/mgmt/lang/interfaces"
"github.com/purpleidea/mgmt/lang/types" "github.com/purpleidea/mgmt/lang/types"
"github.com/purpleidea/mgmt/util/errwrap" "github.com/purpleidea/mgmt/util/errwrap"
@@ -61,6 +62,8 @@ var _ interfaces.BuildableFunc = &MapLookupFunc{} // ensure it meets this expect
// will be returned if the map key is not present. If the third argument is // will be returned if the map key is not present. If the third argument is
// omitted, then this function errors if the map key is not present. // omitted, then this function errors if the map key is not present.
type MapLookupFunc struct { type MapLookupFunc struct {
*wrapped.Func // *wrapped.Func as a type alias to pull in the base impl.
Type *types.Type // Kind == Map, that is used as the map we lookup Type *types.Type // Kind == Map, that is used as the map we lookup
hasDefault *bool // does this function take a third arg? hasDefault *bool // does this function take a third arg?
@@ -199,7 +202,18 @@ func (obj *MapLookupFunc) Build(typ *types.Type) (*types.Type, error) {
} }
} }
obj.Func = &wrapped.Func{
Name: obj.String(),
Type: typ, // .Copy(),
}
obj.Type = tMap // map type obj.Type = tMap // map type
fn := &types.FuncValue{
T: typ,
V: obj.Function, // implementation
}
obj.Fn = fn // inside wrapper.Func
//return obj.Fn.T, nil
return obj.sig(), nil return obj.sig(), nil
} }
@@ -215,6 +229,24 @@ func (obj *MapLookupFunc) Copy() interfaces.Func {
} }
} }
// Function is the actual implementation here. This is used in lieu of the
// Stream function which we'd have these contents within.
func (obj *MapLookupFunc) Function(ctx context.Context, input []types.Value) (types.Value, error) {
m := (input[0]).(*types.MapValue)
key := input[1]
//zero := m.Type().New() // the zero value
val, exists := m.Lookup(key)
if exists {
return val, nil
}
if len(input) == 3 { // default value since lookup is missing
return input[2], nil
}
return nil, fmt.Errorf("map key not present, got: %v", key)
}
// Validate tells us if the input struct takes a valid form. // Validate tells us if the input struct takes a valid form.
func (obj *MapLookupFunc) Validate() error { func (obj *MapLookupFunc) Validate() error {
if obj.Type == nil { // build must be run first if obj.Type == nil { // build must be run first
@@ -239,61 +271,3 @@ func (obj *MapLookupFunc) Info() *interfaces.Info {
Err: obj.Validate(), Err: obj.Validate(),
} }
} }
// Init runs some startup code for this function.
func (obj *MapLookupFunc) Init(init *interfaces.Init) error {
obj.init = init
return nil
}
// Stream returns the changing values that this func has over time.
func (obj *MapLookupFunc) Stream(ctx context.Context) 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
m := (input.Struct()[mapLookupArgNameMap]).(*types.MapValue)
key := input.Struct()[mapLookupArgNameKey]
//zero := m.Type().New() // the zero value
var result types.Value
val, exists := m.Lookup(key)
if exists {
result = val
} else if *obj.hasDefault {
result = input.Struct()[mapLookupArgNameDef]
} else {
return fmt.Errorf("map key not present, got: %v", key)
}
// 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 <-ctx.Done():
return nil
}
select {
case obj.init.Output <- obj.result: // send
case <-ctx.Done():
return nil
}
}
}