hclparser: avoid unnecessary allocations in init

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
Tonis Tiigi 2024-07-15 13:35:04 -07:00
parent b92bfb53d2
commit 85cf3bace9
No known key found for this signature in database
GPG Key ID: AFA9DE5F8AB7AF39
2 changed files with 162 additions and 148 deletions

View File

@ -1,6 +1,7 @@
package hclparser package hclparser
import ( import (
"errors"
"time" "time"
"github.com/hashicorp/go-cty-funcs/cidr" "github.com/hashicorp/go-cty-funcs/cidr"
@ -9,174 +10,187 @@ import (
"github.com/hashicorp/go-cty-funcs/uuid" "github.com/hashicorp/go-cty-funcs/uuid"
"github.com/hashicorp/hcl/v2/ext/tryfunc" "github.com/hashicorp/hcl/v2/ext/tryfunc"
"github.com/hashicorp/hcl/v2/ext/typeexpr" "github.com/hashicorp/hcl/v2/ext/typeexpr"
"github.com/pkg/errors"
"github.com/zclconf/go-cty/cty" "github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function" "github.com/zclconf/go-cty/cty/function"
"github.com/zclconf/go-cty/cty/function/stdlib" "github.com/zclconf/go-cty/cty/function/stdlib"
) )
var stdlibFunctions = map[string]function.Function{ type funcDef struct {
"absolute": stdlib.AbsoluteFunc, name string
"add": stdlib.AddFunc, fn function.Function
"and": stdlib.AndFunc, factory func() function.Function
"base64decode": encoding.Base64DecodeFunc, }
"base64encode": encoding.Base64EncodeFunc,
"bcrypt": crypto.BcryptFunc, var stdlibFunctions = []funcDef{
"byteslen": stdlib.BytesLenFunc, {name: "absolute", fn: stdlib.AbsoluteFunc},
"bytesslice": stdlib.BytesSliceFunc, {name: "add", fn: stdlib.AddFunc},
"can": tryfunc.CanFunc, {name: "and", fn: stdlib.AndFunc},
"ceil": stdlib.CeilFunc, {name: "base64decode", fn: encoding.Base64DecodeFunc},
"chomp": stdlib.ChompFunc, {name: "base64encode", fn: encoding.Base64EncodeFunc},
"chunklist": stdlib.ChunklistFunc, {name: "bcrypt", fn: crypto.BcryptFunc},
"cidrhost": cidr.HostFunc, {name: "byteslen", fn: stdlib.BytesLenFunc},
"cidrnetmask": cidr.NetmaskFunc, {name: "bytesslice", fn: stdlib.BytesSliceFunc},
"cidrsubnet": cidr.SubnetFunc, {name: "can", fn: tryfunc.CanFunc},
"cidrsubnets": cidr.SubnetsFunc, {name: "ceil", fn: stdlib.CeilFunc},
"coalesce": stdlib.CoalesceFunc, {name: "chomp", fn: stdlib.ChompFunc},
"coalescelist": stdlib.CoalesceListFunc, {name: "chunklist", fn: stdlib.ChunklistFunc},
"compact": stdlib.CompactFunc, {name: "cidrhost", fn: cidr.HostFunc},
"concat": stdlib.ConcatFunc, {name: "cidrnetmask", fn: cidr.NetmaskFunc},
"contains": stdlib.ContainsFunc, {name: "cidrsubnet", fn: cidr.SubnetFunc},
"convert": typeexpr.ConvertFunc, {name: "cidrsubnets", fn: cidr.SubnetsFunc},
"csvdecode": stdlib.CSVDecodeFunc, {name: "coalesce", fn: stdlib.CoalesceFunc},
"distinct": stdlib.DistinctFunc, {name: "coalescelist", fn: stdlib.CoalesceListFunc},
"divide": stdlib.DivideFunc, {name: "compact", fn: stdlib.CompactFunc},
"element": stdlib.ElementFunc, {name: "concat", fn: stdlib.ConcatFunc},
"equal": stdlib.EqualFunc, {name: "contains", fn: stdlib.ContainsFunc},
"flatten": stdlib.FlattenFunc, {name: "convert", fn: typeexpr.ConvertFunc},
"floor": stdlib.FloorFunc, {name: "csvdecode", fn: stdlib.CSVDecodeFunc},
"format": stdlib.FormatFunc, {name: "distinct", fn: stdlib.DistinctFunc},
"formatdate": stdlib.FormatDateFunc, {name: "divide", fn: stdlib.DivideFunc},
"formatlist": stdlib.FormatListFunc, {name: "element", fn: stdlib.ElementFunc},
"greaterthan": stdlib.GreaterThanFunc, {name: "equal", fn: stdlib.EqualFunc},
"greaterthanorequalto": stdlib.GreaterThanOrEqualToFunc, {name: "flatten", fn: stdlib.FlattenFunc},
"hasindex": stdlib.HasIndexFunc, {name: "floor", fn: stdlib.FloorFunc},
"indent": stdlib.IndentFunc, {name: "format", fn: stdlib.FormatFunc},
"index": stdlib.IndexFunc, {name: "formatdate", fn: stdlib.FormatDateFunc},
"indexof": indexOfFunc, {name: "formatlist", fn: stdlib.FormatListFunc},
"int": stdlib.IntFunc, {name: "greaterthan", fn: stdlib.GreaterThanFunc},
"join": stdlib.JoinFunc, {name: "greaterthanorequalto", fn: stdlib.GreaterThanOrEqualToFunc},
"jsondecode": stdlib.JSONDecodeFunc, {name: "hasindex", fn: stdlib.HasIndexFunc},
"jsonencode": stdlib.JSONEncodeFunc, {name: "indent", fn: stdlib.IndentFunc},
"keys": stdlib.KeysFunc, {name: "index", fn: stdlib.IndexFunc},
"length": stdlib.LengthFunc, {name: "indexof", factory: indexOfFunc},
"lessthan": stdlib.LessThanFunc, {name: "int", fn: stdlib.IntFunc},
"lessthanorequalto": stdlib.LessThanOrEqualToFunc, {name: "join", fn: stdlib.JoinFunc},
"log": stdlib.LogFunc, {name: "jsondecode", fn: stdlib.JSONDecodeFunc},
"lookup": stdlib.LookupFunc, {name: "jsonencode", fn: stdlib.JSONEncodeFunc},
"lower": stdlib.LowerFunc, {name: "keys", fn: stdlib.KeysFunc},
"max": stdlib.MaxFunc, {name: "length", fn: stdlib.LengthFunc},
"md5": crypto.Md5Func, {name: "lessthan", fn: stdlib.LessThanFunc},
"merge": stdlib.MergeFunc, {name: "lessthanorequalto", fn: stdlib.LessThanOrEqualToFunc},
"min": stdlib.MinFunc, {name: "log", fn: stdlib.LogFunc},
"modulo": stdlib.ModuloFunc, {name: "lookup", fn: stdlib.LookupFunc},
"multiply": stdlib.MultiplyFunc, {name: "lower", fn: stdlib.LowerFunc},
"negate": stdlib.NegateFunc, {name: "max", fn: stdlib.MaxFunc},
"not": stdlib.NotFunc, {name: "md5", fn: crypto.Md5Func},
"notequal": stdlib.NotEqualFunc, {name: "merge", fn: stdlib.MergeFunc},
"or": stdlib.OrFunc, {name: "min", fn: stdlib.MinFunc},
"parseint": stdlib.ParseIntFunc, {name: "modulo", fn: stdlib.ModuloFunc},
"pow": stdlib.PowFunc, {name: "multiply", fn: stdlib.MultiplyFunc},
"range": stdlib.RangeFunc, {name: "negate", fn: stdlib.NegateFunc},
"regex_replace": stdlib.RegexReplaceFunc, {name: "not", fn: stdlib.NotFunc},
"regex": stdlib.RegexFunc, {name: "notequal", fn: stdlib.NotEqualFunc},
"regexall": stdlib.RegexAllFunc, {name: "or", fn: stdlib.OrFunc},
"replace": stdlib.ReplaceFunc, {name: "parseint", fn: stdlib.ParseIntFunc},
"reverse": stdlib.ReverseFunc, {name: "pow", fn: stdlib.PowFunc},
"reverselist": stdlib.ReverseListFunc, {name: "range", fn: stdlib.RangeFunc},
"rsadecrypt": crypto.RsaDecryptFunc, {name: "regex_replace", fn: stdlib.RegexReplaceFunc},
"sethaselement": stdlib.SetHasElementFunc, {name: "regex", fn: stdlib.RegexFunc},
"setintersection": stdlib.SetIntersectionFunc, {name: "regexall", fn: stdlib.RegexAllFunc},
"setproduct": stdlib.SetProductFunc, {name: "replace", fn: stdlib.ReplaceFunc},
"setsubtract": stdlib.SetSubtractFunc, {name: "reverse", fn: stdlib.ReverseFunc},
"setsymmetricdifference": stdlib.SetSymmetricDifferenceFunc, {name: "reverselist", fn: stdlib.ReverseListFunc},
"setunion": stdlib.SetUnionFunc, {name: "rsadecrypt", fn: crypto.RsaDecryptFunc},
"sha1": crypto.Sha1Func, {name: "sethaselement", fn: stdlib.SetHasElementFunc},
"sha256": crypto.Sha256Func, {name: "setintersection", fn: stdlib.SetIntersectionFunc},
"sha512": crypto.Sha512Func, {name: "setproduct", fn: stdlib.SetProductFunc},
"signum": stdlib.SignumFunc, {name: "setsubtract", fn: stdlib.SetSubtractFunc},
"slice": stdlib.SliceFunc, {name: "setsymmetricdifference", fn: stdlib.SetSymmetricDifferenceFunc},
"sort": stdlib.SortFunc, {name: "setunion", fn: stdlib.SetUnionFunc},
"split": stdlib.SplitFunc, {name: "sha1", fn: crypto.Sha1Func},
"strlen": stdlib.StrlenFunc, {name: "sha256", fn: crypto.Sha256Func},
"substr": stdlib.SubstrFunc, {name: "sha512", fn: crypto.Sha512Func},
"subtract": stdlib.SubtractFunc, {name: "signum", fn: stdlib.SignumFunc},
"timeadd": stdlib.TimeAddFunc, {name: "slice", fn: stdlib.SliceFunc},
"timestamp": timestampFunc, {name: "sort", fn: stdlib.SortFunc},
"title": stdlib.TitleFunc, {name: "split", fn: stdlib.SplitFunc},
"trim": stdlib.TrimFunc, {name: "strlen", fn: stdlib.StrlenFunc},
"trimprefix": stdlib.TrimPrefixFunc, {name: "substr", fn: stdlib.SubstrFunc},
"trimspace": stdlib.TrimSpaceFunc, {name: "subtract", fn: stdlib.SubtractFunc},
"trimsuffix": stdlib.TrimSuffixFunc, {name: "timeadd", fn: stdlib.TimeAddFunc},
"try": tryfunc.TryFunc, {name: "timestamp", factory: timestampFunc},
"upper": stdlib.UpperFunc, {name: "title", fn: stdlib.TitleFunc},
"urlencode": encoding.URLEncodeFunc, {name: "trim", fn: stdlib.TrimFunc},
"uuidv4": uuid.V4Func, {name: "trimprefix", fn: stdlib.TrimPrefixFunc},
"uuidv5": uuid.V5Func, {name: "trimspace", fn: stdlib.TrimSpaceFunc},
"values": stdlib.ValuesFunc, {name: "trimsuffix", fn: stdlib.TrimSuffixFunc},
"zipmap": stdlib.ZipmapFunc, {name: "try", fn: tryfunc.TryFunc},
{name: "upper", fn: stdlib.UpperFunc},
{name: "urlencode", fn: encoding.URLEncodeFunc},
{name: "uuidv4", fn: uuid.V4Func},
{name: "uuidv5", fn: uuid.V5Func},
{name: "values", fn: stdlib.ValuesFunc},
{name: "zipmap", fn: stdlib.ZipmapFunc},
} }
// indexOfFunc constructs a function that finds the element index for a given // indexOfFunc constructs a function that finds the element index for a given
// value in a list. // value in a list.
var indexOfFunc = function.New(&function.Spec{ func indexOfFunc() function.Function {
Params: []function.Parameter{ return function.New(&function.Spec{
{ Params: []function.Parameter{
Name: "list", {
Type: cty.DynamicPseudoType, Name: "list",
Type: cty.DynamicPseudoType,
},
{
Name: "value",
Type: cty.DynamicPseudoType,
},
}, },
{ Type: function.StaticReturnType(cty.Number),
Name: "value", Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
Type: cty.DynamicPseudoType, if !(args[0].Type().IsListType() || args[0].Type().IsTupleType()) {
}, return cty.NilVal, errors.New("argument must be a list or tuple")
},
Type: function.StaticReturnType(cty.Number),
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
if !(args[0].Type().IsListType() || args[0].Type().IsTupleType()) {
return cty.NilVal, errors.New("argument must be a list or tuple")
}
if !args[0].IsKnown() {
return cty.UnknownVal(cty.Number), nil
}
if args[0].LengthInt() == 0 { // Easy path
return cty.NilVal, errors.New("cannot search an empty list")
}
for it := args[0].ElementIterator(); it.Next(); {
i, v := it.Element()
eq, err := stdlib.Equal(v, args[1])
if err != nil {
return cty.NilVal, err
} }
if !eq.IsKnown() {
if !args[0].IsKnown() {
return cty.UnknownVal(cty.Number), nil return cty.UnknownVal(cty.Number), nil
} }
if eq.True() {
return i, nil
}
}
return cty.NilVal, errors.New("item not found")
}, if args[0].LengthInt() == 0 { // Easy path
}) return cty.NilVal, errors.New("cannot search an empty list")
}
for it := args[0].ElementIterator(); it.Next(); {
i, v := it.Element()
eq, err := stdlib.Equal(v, args[1])
if err != nil {
return cty.NilVal, err
}
if !eq.IsKnown() {
return cty.UnknownVal(cty.Number), nil
}
if eq.True() {
return i, nil
}
}
return cty.NilVal, errors.New("item not found")
},
})
}
// timestampFunc constructs a function that returns a string representation of the current date and time. // timestampFunc constructs a function that returns a string representation of the current date and time.
// //
// This function was imported from terraform's datetime utilities. // This function was imported from terraform's datetime utilities.
var timestampFunc = function.New(&function.Spec{ func timestampFunc() function.Function {
Params: []function.Parameter{}, return function.New(&function.Spec{
Type: function.StaticReturnType(cty.String), Params: []function.Parameter{},
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) { Type: function.StaticReturnType(cty.String),
return cty.StringVal(time.Now().UTC().Format(time.RFC3339)), nil Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
}, return cty.StringVal(time.Now().UTC().Format(time.RFC3339)), nil
}) },
})
}
func Stdlib() map[string]function.Function { func Stdlib() map[string]function.Function {
funcs := make(map[string]function.Function, len(stdlibFunctions)) funcs := make(map[string]function.Function, len(stdlibFunctions))
for k, v := range stdlibFunctions { for _, v := range stdlibFunctions {
funcs[k] = v if v.factory != nil {
funcs[v.name] = v.factory()
} else {
funcs[v.name] = v.fn
}
} }
return funcs return funcs
} }

View File

@ -34,7 +34,7 @@ func TestIndexOf(t *testing.T) {
for name, test := range tests { for name, test := range tests {
name, test := name, test name, test := name, test
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
got, err := indexOfFunc.Call([]cty.Value{test.input, test.key}) got, err := indexOfFunc().Call([]cty.Value{test.input, test.key})
if err != nil { if err != nil {
if test.wantErr { if test.wantErr {
return return