From 8361ecd246ae1469abe2adb502fab9305a0ad15c Mon Sep 17 00:00:00 2001 From: Daniel Banck Date: Wed, 15 Feb 2023 14:44:54 +0100 Subject: [PATCH] Add Terraform 1.3 functions --- internal/funcs/1.3/functions.go | 59 +++++++++++++++++++++++++++++++++ schema/functions.go | 4 +++ 2 files changed, 63 insertions(+) create mode 100644 internal/funcs/1.3/functions.go diff --git a/internal/funcs/1.3/functions.go b/internal/funcs/1.3/functions.go new file mode 100644 index 00000000..c3aac59f --- /dev/null +++ b/internal/funcs/1.3/functions.go @@ -0,0 +1,59 @@ +package funcs + +import ( + "github.com/hashicorp/go-version" + "github.com/hashicorp/hcl-lang/schema" + "github.com/zclconf/go-cty/cty" + "github.com/zclconf/go-cty/cty/function" + + funcs_v0_15 "github.com/hashicorp/terraform-schema/internal/funcs/0.15" +) + +func Functions(v *version.Version) map[string]schema.FunctionSignature { + f := funcs_v0_15.Functions(v) + + f["endswith"] = schema.FunctionSignature{ + Params: []function.Parameter{ + { + Name: "str", + Type: cty.String, + }, + { + Name: "suffix", + Type: cty.String, + }, + }, + ReturnType: cty.Bool, + Description: "`endswith` takes two values: a string to check and a suffix string. The function returns true if the first string ends with that exact suffix.", + } + f["startswith"] = schema.FunctionSignature{ + Params: []function.Parameter{ + { + Name: "str", + Type: cty.String, + }, + { + Name: "prefix", + Type: cty.String, + }, + }, + ReturnType: cty.Bool, + Description: "`startswith` takes two values: a string to check and a prefix string. The function returns true if the string begins with that exact prefix.", + } + f["timecmp"] = schema.FunctionSignature{ + Params: []function.Parameter{ + { + Name: "timestamp_a", + Type: cty.String, + }, + { + Name: "timestamp_b", + Type: cty.String, + }, + }, + ReturnType: cty.Number, + Description: "`timecmp` compares two timestamps and returns a number that represents the ordering of the instants those timestamps represent.", + } + + return f +} diff --git a/schema/functions.go b/schema/functions.go index daf7e58b..c5655766 100644 --- a/schema/functions.go +++ b/schema/functions.go @@ -10,6 +10,7 @@ import ( funcs_v0_13 "github.com/hashicorp/terraform-schema/internal/funcs/0.13" funcs_v0_14 "github.com/hashicorp/terraform-schema/internal/funcs/0.14" funcs_v0_15 "github.com/hashicorp/terraform-schema/internal/funcs/0.15" + funcs_v1_3 "github.com/hashicorp/terraform-schema/internal/funcs/1.3" ) func FunctionsForVersion(v *version.Version) (map[string]schema.FunctionSignature, error) { @@ -18,6 +19,9 @@ func FunctionsForVersion(v *version.Version) (map[string]schema.FunctionSignatur return nil, fmt.Errorf("invalid version: %w", err) } + if ver.GreaterThanOrEqual(v1_3) { + return funcs_v1_3.Functions(ver), nil + } if ver.GreaterThanOrEqual(v0_15) { return funcs_v0_15.Functions(ver), nil }