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

@@ -27,21 +27,20 @@
// additional permission if he deems it necessary to achieve the goals of this // additional permission if he deems it necessary to achieve the goals of this
// additional permission. // additional permission.
package funcs package core
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/purpleidea/mgmt/lang/funcs"
"github.com/purpleidea/mgmt/lang/interfaces" "github.com/purpleidea/mgmt/lang/interfaces"
"github.com/purpleidea/mgmt/lang/types" "github.com/purpleidea/mgmt/lang/types"
) )
const ( const (
// ContainsFuncName is the name this function is registered as. This // ContainsFuncName is the name this function is registered as.
// starts with an underscore so that it cannot be used from the lexer. ContainsFuncName = funcs.ContainsFuncName
// XXX: change to _contains and add syntax in the lexer/parser
ContainsFuncName = "contains"
// arg names... // arg names...
containsArgNameNeedle = "needle" containsArgNameNeedle = "needle"
@@ -49,7 +48,7 @@ const (
) )
func init() { func init() {
Register(ContainsFuncName, func() interfaces.Func { return &ContainsFunc{} }) // must register the func and name funcs.Register(ContainsFuncName, func() interfaces.Func { return &ContainsFunc{} }) // must register the func and name
} }
var _ interfaces.BuildableFunc = &ContainsFunc{} // ensure it meets this expectation var _ interfaces.BuildableFunc = &ContainsFunc{} // ensure it meets this expectation

View File

@@ -27,12 +27,13 @@
// additional permission if he deems it necessary to achieve the goals of this // additional permission if he deems it necessary to achieve the goals of this
// additional permission. // additional permission.
package funcs // TODO: should this be in its own individual package? package core // TODO: should this be in its own individual package?
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/purpleidea/mgmt/lang/funcs"
"github.com/purpleidea/mgmt/lang/interfaces" "github.com/purpleidea/mgmt/lang/interfaces"
"github.com/purpleidea/mgmt/lang/types" "github.com/purpleidea/mgmt/lang/types"
) )
@@ -48,7 +49,7 @@ const (
) )
func init() { func init() {
Register(HistoryFuncName, func() interfaces.Func { return &HistoryFunc{} }) // must register the func and name funcs.Register(HistoryFuncName, func() interfaces.Func { return &HistoryFunc{} }) // must register the func and name
} }
var _ interfaces.BuildableFunc = &HistoryFunc{} // ensure it meets this expectation var _ interfaces.BuildableFunc = &HistoryFunc{} // ensure it meets this expectation

View File

@@ -27,13 +27,14 @@
// additional permission if he deems it necessary to achieve the goals of this // additional permission if he deems it necessary to achieve the goals of this
// additional permission. // additional permission.
package funcs package core
import ( import (
"context" "context"
"fmt" "fmt"
"math" "math"
"github.com/purpleidea/mgmt/lang/funcs"
"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"
@@ -49,7 +50,7 @@ const (
) )
func init() { func init() {
Register(ListLookupFuncName, func() interfaces.Func { return &ListLookupFunc{} }) // must register the func and name funcs.Register(ListLookupFuncName, func() interfaces.Func { return &ListLookupFunc{} }) // must register the func and name
} }
var _ interfaces.BuildableFunc = &ListLookupFunc{} // ensure it meets this expectation var _ interfaces.BuildableFunc = &ListLookupFunc{} // ensure it meets this expectation

View File

@@ -27,13 +27,14 @@
// additional permission if he deems it necessary to achieve the goals of this // additional permission if he deems it necessary to achieve the goals of this
// additional permission. // additional permission.
package funcs package core
import ( import (
"context" "context"
"fmt" "fmt"
"math" "math"
"github.com/purpleidea/mgmt/lang/funcs"
"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"
@@ -50,7 +51,7 @@ const (
) )
func init() { func init() {
Register(ListLookupDefaultFuncName, func() interfaces.Func { return &ListLookupDefaultFunc{} }) // must register the func and name funcs.Register(ListLookupDefaultFuncName, func() interfaces.Func { return &ListLookupDefaultFunc{} }) // must register the func and name
} }
var _ interfaces.BuildableFunc = &ListLookupDefaultFunc{} // ensure it meets this expectation var _ interfaces.BuildableFunc = &ListLookupDefaultFunc{} // ensure it meets this expectation

View File

@@ -27,12 +27,13 @@
// additional permission if he deems it necessary to achieve the goals of this // additional permission if he deems it necessary to achieve the goals of this
// additional permission. // additional permission.
package funcs package core
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/purpleidea/mgmt/lang/funcs"
"github.com/purpleidea/mgmt/lang/interfaces" "github.com/purpleidea/mgmt/lang/interfaces"
"github.com/purpleidea/mgmt/lang/types" "github.com/purpleidea/mgmt/lang/types"
) )
@@ -41,7 +42,7 @@ const (
// LookupFuncName is the name this function is registered as. // LookupFuncName is the name this function is registered as.
// This starts with an underscore so that it cannot be used from the // This starts with an underscore so that it cannot be used from the
// lexer. // lexer.
LookupFuncName = "_lookup" LookupFuncName = funcs.LookupFuncName
// arg names... // arg names...
lookupArgNameListOrMap = "listormap" lookupArgNameListOrMap = "listormap"
@@ -49,7 +50,7 @@ const (
) )
func init() { func init() {
Register(LookupFuncName, func() interfaces.Func { return &LookupFunc{} }) // must register the func and name funcs.Register(LookupFuncName, func() interfaces.Func { return &LookupFunc{} }) // must register the func and name
} }
var _ interfaces.BuildableFunc = &LookupFunc{} // ensure it meets this expectation var _ interfaces.BuildableFunc = &LookupFunc{} // ensure it meets this expectation

View File

@@ -27,12 +27,13 @@
// additional permission if he deems it necessary to achieve the goals of this // additional permission if he deems it necessary to achieve the goals of this
// additional permission. // additional permission.
package funcs package core
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/purpleidea/mgmt/lang/funcs"
"github.com/purpleidea/mgmt/lang/interfaces" "github.com/purpleidea/mgmt/lang/interfaces"
"github.com/purpleidea/mgmt/lang/types" "github.com/purpleidea/mgmt/lang/types"
) )
@@ -41,7 +42,7 @@ const (
// LookupDefaultFuncName is the name this function is registered as. // LookupDefaultFuncName is the name this function is registered as.
// This starts with an underscore so that it cannot be used from the // This starts with an underscore so that it cannot be used from the
// lexer. // lexer.
LookupDefaultFuncName = "_lookup_default" LookupDefaultFuncName = funcs.LookupDefaultFuncName
// arg names... // arg names...
lookupDefaultArgNameListOrMap = "listormap" lookupDefaultArgNameListOrMap = "listormap"
@@ -50,7 +51,7 @@ const (
) )
func init() { func init() {
Register(LookupDefaultFuncName, func() interfaces.Func { return &LookupDefaultFunc{} }) // must register the func and name funcs.Register(LookupDefaultFuncName, func() interfaces.Func { return &LookupDefaultFunc{} }) // must register the func and name
} }
var _ interfaces.BuildableFunc = &LookupDefaultFunc{} // ensure it meets this expectation var _ interfaces.BuildableFunc = &LookupDefaultFunc{} // ensure it meets this expectation

View File

@@ -27,12 +27,13 @@
// additional permission if he deems it necessary to achieve the goals of this // additional permission if he deems it necessary to achieve the goals of this
// additional permission. // additional permission.
package funcs package core
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/purpleidea/mgmt/lang/funcs"
"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"
@@ -48,7 +49,7 @@ const (
) )
func init() { func init() {
Register(MapLookupFuncName, func() interfaces.Func { return &MapLookupFunc{} }) // must register the func and name funcs.Register(MapLookupFuncName, func() interfaces.Func { return &MapLookupFunc{} }) // must register the func and name
} }
var _ interfaces.BuildableFunc = &MapLookupFunc{} // ensure it meets this expectation var _ interfaces.BuildableFunc = &MapLookupFunc{} // ensure it meets this expectation

View File

@@ -27,12 +27,13 @@
// additional permission if he deems it necessary to achieve the goals of this // additional permission if he deems it necessary to achieve the goals of this
// additional permission. // additional permission.
package funcs package core
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/purpleidea/mgmt/lang/funcs"
"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"
@@ -49,7 +50,7 @@ const (
) )
func init() { func init() {
Register(MapLookupDefaultFuncName, func() interfaces.Func { return &MapLookupDefaultFunc{} }) // must register the func and name funcs.Register(MapLookupDefaultFuncName, func() interfaces.Func { return &MapLookupDefaultFunc{} }) // must register the func and name
} }
var _ interfaces.BuildableFunc = &MapLookupDefaultFunc{} // ensure it meets this expectation var _ interfaces.BuildableFunc = &MapLookupDefaultFunc{} // ensure it meets this expectation

View File

@@ -27,12 +27,13 @@
// additional permission if he deems it necessary to achieve the goals of this // additional permission if he deems it necessary to achieve the goals of this
// additional permission. // additional permission.
package funcs package core
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/purpleidea/mgmt/lang/funcs"
"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"
@@ -41,7 +42,7 @@ import (
const ( const (
// StructLookupFuncName is the name this function is registered as. This // StructLookupFuncName is the name this function is registered as. This
// starts with an underscore so that it cannot be used from the lexer. // starts with an underscore so that it cannot be used from the lexer.
StructLookupFuncName = "_struct_lookup" StructLookupFuncName = funcs.StructLookupFuncName
// arg names... // arg names...
structLookupArgNameStruct = "struct" structLookupArgNameStruct = "struct"
@@ -49,7 +50,7 @@ const (
) )
func init() { func init() {
Register(StructLookupFuncName, func() interfaces.Func { return &StructLookupFunc{} }) // must register the func and name funcs.Register(StructLookupFuncName, func() interfaces.Func { return &StructLookupFunc{} }) // must register the func and name
} }
var _ interfaces.BuildableFunc = &StructLookupFunc{} // ensure it meets this expectation var _ interfaces.BuildableFunc = &StructLookupFunc{} // ensure it meets this expectation

View File

@@ -27,12 +27,13 @@
// additional permission if he deems it necessary to achieve the goals of this // additional permission if he deems it necessary to achieve the goals of this
// additional permission. // additional permission.
package funcs package core
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/purpleidea/mgmt/lang/funcs"
"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"
@@ -42,7 +43,7 @@ const (
// StructLookupOptionalFuncName is the name this function is registered // StructLookupOptionalFuncName is the name this function is registered
// as. This starts with an underscore so that it cannot be used from the // as. This starts with an underscore so that it cannot be used from the
// lexer. // lexer.
StructLookupOptionalFuncName = "_struct_lookup_optional" StructLookupOptionalFuncName = funcs.StructLookupOptionalFuncName
// arg names... // arg names...
structLookupOptionalArgNameStruct = "struct" structLookupOptionalArgNameStruct = "struct"
@@ -51,7 +52,7 @@ const (
) )
func init() { func init() {
Register(StructLookupOptionalFuncName, func() interfaces.Func { return &StructLookupOptionalFunc{} }) // must register the func and name funcs.Register(StructLookupOptionalFuncName, func() interfaces.Func { return &StructLookupOptionalFunc{} }) // must register the func and name
} }
var _ interfaces.InferableFunc = &StructLookupOptionalFunc{} // ensure it meets this expectation var _ interfaces.InferableFunc = &StructLookupOptionalFunc{} // ensure it meets this expectation

View File

@@ -61,6 +61,28 @@ const (
// is listed here because it needs a well-known name that can be used by // is listed here because it needs a well-known name that can be used by
// the string interpolation code. // the string interpolation code.
ConcatFuncName = "concat" 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 // registeredFuncs is a global map of all possible funcs which can be used. You