lang: funcs: Move standalone functions into core

Everything should be all together.
This commit is contained in:
James Shubin
2024-11-21 22:56:17 -05:00
parent b40d10a366
commit 018d3efc90
15 changed files with 58 additions and 28 deletions

View File

@@ -1,177 +0,0 @@
// Mgmt
// Copyright (C) 2013-2024+ James Shubin and the project contributors
// Written by James Shubin <james@shubin.ca> and the project contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// Additional permission under GNU GPL version 3 section 7
//
// If you modify this program, or any covered work, by linking or combining it
// with embedded mcl code and modules (and that the embedded mcl code and
// modules which link with this program, contain a copy of their source code in
// the authoritative form) containing parts covered by the terms of any other
// license, the licensors of this program grant you additional permission to
// convey the resulting work. Furthermore, the licensors of this program grant
// the original author, James Shubin, additional permission to update this
// additional permission if he deems it necessary to achieve the goals of this
// additional permission.
package funcs
import (
"context"
"fmt"
"github.com/purpleidea/mgmt/lang/interfaces"
"github.com/purpleidea/mgmt/lang/types"
)
const (
// ContainsFuncName is the name this function is registered as. This
// starts with an underscore so that it cannot be used from the lexer.
// XXX: change to _contains and add syntax in the lexer/parser
ContainsFuncName = "contains"
// arg names...
containsArgNameNeedle = "needle"
containsArgNameHaystack = "haystack"
)
func init() {
Register(ContainsFuncName, func() interfaces.Func { return &ContainsFunc{} }) // must register the func and name
}
var _ interfaces.BuildableFunc = &ContainsFunc{} // ensure it meets this expectation
// ContainsFunc returns true if a value is found in a list. Otherwise false.
type ContainsFunc struct {
Type *types.Type // this is the type of value stored in our list
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 *ContainsFunc) String() string {
return ContainsFuncName
}
// ArgGen returns the Nth arg name for this function.
func (obj *ContainsFunc) ArgGen(index int) (string, error) {
seq := []string{containsArgNameNeedle, containsArgNameHaystack}
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 *ContainsFunc) sig() *types.Type {
// func(needle ?1, haystack []?1) bool
s := "?1"
if obj.Type != nil { // don't panic if called speculatively
s = obj.Type.String() // if solved
}
return types.NewType(fmt.Sprintf("func(%s %s, %s []%s) bool", containsArgNameNeedle, s, containsArgNameHaystack, s))
}
// 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 *ContainsFunc) Build(typ *types.Type) (*types.Type, error) {
// We don't need to check that this matches, or that .Map has the right
// length, because otherwise it would mean type unification is giving a
// bad solution, which would be a major bug. Check to avoid any panics.
// Other functions might need to check something if they only accept a
// limited subset of the original type unification variables signature.
//if err := unificationUtil.UnifyCmp(typ, obj.sig()); err != nil {
// return nil, err
//}
obj.Type = typ.Map[typ.Ord[0]] // type of value stored in our list
return obj.sig(), nil
}
// Validate tells us if the input struct takes a valid form.
func (obj *ContainsFunc) Validate() error {
if obj.Type == nil { // build must be run first
return fmt.Errorf("type is still unspecified")
}
return nil
}
// Info returns some static info about itself. Build must be called before this
// will return correct data.
func (obj *ContainsFunc) Info() *interfaces.Info {
return &interfaces.Info{
Pure: true,
Memo: false,
Sig: obj.sig(), // helper, func kind
Err: obj.Validate(),
}
}
// Init runs some startup code for this function.
func (obj *ContainsFunc) Init(init *interfaces.Init) error {
obj.init = init
return nil
}
// Stream returns the changing values that this func has over time.
func (obj *ContainsFunc) 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
needle := input.Struct()[containsArgNameNeedle]
haystack := (input.Struct()[containsArgNameHaystack]).(*types.ListValue)
_, exists := haystack.Contains(needle)
var result types.Value = &types.BoolValue{V: exists}
// 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

@@ -61,6 +61,28 @@ const (
// is listed here because it needs a well-known name that can be used by
// the string interpolation code.
ConcatFuncName = "concat"
// ContainsFuncName is the name the contains function is registered as.
ContainsFuncName = "contains"
// LookupDefaultFuncName is the name this function is registered as.
// This starts with an underscore so that it cannot be used from the
// lexer.
LookupDefaultFuncName = "_lookup_default"
// LookupFuncName is the name this function is registered as.
// This starts with an underscore so that it cannot be used from the
// lexer.
LookupFuncName = "_lookup"
// StructLookupFuncName is the name this function is registered as. This
// starts with an underscore so that it cannot be used from the lexer.
StructLookupFuncName = "_struct_lookup"
// StructLookupOptionalFuncName is the name this function is registered
// as. This starts with an underscore so that it cannot be used from the
// lexer.
StructLookupOptionalFuncName = "_struct_lookup_optional"
)
// registeredFuncs is a global map of all possible funcs which can be used. You

View File

@@ -1,222 +0,0 @@
// Mgmt
// Copyright (C) 2013-2024+ James Shubin and the project contributors
// Written by James Shubin <james@shubin.ca> and the project contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// Additional permission under GNU GPL version 3 section 7
//
// If you modify this program, or any covered work, by linking or combining it
// with embedded mcl code and modules (and that the embedded mcl code and
// modules which link with this program, contain a copy of their source code in
// the authoritative form) containing parts covered by the terms of any other
// license, the licensors of this program grant you additional permission to
// convey the resulting work. Furthermore, the licensors of this program grant
// the original author, James Shubin, additional permission to update this
// additional permission if he deems it necessary to achieve the goals of this
// additional permission.
package funcs // TODO: should this be in its own individual package?
import (
"context"
"fmt"
"github.com/purpleidea/mgmt/lang/interfaces"
"github.com/purpleidea/mgmt/lang/types"
)
const (
// HistoryFuncName is the name this function is registered as.
// TODO: move this into a separate package
HistoryFuncName = "history"
// arg names...
historyArgNameValue = "value"
historyArgNameIndex = "index"
)
func init() {
Register(HistoryFuncName, func() interfaces.Func { return &HistoryFunc{} }) // must register the func and name
}
var _ interfaces.BuildableFunc = &HistoryFunc{} // ensure it meets this expectation
// HistoryFunc is special function which returns the Nth oldest value seen. It
// must store up incoming values until it gets enough to return the desired one.
// A restart of the program, will expunge the stored state. This obviously takes
// more memory, the further back you wish to index. A change in the index var is
// generally not useful, but it is permitted. Moving it to a smaller value will
// cause older index values to be expunged. If this is undesirable, a max count
// could be added. This was not implemented with efficiency in mind. Since some
// functions might not send out un-changed values, it might also make sense to
// implement a *time* based hysteresis, since this only looks at the last N
// changed values. A time based hysteresis would tick every precision-width, and
// store whatever the latest value at that time is.
type HistoryFunc struct {
Type *types.Type // type of input value (same as output type)
init *interfaces.Init
history []types.Value // goes from newest (index->0) to oldest (len()-1)
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 *HistoryFunc) String() string {
return HistoryFuncName
}
// ArgGen returns the Nth arg name for this function.
func (obj *HistoryFunc) ArgGen(index int) (string, error) {
seq := []string{historyArgNameValue, historyArgNameIndex}
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 *HistoryFunc) sig() *types.Type {
// func(value ?1, index int) ?1
s := "?1"
if obj.Type != nil {
s = obj.Type.String()
}
return types.NewType(fmt.Sprintf("func(%s %s, %s int) %s", historyArgNameValue, s, historyArgNameIndex, s))
}
// Build takes the now known function signature and stores it so that this
// function can appear to be static. That type is used to build our function
// statically.
func (obj *HistoryFunc) Build(typ *types.Type) (*types.Type, error) {
if typ.Kind != types.KindFunc {
return nil, fmt.Errorf("input type must be of kind func")
}
if len(typ.Ord) != 2 {
return nil, fmt.Errorf("the history function needs exactly two 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")
}
t1, exists := typ.Map[typ.Ord[1]]
if !exists || t1 == nil {
return nil, fmt.Errorf("second arg must be specified")
}
if t1.Cmp(types.TypeInt) != nil {
return nil, fmt.Errorf("second arg for history must be an int")
}
t0, exists := typ.Map[typ.Ord[0]]
if !exists || t0 == nil {
return nil, fmt.Errorf("first arg must be specified")
}
obj.Type = t0 // type of historical value is now known!
return obj.sig(), nil
}
// Validate makes sure we've built our struct properly. It is usually unused for
// normal functions that users can use directly.
func (obj *HistoryFunc) Validate() error {
if obj.Type == nil { // build must be run first
return fmt.Errorf("type is still unspecified")
}
return nil
}
// Info returns some static info about itself.
func (obj *HistoryFunc) Info() *interfaces.Info {
return &interfaces.Info{
Pure: false, // definitely false
Memo: false,
Sig: obj.sig(), // helper
Err: obj.Validate(),
}
}
// Init runs some startup code for this function.
func (obj *HistoryFunc) Init(init *interfaces.Init) error {
obj.init = init
return nil
}
// Stream returns the changing values that this func has over time.
func (obj *HistoryFunc) 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
index := int(input.Struct()[historyArgNameIndex].Int())
value := input.Struct()[historyArgNameValue]
var result types.Value
if index < 0 {
return fmt.Errorf("can't use a negative index of %d", index)
}
// 1) truncate history so length equals index
if len(obj.history) > index {
// remove all but first N elements, where N == index
obj.history = obj.history[:index]
}
// 2) (un)shift (add our new value to the beginning)
obj.history = append([]types.Value{value}, obj.history...)
// 3) are we ready to output a sufficiently old value?
if index >= len(obj.history) {
continue // not enough history is stored yet...
}
// 4) read one off the back
result = obj.history[len(obj.history)-1]
// TODO: do we want to do this?
// if the result is still the same, 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
// pass
case <-ctx.Done():
return nil
}
}
}

View File

@@ -1,238 +0,0 @@
// Mgmt
// Copyright (C) 2013-2024+ James Shubin and the project contributors
// Written by James Shubin <james@shubin.ca> and the project contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// Additional permission under GNU GPL version 3 section 7
//
// If you modify this program, or any covered work, by linking or combining it
// with embedded mcl code and modules (and that the embedded mcl code and
// modules which link with this program, contain a copy of their source code in
// the authoritative form) containing parts covered by the terms of any other
// license, the licensors of this program grant you additional permission to
// convey the resulting work. Furthermore, the licensors of this program grant
// the original author, James Shubin, additional permission to update this
// additional permission if he deems it necessary to achieve the goals of this
// additional permission.
package funcs
import (
"context"
"fmt"
"math"
"github.com/purpleidea/mgmt/lang/interfaces"
"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() {
Register(ListLookupDefaultFuncName, func() interfaces.Func { return &ListLookupDefaultFunc{} }) // must register the func and name
}
var _ interfaces.BuildableFunc = &ListLookupDefaultFunc{} // ensure it meets this expectation
// 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
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
}
}
}

View File

@@ -1,223 +0,0 @@
// Mgmt
// Copyright (C) 2013-2024+ James Shubin and the project contributors
// Written by James Shubin <james@shubin.ca> and the project contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// Additional permission under GNU GPL version 3 section 7
//
// If you modify this program, or any covered work, by linking or combining it
// with embedded mcl code and modules (and that the embedded mcl code and
// modules which link with this program, contain a copy of their source code in
// the authoritative form) containing parts covered by the terms of any other
// license, the licensors of this program grant you additional permission to
// convey the resulting work. Furthermore, the licensors of this program grant
// the original author, James Shubin, additional permission to update this
// additional permission if he deems it necessary to achieve the goals of this
// additional permission.
package funcs
import (
"context"
"fmt"
"math"
"github.com/purpleidea/mgmt/lang/interfaces"
"github.com/purpleidea/mgmt/lang/types"
"github.com/purpleidea/mgmt/util/errwrap"
)
const (
// ListLookupFuncName is the name this function is registered as.
ListLookupFuncName = "list_lookup"
// arg names...
listLookupArgNameList = "list"
listLookupArgNameIndex = "index"
)
func init() {
Register(ListLookupFuncName, func() interfaces.Func { return &ListLookupFunc{} }) // must register the func and name
}
var _ interfaces.BuildableFunc = &ListLookupFunc{} // ensure it meets this expectation
// ListLookupFunc is a list index lookup function. If you provide a negative
// index, then it will return the zero value for that type.
type ListLookupFunc struct {
Type *types.Type // Kind == List, that is used as the list we lookup in
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 *ListLookupFunc) String() string {
return ListLookupFuncName
}
// ArgGen returns the Nth arg name for this function.
func (obj *ListLookupFunc) ArgGen(index int) (string, error) {
seq := []string{listLookupArgNameList, listLookupArgNameIndex}
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 *ListLookupFunc) 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",
listLookupArgNameList, v,
listLookupArgNameIndex,
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 *ListLookupFunc) 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) != 2 {
return nil, fmt.Errorf("the listlookup function needs exactly two 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")
}
if tIndex != nil && tIndex.Kind != types.KindInt {
return nil, fmt.Errorf("index must be int kind")
}
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 *ListLookupFunc) 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 *ListLookupFunc) 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 *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().Val.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)
}
// negative index values are "not found" here!
var result types.Value
val, exists := l.Lookup(int(index))
if exists {
result = val
} else {
result = zero
}
// 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

@@ -1,162 +0,0 @@
// Mgmt
// Copyright (C) 2013-2024+ James Shubin and the project contributors
// Written by James Shubin <james@shubin.ca> and the project contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// Additional permission under GNU GPL version 3 section 7
//
// If you modify this program, or any covered work, by linking or combining it
// with embedded mcl code and modules (and that the embedded mcl code and
// modules which link with this program, contain a copy of their source code in
// the authoritative form) containing parts covered by the terms of any other
// license, the licensors of this program grant you additional permission to
// convey the resulting work. Furthermore, the licensors of this program grant
// the original author, James Shubin, additional permission to update this
// additional permission if he deems it necessary to achieve the goals of this
// additional permission.
package funcs
import (
"context"
"fmt"
"github.com/purpleidea/mgmt/lang/interfaces"
"github.com/purpleidea/mgmt/lang/types"
)
const (
// LookupDefaultFuncName is the name this function is registered as.
// This starts with an underscore so that it cannot be used from the
// lexer.
LookupDefaultFuncName = "_lookup_default"
// arg names...
lookupDefaultArgNameListOrMap = "listormap"
lookupDefaultArgNameIndexOrKey = "indexorkey"
lookupDefaultArgNameDefault = "default"
)
func init() {
Register(LookupDefaultFuncName, func() interfaces.Func { return &LookupDefaultFunc{} }) // must register the func and name
}
var _ interfaces.BuildableFunc = &LookupDefaultFunc{} // ensure it meets this expectation
// LookupDefaultFunc is a list index or map key lookup function. It does both
// because the current syntax in the parser is identical, so it's convenient to
// mix the two together. This calls out to some of the code in the
// ListLookupDefaultFunc and MapLookupDefaultFunc implementations. If the index
// or key for this input doesn't exist, 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 LookupDefaultFunc struct {
Type *types.Type // Kind == List OR Map, that is used as the list/map we lookup in
//init *interfaces.Init
fn interfaces.BuildableFunc // handle to ListLookupDefaultFunc or MapLookupDefaultFunc
}
// String returns a simple name for this function. This is needed so this struct
// can satisfy the pgraph.Vertex interface.
func (obj *LookupDefaultFunc) String() string {
return LookupDefaultFuncName
}
// ArgGen returns the Nth arg name for this function.
func (obj *LookupDefaultFunc) ArgGen(index int) (string, error) {
seq := []string{lookupDefaultArgNameListOrMap, lookupDefaultArgNameIndexOrKey, lookupDefaultArgNameDefault}
if l := len(seq); index >= l {
return "", fmt.Errorf("index %d exceeds arg length of %d", index, l)
}
return seq[index], nil
}
// 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 *LookupDefaultFunc) 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) < 1 {
return nil, fmt.Errorf("the lookup function needs at least one arg") // actually 2 or 3
}
tListOrMap, exists := typ.Map[typ.Ord[0]]
if !exists || tListOrMap == nil {
return nil, fmt.Errorf("first arg must be specified")
}
if tListOrMap == nil {
return nil, fmt.Errorf("first arg must have a type")
}
if tListOrMap.Kind == types.KindList {
obj.fn = &ListLookupDefaultFunc{} // set it
return obj.fn.Build(typ)
}
if tListOrMap.Kind == types.KindMap {
obj.fn = &MapLookupDefaultFunc{} // set it
return obj.fn.Build(typ)
}
return nil, fmt.Errorf("we must lookup from either a list or a map")
}
// Validate tells us if the input struct takes a valid form.
func (obj *LookupDefaultFunc) Validate() error {
if obj.fn == nil { // build must be run first
return fmt.Errorf("type is still unspecified")
}
return obj.fn.Validate()
}
// Info returns some static info about itself. Build must be called before this
// will return correct data.
func (obj *LookupDefaultFunc) Info() *interfaces.Info {
// func(list []?1, index int, default ?1) ?1
// OR
// func(map map{?1: ?2}, key ?1, default ?2) ?2
if obj.fn == nil {
return &interfaces.Info{
Pure: true,
Memo: false,
Sig: types.NewType("func(?1, ?2, ?3) ?3"), // func kind
Err: obj.Validate(),
}
}
return obj.fn.Info()
}
// Init runs some startup code for this function.
func (obj *LookupDefaultFunc) Init(init *interfaces.Init) error {
if obj.fn == nil {
return fmt.Errorf("function not built correctly")
}
//obj.init = init
return obj.fn.Init(init)
}
// Stream returns the changing values that this func has over time.
func (obj *LookupDefaultFunc) Stream(ctx context.Context) error {
if obj.fn == nil {
return fmt.Errorf("function not built correctly")
}
return obj.fn.Stream(ctx)
}

View File

@@ -1,160 +0,0 @@
// Mgmt
// Copyright (C) 2013-2024+ James Shubin and the project contributors
// Written by James Shubin <james@shubin.ca> and the project contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// Additional permission under GNU GPL version 3 section 7
//
// If you modify this program, or any covered work, by linking or combining it
// with embedded mcl code and modules (and that the embedded mcl code and
// modules which link with this program, contain a copy of their source code in
// the authoritative form) containing parts covered by the terms of any other
// license, the licensors of this program grant you additional permission to
// convey the resulting work. Furthermore, the licensors of this program grant
// the original author, James Shubin, additional permission to update this
// additional permission if he deems it necessary to achieve the goals of this
// additional permission.
package funcs
import (
"context"
"fmt"
"github.com/purpleidea/mgmt/lang/interfaces"
"github.com/purpleidea/mgmt/lang/types"
)
const (
// LookupFuncName is the name this function is registered as.
// This starts with an underscore so that it cannot be used from the
// lexer.
LookupFuncName = "_lookup"
// arg names...
lookupArgNameListOrMap = "listormap"
lookupArgNameIndexOrKey = "indexorkey"
)
func init() {
Register(LookupFuncName, func() interfaces.Func { return &LookupFunc{} }) // must register the func and name
}
var _ interfaces.BuildableFunc = &LookupFunc{} // ensure it meets this expectation
// LookupFunc is a list index or map key lookup function. It does both because
// the current syntax in the parser is identical, so it's convenient to mix the
// two together. This calls out to some of the code in the ListLookupFunc and
// MapLookupFunc implementations. If the index or key for this input doesn't
// exist, then it will return the zero value for that type.
// 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 LookupFunc struct {
Type *types.Type // Kind == List OR Map, that is used as the list/map we lookup in
//init *interfaces.Init
fn interfaces.BuildableFunc // handle to ListLookupFunc or MapLookupFunc
}
// String returns a simple name for this function. This is needed so this struct
// can satisfy the pgraph.Vertex interface.
func (obj *LookupFunc) String() string {
return LookupFuncName
}
// ArgGen returns the Nth arg name for this function.
func (obj *LookupFunc) ArgGen(index int) (string, error) {
seq := []string{lookupArgNameListOrMap, lookupArgNameIndexOrKey}
if l := len(seq); index >= l {
return "", fmt.Errorf("index %d exceeds arg length of %d", index, l)
}
return seq[index], nil
}
// 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 *LookupFunc) 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) < 1 {
return nil, fmt.Errorf("the lookup function needs at least one arg") // actually 2 or 3
}
tListOrMap, exists := typ.Map[typ.Ord[0]]
if !exists || tListOrMap == nil {
return nil, fmt.Errorf("first arg must be specified")
}
if tListOrMap == nil {
return nil, fmt.Errorf("first arg must have a type")
}
if tListOrMap.Kind == types.KindList {
obj.fn = &ListLookupFunc{} // set it
return obj.fn.Build(typ)
}
if tListOrMap.Kind == types.KindMap {
obj.fn = &MapLookupFunc{} // set it
return obj.fn.Build(typ)
}
return nil, fmt.Errorf("we must lookup from either a list or a map")
}
// Validate tells us if the input struct takes a valid form.
func (obj *LookupFunc) Validate() error {
if obj.fn == nil { // build must be run first
return fmt.Errorf("type is still unspecified")
}
return obj.fn.Validate()
}
// Info returns some static info about itself. Build must be called before this
// will return correct data.
func (obj *LookupFunc) Info() *interfaces.Info {
// func(list []?1, index int) ?1
// OR
// func(map map{?1: ?2}, key ?1) ?2
if obj.fn == nil {
return &interfaces.Info{
Pure: true,
Memo: false,
Sig: types.NewType("func(?1, ?2) ?3"), // func kind
Err: obj.Validate(),
}
}
return obj.fn.Info()
}
// Init runs some startup code for this function.
func (obj *LookupFunc) Init(init *interfaces.Init) error {
if obj.fn == nil {
return fmt.Errorf("function not built correctly")
}
//obj.init = init
return obj.fn.Init(init)
}
// Stream returns the changing values that this func has over time.
func (obj *LookupFunc) Stream(ctx context.Context) error {
if obj.fn == nil {
return fmt.Errorf("function not built correctly")
}
return obj.fn.Stream(ctx)
}

View File

@@ -1,233 +0,0 @@
// Mgmt
// Copyright (C) 2013-2024+ James Shubin and the project contributors
// Written by James Shubin <james@shubin.ca> and the project contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// Additional permission under GNU GPL version 3 section 7
//
// If you modify this program, or any covered work, by linking or combining it
// with embedded mcl code and modules (and that the embedded mcl code and
// modules which link with this program, contain a copy of their source code in
// the authoritative form) containing parts covered by the terms of any other
// license, the licensors of this program grant you additional permission to
// convey the resulting work. Furthermore, the licensors of this program grant
// the original author, James Shubin, additional permission to update this
// additional permission if he deems it necessary to achieve the goals of this
// additional permission.
package funcs
import (
"context"
"fmt"
"github.com/purpleidea/mgmt/lang/interfaces"
"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() {
Register(MapLookupDefaultFuncName, func() interfaces.Func { return &MapLookupDefaultFunc{} }) // must register the func and name
}
var _ interfaces.BuildableFunc = &MapLookupDefaultFunc{} // ensure it meets this expectation
// 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
}
}
}

View File

@@ -1,220 +0,0 @@
// Mgmt
// Copyright (C) 2013-2024+ James Shubin and the project contributors
// Written by James Shubin <james@shubin.ca> and the project contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// Additional permission under GNU GPL version 3 section 7
//
// If you modify this program, or any covered work, by linking or combining it
// with embedded mcl code and modules (and that the embedded mcl code and
// modules which link with this program, contain a copy of their source code in
// the authoritative form) containing parts covered by the terms of any other
// license, the licensors of this program grant you additional permission to
// convey the resulting work. Furthermore, the licensors of this program grant
// the original author, James Shubin, additional permission to update this
// additional permission if he deems it necessary to achieve the goals of this
// additional permission.
package funcs
import (
"context"
"fmt"
"github.com/purpleidea/mgmt/lang/interfaces"
"github.com/purpleidea/mgmt/lang/types"
"github.com/purpleidea/mgmt/util/errwrap"
)
const (
// MapLookupFuncName is the name this function is registered as.
MapLookupFuncName = "map_lookup"
// arg names...
mapLookupArgNameMap = "map"
mapLookupArgNameKey = "key"
)
func init() {
Register(MapLookupFuncName, func() interfaces.Func { return &MapLookupFunc{} }) // must register the func and name
}
var _ interfaces.BuildableFunc = &MapLookupFunc{} // ensure it meets this expectation
// MapLookupFunc is a key map lookup function. If you provide a missing key,
// then it will return the zero value for that type.
type MapLookupFunc 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 *MapLookupFunc) String() string {
return MapLookupFuncName
}
// ArgGen returns the Nth arg name for this function.
func (obj *MapLookupFunc) ArgGen(index int) (string, error) {
seq := []string{mapLookupArgNameMap, mapLookupArgNameKey}
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 *MapLookupFunc) 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",
mapLookupArgNameMap, m,
mapLookupArgNameKey, k,
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 *MapLookupFunc) 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) != 2 {
return nil, fmt.Errorf("the maplookup function needs exactly two 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")
}
if err := tMap.Key.Cmp(tKey); err != nil {
return nil, errwrap.Wrapf(err, "key must match map key 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 *MapLookupFunc) 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 *MapLookupFunc) 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 *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().Val.New() // the zero value
var result types.Value
val, exists := m.Lookup(key)
if exists {
result = val
} else {
result = zero
}
// 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

@@ -1,337 +0,0 @@
// Mgmt
// Copyright (C) 2013-2024+ James Shubin and the project contributors
// Written by James Shubin <james@shubin.ca> and the project contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// Additional permission under GNU GPL version 3 section 7
//
// If you modify this program, or any covered work, by linking or combining it
// with embedded mcl code and modules (and that the embedded mcl code and
// modules which link with this program, contain a copy of their source code in
// the authoritative form) containing parts covered by the terms of any other
// license, the licensors of this program grant you additional permission to
// convey the resulting work. Furthermore, the licensors of this program grant
// the original author, James Shubin, additional permission to update this
// additional permission if he deems it necessary to achieve the goals of this
// additional permission.
package funcs
import (
"context"
"fmt"
"github.com/purpleidea/mgmt/lang/interfaces"
"github.com/purpleidea/mgmt/lang/types"
"github.com/purpleidea/mgmt/util/errwrap"
)
const (
// StructLookupFuncName is the name this function is registered as. This
// starts with an underscore so that it cannot be used from the lexer.
StructLookupFuncName = "_struct_lookup"
// arg names...
structLookupArgNameStruct = "struct"
structLookupArgNameField = "field"
)
func init() {
Register(StructLookupFuncName, func() interfaces.Func { return &StructLookupFunc{} }) // must register the func and name
}
var _ interfaces.BuildableFunc = &StructLookupFunc{} // ensure it meets this expectation
// StructLookupFunc is a struct field lookup function.
type StructLookupFunc struct {
Type *types.Type // Kind == Struct, that is used as the struct we lookup
Out *types.Type // type of field we're extracting
built bool // was this function built yet?
init *interfaces.Init
last types.Value // last value received to use for diff
field string
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 *StructLookupFunc) String() string {
return StructLookupFuncName
}
// ArgGen returns the Nth arg name for this function.
func (obj *StructLookupFunc) ArgGen(index int) (string, error) {
seq := []string{structLookupArgNameStruct, structLookupArgNameField}
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 *StructLookupFunc) sig() *types.Type {
st := "?1"
out := "?2"
if obj.Type != nil {
st = obj.Type.String()
}
if obj.Out != nil {
out = obj.Out.String()
}
return types.NewType(fmt.Sprintf(
"func(%s %s, %s str) %s",
structLookupArgNameStruct, st,
structLookupArgNameField,
out,
))
}
// FuncInfer takes partial type and value information from the call site of this
// function so that it can build an appropriate type signature for it. The type
// signature may include unification variables.
func (obj *StructLookupFunc) FuncInfer(partialType *types.Type, partialValues []types.Value) (*types.Type, []*interfaces.UnificationInvariant, error) {
// func(struct ?1, field str) ?2
// This particular function should always get called with a known string
// for the second argument. Without it being known statically, we refuse
// to build this function.
if l := 2; len(partialValues) != l {
return nil, nil, fmt.Errorf("function must have %d args", l)
}
if err := partialValues[1].Type().Cmp(types.TypeStr); err != nil {
return nil, nil, errwrap.Wrapf(err, "function field name must be a str")
}
s := partialValues[1].Str() // must not panic
if s == "" {
return nil, nil, fmt.Errorf("function must not have an empty field name")
}
// This can happen at runtime too, but we save it here for Build()!
obj.field = s // store for later
// Figure out more about the sig if any information is known statically.
if len(partialType.Ord) > 0 && partialType.Map[partialType.Ord[0]] != nil {
obj.Type = partialType.Map[partialType.Ord[0]] // assume this
if obj.Type.Kind == types.KindStruct && obj.Type.Map != nil {
if typ, exists := obj.Type.Map[s]; exists {
obj.Out = typ
}
}
}
// This isn't precise enough because we must guarantee that the field is
// in the struct and that ?1 is actually a struct, but that's okay it is
// something that we'll verify at build time!
return obj.sig(), []*interfaces.UnificationInvariant{}, nil
}
// 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 *StructLookupFunc) 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) != 2 {
return nil, fmt.Errorf("the structlookup function needs exactly two 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")
}
tStruct, exists := typ.Map[typ.Ord[0]]
if !exists || tStruct == nil {
return nil, fmt.Errorf("first arg must be specified")
}
tField, exists := typ.Map[typ.Ord[1]]
if !exists || tField == nil {
return nil, fmt.Errorf("second arg must be specified")
}
if err := tField.Cmp(types.TypeStr); err != nil {
return nil, errwrap.Wrapf(err, "field must be an str")
}
// NOTE: We actually don't know which field this is yet, only its type!
// We cached the discovered field during Infer(), but it turns out it's
// not actually necessary for us to know it to build the struct. It is
// needed to make sure the lossy Infer unification variables are right.
if tStruct.Kind != types.KindStruct {
return nil, fmt.Errorf("first arg must be of kind struct, got: %s", tStruct.Kind)
}
if obj.field == "" {
// programming error
return nil, fmt.Errorf("did not infer correctly")
}
ix := -1 // not found
for i, x := range tStruct.Ord {
if x != obj.field {
continue
}
// found
if ix != -1 {
// programming error
return nil, fmt.Errorf("duplicate field found")
}
ix = i // found it here!
//break // keep checking for extra safety
}
if ix == -1 {
return nil, fmt.Errorf("field %s was not found in struct", obj.field)
}
tF, exists := tStruct.Map[tStruct.Ord[ix]]
if !exists {
return nil, fmt.Errorf("field %s was not found in struct", obj.field)
}
// The return value must match the type of the field we're pulling out!
if err := typ.Out.Cmp(tF); err != nil {
return nil, fmt.Errorf("field %s type error: %+v", obj.field, err)
}
obj.Type = tStruct // struct type
obj.Out = typ.Out // type of return value
obj.built = true
return obj.sig(), nil
}
// Copy is implemented so that the obj.field value is not lost if we copy this
// function. That value is learned during FuncInfer, and previously would have
// been lost by the time we used it in Build.
func (obj *StructLookupFunc) Copy() interfaces.Func {
return &StructLookupFunc{
Type: obj.Type, // don't copy because we use this after unification
Out: obj.Out,
built: obj.built,
init: obj.init, // likely gets overwritten anyways
field: obj.field, // this we really need!
}
}
// Validate tells us if the input struct takes a valid form.
func (obj *StructLookupFunc) Validate() error {
if !obj.built {
return fmt.Errorf("function wasn't built yet")
}
if obj.Type == nil { // build must be run first
return fmt.Errorf("type is still unspecified")
}
if obj.Type.Kind != types.KindStruct {
return fmt.Errorf("type must be a kind of struct")
}
if obj.Out == nil {
return fmt.Errorf("return type must be specified")
}
for _, t := range obj.Type.Map {
if obj.Out.Cmp(t) == nil {
return nil // found at least one match
}
}
return fmt.Errorf("return type is not in the list of available struct fields")
}
// Info returns some static info about itself. Build must be called before this
// will return correct data.
func (obj *StructLookupFunc) Info() *interfaces.Info {
// Since this function implements FuncInfer we want sig to return nil to
// avoid an accidental return of unification variables when we should be
// getting them from FuncInfer, and not from here. (During unification!)
var sig *types.Type
if obj.built {
sig = obj.sig() // helper
}
return &interfaces.Info{
Pure: true,
Memo: false,
Sig: sig,
Err: obj.Validate(),
}
}
// Init runs some startup code for this function.
func (obj *StructLookupFunc) Init(init *interfaces.Init) error {
obj.init = init
return nil
}
// Stream returns the changing values that this func has over time.
func (obj *StructLookupFunc) 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
st := (input.Struct()[structLookupArgNameStruct]).(*types.StructValue)
field := input.Struct()[structLookupArgNameField].Str()
if field == "" {
return fmt.Errorf("received empty field")
}
if obj.field == "" {
// This can happen at compile time too. Bonus!
obj.field = field // store first field
}
if field != obj.field {
return fmt.Errorf("input field changed from: `%s`, to: `%s`", obj.field, field)
}
result, exists := st.Lookup(obj.field)
if !exists {
return fmt.Errorf("could not lookup field: `%s` in struct", field)
}
// 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

@@ -1,316 +0,0 @@
// Mgmt
// Copyright (C) 2013-2024+ James Shubin and the project contributors
// Written by James Shubin <james@shubin.ca> and the project contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// Additional permission under GNU GPL version 3 section 7
//
// If you modify this program, or any covered work, by linking or combining it
// with embedded mcl code and modules (and that the embedded mcl code and
// modules which link with this program, contain a copy of their source code in
// the authoritative form) containing parts covered by the terms of any other
// license, the licensors of this program grant you additional permission to
// convey the resulting work. Furthermore, the licensors of this program grant
// the original author, James Shubin, additional permission to update this
// additional permission if he deems it necessary to achieve the goals of this
// additional permission.
package funcs
import (
"context"
"fmt"
"github.com/purpleidea/mgmt/lang/interfaces"
"github.com/purpleidea/mgmt/lang/types"
"github.com/purpleidea/mgmt/util/errwrap"
)
const (
// StructLookupOptionalFuncName is the name this function is registered
// as. This starts with an underscore so that it cannot be used from the
// lexer.
StructLookupOptionalFuncName = "_struct_lookup_optional"
// arg names...
structLookupOptionalArgNameStruct = "struct"
structLookupOptionalArgNameField = "field"
structLookupOptionalArgNameOptional = "optional"
)
func init() {
Register(StructLookupOptionalFuncName, func() interfaces.Func { return &StructLookupOptionalFunc{} }) // must register the func and name
}
var _ interfaces.InferableFunc = &StructLookupOptionalFunc{} // ensure it meets this expectation
// StructLookupOptionalFunc is a struct field lookup function. It does a special
// trick in that it will unify on a struct that doesn't have the specified field
// in it, but in that case, it will always return the optional value. This is a
// bit different from the "default" mechanism that is used by list and map
// lookup functions.
type StructLookupOptionalFunc struct {
Type *types.Type // Kind == Struct, that is used as the struct we lookup
Out *types.Type // type of field we're extracting (also the type of optional)
built bool // was this function built yet?
init *interfaces.Init
last types.Value // last value received to use for diff
field string
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 *StructLookupOptionalFunc) String() string {
return StructLookupOptionalFuncName
}
// ArgGen returns the Nth arg name for this function.
func (obj *StructLookupOptionalFunc) ArgGen(index int) (string, error) {
seq := []string{structLookupOptionalArgNameStruct, structLookupOptionalArgNameField, structLookupOptionalArgNameOptional}
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 *StructLookupOptionalFunc) sig() *types.Type {
st := "?1"
out := "?2"
if obj.Type != nil {
st = obj.Type.String()
}
if obj.Out != nil {
out = obj.Out.String()
}
return types.NewType(fmt.Sprintf(
"func(%s %s, %s str, %s %s) %s",
structLookupOptionalArgNameStruct, st,
structLookupOptionalArgNameField,
structLookupOptionalArgNameOptional, out,
out,
))
}
// FuncInfer takes partial type and value information from the call site of this
// function so that it can build an appropriate type signature for it. The type
// signature may include unification variables.
func (obj *StructLookupOptionalFunc) FuncInfer(partialType *types.Type, partialValues []types.Value) (*types.Type, []*interfaces.UnificationInvariant, error) {
// func(struct ?1, field str, optional ?2) ?2
// This particular function should always get called with a known string
// for the second argument. Without it being known statically, we refuse
// to build this function.
if l := 3; len(partialValues) != l {
return nil, nil, fmt.Errorf("function must have %d args", l)
}
if err := partialValues[1].Type().Cmp(types.TypeStr); err != nil {
return nil, nil, errwrap.Wrapf(err, "function field name must be a str")
}
s := partialValues[1].Str() // must not panic
if s == "" {
return nil, nil, fmt.Errorf("function must not have an empty field name")
}
// This can happen at runtime too, but we save it here for Build()!
//obj.field = s // don't store for this optional lookup version!
// Figure out more about the sig if any information is known statically.
if len(partialType.Ord) > 0 && partialType.Map[partialType.Ord[0]] != nil {
obj.Type = partialType.Map[partialType.Ord[0]] // assume this
if obj.Type.Kind == types.KindStruct && obj.Type.Map != nil {
if typ, exists := obj.Type.Map[s]; exists {
obj.Out = typ
}
}
}
// This isn't precise enough because we must guarantee that the field is
// in the struct and that ?1 is actually a struct, but that's okay it is
// something that we'll verify at build time! (Or skip it for optional!)
return obj.sig(), []*interfaces.UnificationInvariant{}, nil
}
// 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 *StructLookupOptionalFunc) 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 structlookup 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")
}
tStruct, exists := typ.Map[typ.Ord[0]]
if !exists || tStruct == nil {
return nil, fmt.Errorf("first arg must be specified")
}
tField, exists := typ.Map[typ.Ord[1]]
if !exists || tField == nil {
return nil, fmt.Errorf("second arg must be specified")
}
if err := tField.Cmp(types.TypeStr); err != nil {
return nil, errwrap.Wrapf(err, "field must be an str")
}
tOptional, exists := typ.Map[typ.Ord[2]]
if !exists || tOptional == nil {
return nil, fmt.Errorf("third arg must be specified")
}
if err := tOptional.Cmp(typ.Out); err != nil {
return nil, errwrap.Wrapf(err, "optional arg must match return type")
}
// NOTE: We actually don't know which field this is yet, only its type!
// We don't care, because that's a runtime issue and doesn't need to be
// our problem as long as this is a struct. The only optimization we can
// add is to know statically if we're returning the optional value.
if tStruct.Kind != types.KindStruct {
return nil, fmt.Errorf("first arg must be of kind struct, got: %s", tStruct.Kind)
}
obj.Type = tStruct // struct type
obj.Out = typ.Out // type of return value
obj.built = true
return obj.sig(), nil
}
// Validate tells us if the input struct takes a valid form.
func (obj *StructLookupOptionalFunc) Validate() error {
if !obj.built {
return fmt.Errorf("function wasn't built yet")
}
if obj.Type == nil { // build must be run first
return fmt.Errorf("type is still unspecified")
}
if obj.Type.Kind != types.KindStruct {
return fmt.Errorf("type must be a kind of struct")
}
if obj.Out == nil {
return fmt.Errorf("return type must be specified")
}
// TODO: can we do better and validate more aspects here?
return nil
}
// Info returns some static info about itself. Build must be called before this
// will return correct data.
func (obj *StructLookupOptionalFunc) Info() *interfaces.Info {
// Since this function implements FuncInfer we want sig to return nil to
// avoid an accidental return of unification variables when we should be
// getting them from FuncInfer, and not from here. (During unification!)
var sig *types.Type
if obj.built {
sig = obj.sig() // helper
}
return &interfaces.Info{
Pure: true,
Memo: false,
Sig: sig,
Err: obj.Validate(),
}
}
// Init runs some startup code for this function.
func (obj *StructLookupOptionalFunc) Init(init *interfaces.Init) error {
obj.init = init
return nil
}
// Stream returns the changing values that this func has over time.
func (obj *StructLookupOptionalFunc) 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
st := (input.Struct()[structLookupOptionalArgNameStruct]).(*types.StructValue)
field := input.Struct()[structLookupOptionalArgNameField].Str()
optional := input.Struct()[structLookupOptionalArgNameOptional]
if field == "" {
return fmt.Errorf("received empty field")
}
if obj.field == "" {
// This can happen at compile time too. Bonus!
obj.field = field // store first field
}
if field != obj.field {
return fmt.Errorf("input field changed from: `%s`, to: `%s`", obj.field, field)
}
// We know the result of this lookup statically at
// compile time, but for simplicity we check each time
// here anyways. Maybe one day there will be a fancy
// reason why this might vary over time.
var result types.Value
val, exists := st.Lookup(obj.field)
if exists {
result = val
} else {
result = optional
}
// 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
}
}
}