Skip to content

Commit

Permalink
Fix provider::terraform::* function names (#2046)
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 authored May 11, 2024
1 parent b7bc5fe commit d69dad0
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion integrationtest/inspection/functions/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ resource "aws_instance" "provider" {
}

resource "aws_instance" "builtin_provider" {
instance_type = provider::terraform::tfvarsencode({ a = 1 })
instance_type = provider::terraform::encode_tfvars({ a = 1 })
}
2 changes: 1 addition & 1 deletion integrationtest/inspection/functions/result.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
},
"end": {
"line": 14,
"column": 63
"column": 64
}
},
"callers": []
Expand Down
2 changes: 1 addition & 1 deletion terraform/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ variable "string_var" {
},
{
name: "built-in provider-defined functions",
expr: expr(`provider::terraform::tfvarsdecode("a = 1")`),
expr: expr(`provider::terraform::decode_tfvars("a = 1")`),
ty: cty.Object(map[string]cty.Type{"a": cty.Number}),
want: `cty.ObjectVal(map[string]cty.Value{"a":cty.NumberIntVal(1)})`,
errCheck: neverHappend,
Expand Down
8 changes: 4 additions & 4 deletions terraform/lang/funcs/terraform/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/zclconf/go-cty/cty/function"
)

var TFVarsEncodeFunc = function.New(&function.Spec{
var EncodeTfvarsFunc = function.New(&function.Spec{
Params: []function.Parameter{
{
Name: "value",
Expand Down Expand Up @@ -88,7 +88,7 @@ var TFVarsEncodeFunc = function.New(&function.Spec{
},
})

var TFVarsDecodeFunc = function.New(&function.Spec{
var DecodeTfvarsFunc = function.New(&function.Spec{
Params: []function.Parameter{
{
Name: "src",
Expand Down Expand Up @@ -131,7 +131,7 @@ var TFVarsDecodeFunc = function.New(&function.Spec{
// stuff HCL diagnostics into plain string error messages. This produces
// a non-ideal result but is still better than hiding the HCL-provided
// diagnosis altogether.
f, hclDiags := hclsyntax.ParseConfig(src, "<tfvarsdecode argument>", hcl.InitialPos)
f, hclDiags := hclsyntax.ParseConfig(src, "<decode_tfvars argument>", hcl.InitialPos)
if hclDiags.HasErrors() {
return cty.NilVal, fmt.Errorf("invalid tfvars syntax: %s", hclDiags.Error())
}
Expand All @@ -155,7 +155,7 @@ var TFVarsDecodeFunc = function.New(&function.Spec{
},
})

var ExprEncodeFunc = function.New(&function.Spec{
var EncodeExprFunc = function.New(&function.Spec{
Params: []function.Parameter{
{
Name: "value",
Expand Down
20 changes: 10 additions & 10 deletions terraform/lang/funcs/terraform/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/zclconf/go-cty/cty"
)

func TestTFVarsEncode(t *testing.T) {
func TestEncodeTfvars(t *testing.T) {
tests := []struct {
Input cty.Value
Want cty.Value
Expand Down Expand Up @@ -128,7 +128,7 @@ two = 2

for _, test := range tests {
t.Run(test.Input.GoString(), func(t *testing.T) {
got, err := TFVarsEncodeFunc.Call([]cty.Value{test.Input})
got, err := EncodeTfvarsFunc.Call([]cty.Value{test.Input})
if test.WantErr != "" {
if err == nil {
t.Fatalf("unexpected success for %#v; want error\ngot: %#v", test.Input, got)
Expand All @@ -148,7 +148,7 @@ two = 2
}
}

func TestTFVarsDecode(t *testing.T) {
func TestDecodeTfvars(t *testing.T) {
tests := []struct {
Input cty.Value
Want cty.Value
Expand Down Expand Up @@ -179,25 +179,25 @@ number = 2`),
// This is actually not a very good diagnosis for this error,
// since we're expecting HCL arguments rather than HCL blocks,
// but that's something we'd need to address in HCL.
WantErr: `invalid tfvars syntax: <tfvarsdecode argument>:1,17-17: Invalid block definition; Either a quoted string block label or an opening brace ("{") is expected here.`,
WantErr: `invalid tfvars syntax: <decode_tfvars argument>:1,17-17: Invalid block definition; Either a quoted string block label or an opening brace ("{") is expected here.`,
},
{
Input: cty.StringVal(`foo = not valid syntax`),
WantErr: `invalid tfvars syntax: <tfvarsdecode argument>:1,11-16: Missing newline after argument; An argument definition must end with a newline.`,
WantErr: `invalid tfvars syntax: <decode_tfvars argument>:1,11-16: Missing newline after argument; An argument definition must end with a newline.`,
},
{
Input: cty.StringVal(`foo = var.whatever`),
WantErr: `invalid expression for variable "foo": <tfvarsdecode argument>:1,7-10: Variables not allowed; Variables may not be used here.`,
WantErr: `invalid expression for variable "foo": <decode_tfvars argument>:1,7-10: Variables not allowed; Variables may not be used here.`,
},
{
Input: cty.StringVal(`foo = whatever()`),
WantErr: `invalid expression for variable "foo": <tfvarsdecode argument>:1,7-17: Function calls not allowed; Functions may not be called here.`,
WantErr: `invalid expression for variable "foo": <decode_tfvars argument>:1,7-17: Function calls not allowed; Functions may not be called here.`,
},
}

for _, test := range tests {
t.Run(test.Input.GoString(), func(t *testing.T) {
got, err := TFVarsDecodeFunc.Call([]cty.Value{test.Input})
got, err := DecodeTfvarsFunc.Call([]cty.Value{test.Input})
if test.WantErr != "" {
if err == nil {
t.Fatalf("unexpected success for %#v; want error\ngot: %#v", test.Input, got)
Expand All @@ -217,7 +217,7 @@ number = 2`),
}
}

func TestExprEncode(t *testing.T) {
func TestEncodeExpr(t *testing.T) {
tests := []struct {
Input cty.Value
Want cty.Value
Expand Down Expand Up @@ -361,7 +361,7 @@ func TestExprEncode(t *testing.T) {

for _, test := range tests {
t.Run(test.Input.GoString(), func(t *testing.T) {
got, err := ExprEncodeFunc.Call([]cty.Value{test.Input})
got, err := EncodeExprFunc.Call([]cty.Value{test.Input})
if test.WantErr != "" {
if err == nil {
t.Fatalf("unexpected success for %#v; want error\ngot: %#v", test.Input, got)
Expand Down
6 changes: 3 additions & 3 deletions terraform/lang/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ func (s *Scope) Functions() map[string]function.Function {

// Built-in Terraform provider-defined functions are typically obtained dynamically,
// but given that they are built-ins, they are provided just like regular functions.
s.funcs["provider::terraform::tfvarsencode"] = terraform.TFVarsEncodeFunc
s.funcs["provider::terraform::tfvarsdecode"] = terraform.TFVarsDecodeFunc
s.funcs["provider::terraform::exprencode"] = terraform.ExprEncodeFunc
s.funcs["provider::terraform::encode_tfvars"] = terraform.EncodeTfvarsFunc
s.funcs["provider::terraform::decode_tfvars"] = terraform.DecodeTfvarsFunc
s.funcs["provider::terraform::encode_expr"] = terraform.EncodeExprFunc
}
s.funcsLock.Unlock()

Expand Down

0 comments on commit d69dad0

Please sign in to comment.