lang: core: Simplify list and map lookup default functions

This commit is contained in:
James Shubin
2025-02-26 19:35:07 -05:00
parent d579787bcd
commit d56896cb0d
4 changed files with 77 additions and 381 deletions

View File

@@ -0,0 +1,22 @@
import "fmt"
$some_list = ["l", "m", "n",]
$some_map = {
"ottawa" => 6,
"toronto" => 7,
"montreal" => 8,
"vancouver" => 9,
}
print "letter" {
msg => fmt.printf("letter: %s", $some_list[13] || "j"),
Meta:autogroup => false,
}
print "city" {
msg => fmt.printf("city: %d", $some_map["montréal"] || 13),
Meta:autogroup => false,
}

View File

@@ -34,206 +34,39 @@ import (
"fmt"
"math"
"github.com/purpleidea/mgmt/lang/funcs"
"github.com/purpleidea/mgmt/lang/interfaces"
"github.com/purpleidea/mgmt/lang/funcs/simple"
"github.com/purpleidea/mgmt/lang/types"
"github.com/purpleidea/mgmt/util/errwrap"
)
const (
// ListLookupDefaultFuncName is the name this function is registered as.
ListLookupDefaultFuncName = "list_lookup_default"
// arg names...
listLookupDefaultArgNameList = "list"
listLookupDefaultArgNameIndex = "index"
listLookupDefaultArgNameDefault = "default"
)
func init() {
funcs.Register(ListLookupDefaultFuncName, func() interfaces.Func { return &ListLookupDefaultFunc{} }) // must register the func and name
simple.Register(ListLookupDefaultFuncName, &simple.Scaffold{
T: types.NewType("func(list []?1, index int, default ?1) ?1"),
F: ListLookupDefault,
})
}
var _ interfaces.BuildableFunc = &ListLookupDefaultFunc{} // ensure it meets this expectation
// ListLookupDefault returns the value corresponding to the input index in the
// list. If the value is not present, it returns the default value supplied.
func ListLookupDefault(ctx context.Context, input []types.Value) (types.Value, error) {
l := input[0].(*types.ListValue)
index := input[1].Int()
// ListLookupDefaultFunc is a list index lookup function. If you provide a
// negative index, then it will return the default value you specified for this
// function.
// TODO: Eventually we will deprecate this function when the function engine can
// support passing a value for erroring functions. (Bad index could be an err!)
type ListLookupDefaultFunc struct {
// TODO: Logically should this be ported to be the type of the elements?
Type *types.Type // Kind == List, that is used as the list we lookup in
// TODO: should we handle overflow by returning default?
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 { // lists can't have negative indexes (for now)
return nil, fmt.Errorf("list index negative, got: %d", index)
}
init *interfaces.Init
last types.Value // last value received to use for diff
result types.Value // last calculated output
}
// String returns a simple name for this function. This is needed so this struct
// can satisfy the pgraph.Vertex interface.
func (obj *ListLookupDefaultFunc) String() string {
return ListLookupDefaultFuncName
}
// ArgGen returns the Nth arg name for this function.
func (obj *ListLookupDefaultFunc) ArgGen(index int) (string, error) {
seq := []string{listLookupDefaultArgNameList, listLookupDefaultArgNameIndex, listLookupDefaultArgNameDefault}
if l := len(seq); index >= l {
return "", fmt.Errorf("index %d exceeds arg length of %d", index, l)
}
return seq[index], nil
}
// helper
func (obj *ListLookupDefaultFunc) sig() *types.Type {
// func(list []?1, index int, default ?1) ?1
v := "?1"
if obj.Type != nil { // don't panic if called speculatively
v = obj.Type.Val.String()
}
return types.NewType(fmt.Sprintf(
"func(%s []%s, %s int, %s %s) %s",
listLookupDefaultArgNameList, v,
listLookupDefaultArgNameIndex,
listLookupDefaultArgNameDefault, v,
v,
))
}
// Build is run to turn the polymorphic, undetermined function, into the
// specific statically typed version. It is usually run after Unify completes,
// and must be run before Info() and any of the other Func interface methods are
// used. This function is idempotent, as long as the arg isn't changed between
// runs.
func (obj *ListLookupDefaultFunc) Build(typ *types.Type) (*types.Type, error) {
// typ is the KindFunc signature we're trying to build...
if typ.Kind != types.KindFunc {
return nil, fmt.Errorf("input type must be of kind func")
}
if len(typ.Ord) != 3 {
return nil, fmt.Errorf("the listlookup function needs exactly three args")
}
if typ.Out == nil {
return nil, fmt.Errorf("return type of function must be specified")
}
if typ.Map == nil {
return nil, fmt.Errorf("invalid input type")
}
tList, exists := typ.Map[typ.Ord[0]]
if !exists || tList == nil {
return nil, fmt.Errorf("first arg must be specified")
}
tIndex, exists := typ.Map[typ.Ord[1]]
if !exists || tIndex == nil {
return nil, fmt.Errorf("second arg must be specified")
}
tDefault, exists := typ.Map[typ.Ord[2]]
if !exists || tDefault == nil {
return nil, fmt.Errorf("third arg must be specified")
}
if tIndex != nil && tIndex.Kind != types.KindInt {
return nil, fmt.Errorf("index must be int kind")
}
if err := tList.Val.Cmp(tDefault); err != nil {
return nil, errwrap.Wrapf(err, "default must match list val type")
}
if err := tList.Val.Cmp(typ.Out); err != nil {
return nil, errwrap.Wrapf(err, "return type must match list val type")
}
obj.Type = tList // list type
return obj.sig(), nil
}
// Validate tells us if the input struct takes a valid form.
func (obj *ListLookupDefaultFunc) Validate() error {
if obj.Type == nil { // build must be run first
return fmt.Errorf("type is still unspecified")
}
if obj.Type.Kind != types.KindList {
return fmt.Errorf("type must be a kind of list")
}
return nil
}
// Info returns some static info about itself. Build must be called before this
// will return correct data.
func (obj *ListLookupDefaultFunc) Info() *interfaces.Info {
return &interfaces.Info{
Pure: true,
Memo: false,
Sig: obj.sig(), // helper
Err: obj.Validate(),
}
}
// Init runs some startup code for this function.
func (obj *ListLookupDefaultFunc) Init(init *interfaces.Init) error {
obj.init = init
return nil
}
// Stream returns the changing values that this func has over time.
func (obj *ListLookupDefaultFunc) 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()[listLookupDefaultArgNameList]).(*types.ListValue)
index := input.Struct()[listLookupDefaultArgNameIndex].Int()
def := input.Struct()[listLookupDefaultArgNameDefault]
// TODO: should we handle overflow by returning default?
if index > math.MaxInt { // max int size varies by arch
return fmt.Errorf("list index overflow, got: %d, max is: %d", index, math.MaxInt)
}
// negative index values are "not found" here!
var result types.Value
val, exists := l.Lookup(int(index))
if exists {
result = val
} else {
result = def
}
// 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
}
}
val, exists := l.Lookup(int(index))
if !exists {
return input[2], nil // default value
}
return val, nil
}

View File

@@ -108,16 +108,30 @@ func (obj *LookupDefaultFunc) Build(typ *types.Type) (*types.Type, error) {
return nil, fmt.Errorf("first arg must have a type")
}
name := ""
if tListOrMap.Kind == types.KindList {
obj.fn = &ListLookupDefaultFunc{} // set it
return obj.fn.Build(typ)
name = ListLookupDefaultFuncName
}
if tListOrMap.Kind == types.KindMap {
obj.fn = &MapLookupDefaultFunc{} // set it
return obj.fn.Build(typ)
name = MapLookupDefaultFuncName
}
if name == "" {
return nil, fmt.Errorf("we must lookup from either a list or a map")
}
return nil, fmt.Errorf("we must lookup from either a list or a map")
f, err := funcs.Lookup(name)
if err != nil {
// programming error
return nil, err
}
bf, ok := f.(interfaces.BuildableFunc)
if !ok {
// programming error
return nil, fmt.Errorf("not a BuildableFunc")
}
obj.fn = bf
return obj.fn.Build(typ)
}
// Validate tells us if the input struct takes a valid form.

View File

@@ -31,204 +31,31 @@ package core
import (
"context"
"fmt"
"github.com/purpleidea/mgmt/lang/funcs"
"github.com/purpleidea/mgmt/lang/interfaces"
"github.com/purpleidea/mgmt/lang/funcs/simple"
"github.com/purpleidea/mgmt/lang/types"
"github.com/purpleidea/mgmt/util/errwrap"
)
const (
// MapLookupDefaultFuncName is the name this function is registered as.
MapLookupDefaultFuncName = "map_lookup_default"
// arg names...
mapLookupDefaultArgNameMap = "map"
mapLookupDefaultArgNameKey = "key"
mapLookupDefaultArgNameDef = "default"
)
func init() {
funcs.Register(MapLookupDefaultFuncName, func() interfaces.Func { return &MapLookupDefaultFunc{} }) // must register the func and name
simple.Register(MapLookupDefaultFuncName, &simple.Scaffold{
T: types.NewType("func(map map{?1: ?2}, key ?1, default ?2) ?2"),
F: MapLookupDefault,
})
}
var _ interfaces.BuildableFunc = &MapLookupDefaultFunc{} // ensure it meets this expectation
// MapLookupDefault returns the value corresponding to the input key in the map.
// If the value is not present, it returns the default value supplied.
func MapLookupDefault(ctx context.Context, input []types.Value) (types.Value, error) {
m := input[0].(*types.MapValue)
// MapLookupDefaultFunc is a key map lookup function. If you provide a missing
// key, then it will return the default value you specified for this function.
// TODO: Eventually we will deprecate this function when the function engine can
// support passing a value for erroring functions. (Bad index could be an err!)
type MapLookupDefaultFunc struct {
Type *types.Type // Kind == Map, that is used as the map we lookup
init *interfaces.Init
last types.Value // last value received to use for diff
result types.Value // last calculated output
}
// String returns a simple name for this function. This is needed so this struct
// can satisfy the pgraph.Vertex interface.
func (obj *MapLookupDefaultFunc) String() string {
return MapLookupDefaultFuncName
}
// ArgGen returns the Nth arg name for this function.
func (obj *MapLookupDefaultFunc) ArgGen(index int) (string, error) {
seq := []string{mapLookupDefaultArgNameMap, mapLookupDefaultArgNameKey, mapLookupDefaultArgNameDef}
if l := len(seq); index >= l {
return "", fmt.Errorf("index %d exceeds arg length of %d", index, l)
}
return seq[index], nil
}
// helper
func (obj *MapLookupDefaultFunc) sig() *types.Type {
// func(map map{?1: ?2}, key ?1) ?2
k := "?1"
v := "?2"
m := fmt.Sprintf("map{%s: %s}", k, v)
if obj.Type != nil { // don't panic if called speculatively
k = obj.Type.Key.String()
v = obj.Type.Val.String()
m = obj.Type.String()
}
return types.NewType(fmt.Sprintf(
"func(%s %s, %s %s, %s %s) %s",
mapLookupDefaultArgNameMap, m,
mapLookupDefaultArgNameKey, k,
mapLookupDefaultArgNameDef, v,
v,
))
}
// Build is run to turn the polymorphic, undetermined function, into the
// specific statically typed version. It is usually run after Unify completes,
// and must be run before Info() and any of the other Func interface methods are
// used. This function is idempotent, as long as the arg isn't changed between
// runs.
func (obj *MapLookupDefaultFunc) Build(typ *types.Type) (*types.Type, error) {
// typ is the KindFunc signature we're trying to build...
if typ.Kind != types.KindFunc {
return nil, fmt.Errorf("input type must be of kind func")
}
if len(typ.Ord) != 3 {
return nil, fmt.Errorf("the maplookup function needs exactly three args")
}
if typ.Out == nil {
return nil, fmt.Errorf("return type of function must be specified")
}
if typ.Map == nil {
return nil, fmt.Errorf("invalid input type")
}
tMap, exists := typ.Map[typ.Ord[0]]
if !exists || tMap == nil {
return nil, fmt.Errorf("first arg must be specified")
}
tKey, exists := typ.Map[typ.Ord[1]]
if !exists || tKey == nil {
return nil, fmt.Errorf("second arg must be specified")
}
tDef, exists := typ.Map[typ.Ord[2]]
if !exists || tDef == nil {
return nil, fmt.Errorf("third arg must be specified")
}
if err := tMap.Key.Cmp(tKey); err != nil {
return nil, errwrap.Wrapf(err, "key must match map key type")
}
if err := tMap.Val.Cmp(tDef); err != nil {
return nil, errwrap.Wrapf(err, "default must match map val type")
}
if err := tMap.Val.Cmp(typ.Out); err != nil {
return nil, errwrap.Wrapf(err, "return type must match map val type")
}
obj.Type = tMap // map type
return obj.sig(), nil
}
// Validate tells us if the input struct takes a valid form.
func (obj *MapLookupDefaultFunc) Validate() error {
if obj.Type == nil { // build must be run first
return fmt.Errorf("type is still unspecified")
}
if obj.Type.Kind != types.KindMap {
return fmt.Errorf("type must be a kind of map")
}
return nil
}
// Info returns some static info about itself. Build must be called before this
// will return correct data.
func (obj *MapLookupDefaultFunc) Info() *interfaces.Info {
return &interfaces.Info{
Pure: true,
Memo: false,
Sig: obj.sig(), // helper
Err: obj.Validate(),
}
}
// Init runs some startup code for this function.
func (obj *MapLookupDefaultFunc) Init(init *interfaces.Init) error {
obj.init = init
return nil
}
// Stream returns the changing values that this func has over time.
func (obj *MapLookupDefaultFunc) 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()[mapLookupDefaultArgNameMap]).(*types.MapValue)
key := input.Struct()[mapLookupDefaultArgNameKey]
def := input.Struct()[mapLookupDefaultArgNameDef]
var result types.Value
val, exists := m.Lookup(key)
if exists {
result = val
} else {
result = def
}
// 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
}
}
val, exists := m.Lookup(input[1])
if !exists {
return input[2], nil // default value
}
return val, nil
}