Skip to content

Commit

Permalink
schemahcl: add empty and alltrue to stdlib (#3358)
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m authored Feb 2, 2025
1 parent 8b5e268 commit 5dc92c5
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
59 changes: 59 additions & 0 deletions schemahcl/stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func stdTypes(ctx *hcl.EvalContext) *hcl.EvalContext {
func stdFuncs() map[string]function.Function {
return map[string]function.Function{
"abs": stdlib.AbsoluteFunc,
"alltrue": allTrueFunc,
"can": tryfunc.CanFunc,
"ceil": stdlib.CeilFunc,
"chomp": stdlib.ChompFunc,
Expand All @@ -120,6 +121,7 @@ func stdFuncs() map[string]function.Function {
"csvdecode": stdlib.CSVDecodeFunc,
"distinct": stdlib.DistinctFunc,
"element": stdlib.ElementFunc,
"empty": emptyFunc,
"endswith": endsWithFunc,
"flatten": stdlib.FlattenFunc,
"floor": stdlib.FloorFunc,
Expand Down Expand Up @@ -463,6 +465,63 @@ var (
return cty.StringVal(regexp.QuoteMeta(args[0].AsString())), nil
},
})

refineNonNull = func(b *cty.RefinementBuilder) *cty.RefinementBuilder {
return b.NotNull()
}

emptyFunc = function.New(&function.Spec{
Description: `Returns a boolean indicating whether the given collection is empty.`,
Params: []function.Parameter{
{
Name: "collection",
Type: cty.DynamicPseudoType,
AllowDynamicType: true,
AllowUnknown: true,
AllowMarked: true,
},
},
Type: func(args []cty.Value) (ret cty.Type, err error) {
collTy := args[0].Type()
if !(collTy.IsTupleType() || collTy.IsListType() || collTy.IsMapType() || collTy.IsSetType() || collTy == cty.DynamicPseudoType) {
return cty.NilType, fmt.Errorf("collection must be a list, a map or a tuple")
}
return cty.Bool, nil
},
RefineResult: refineNonNull,
Impl: func(args []cty.Value, _ cty.Type) (ret cty.Value, err error) {
return args[0].Length().Equals(cty.NumberIntVal(0)), nil
},
})

allTrueFunc = function.New(&function.Spec{
Description: `Returns a boolean indicating whether all elements in the given collection are true.`,
Params: []function.Parameter{
{
Name: "collection",
Type: cty.DynamicPseudoType,
AllowDynamicType: true,
AllowUnknown: true,
AllowMarked: true,
},
},
Type: func(args []cty.Value) (ret cty.Type, err error) {
collTy := args[0].Type()
if !(collTy.IsTupleType() || collTy.IsListType() || collTy.IsMapType() || collTy.IsSetType() || collTy == cty.DynamicPseudoType) {
return cty.NilType, fmt.Errorf("collection must be a list, a map or a tuple")
}
return cty.Bool, nil
},
RefineResult: refineNonNull,
Impl: func(args []cty.Value, _ cty.Type) (ret cty.Value, err error) {
for _, v := range args[0].AsValueSlice() {
if v.Type() != cty.Bool || !v.True() {
return cty.False, nil
}
}
return cty.True, nil
},
})
)

// MakeFileFunc returns a function that reads a file
Expand Down
21 changes: 21 additions & 0 deletions schemahcl/stdlib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,27 @@ func TestEndsWithFunc(t *testing.T) {
require.Equal(t, cty.True, got)
}

func TestEmptyFunc(t *testing.T) {
got, err := emptyFunc.Call([]cty.Value{cty.ListValEmpty(cty.String)})
require.NoError(t, err)
require.Equal(t, cty.True, got)
got, err = emptyFunc.Call([]cty.Value{cty.SetValEmpty(cty.String)})
require.NoError(t, err)
require.Equal(t, cty.True, got)
got, err = emptyFunc.Call([]cty.Value{cty.MapValEmpty(cty.String)})
require.NoError(t, err)
require.Equal(t, cty.True, got)
got, err = emptyFunc.Call([]cty.Value{cty.EmptyTupleVal})
require.NoError(t, err)
require.Equal(t, cty.True, got)
got, err = emptyFunc.Call([]cty.Value{cty.ListVal([]cty.Value{cty.StringVal("a")})})
require.NoError(t, err)
require.Equal(t, cty.False, got)
// Invalid value.
got, err = emptyFunc.Call([]cty.Value{cty.StringVal("a")})
require.EqualError(t, err, "collection must be a list, a map or a tuple")
}

func TestRegexpEscapeFunc(t *testing.T) {
got, err := regexpEscape.Call([]cty.Value{cty.StringVal("a|b|c")})
require.NoError(t, err)
Expand Down

0 comments on commit 5dc92c5

Please sign in to comment.