Refactor rendering Terraform functions into constructor

This commit is contained in:
John Gardiner Myers 2022-11-24 19:12:18 -08:00
parent 46ece64893
commit 1b99cae617
2 changed files with 9 additions and 12 deletions

View File

@ -17,10 +17,8 @@ limitations under the License.
package terraform package terraform
import ( import (
"fmt"
"reflect" "reflect"
"sort" "sort"
"strings"
"github.com/hashicorp/hcl/v2" "github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax" "github.com/hashicorp/hcl/v2/hclsyntax"
@ -104,14 +102,13 @@ func writeValue(body *hclwrite.Body, key string, value cty.Value) {
// key = res_type.res_name.res_prop // key = res_type.res_name.res_prop
// key = file("${module.path}/foo") // key = file("${module.path}/foo")
func writeLiteral(body *hclwrite.Body, key string, literal *terraformWriter.Literal) { func writeLiteral(body *hclwrite.Body, key string, literal *terraformWriter.Literal) {
if literal.FnName != "" { if literal.String != "" {
tokens := hclwrite.Tokens{ body.SetAttributeRaw(key, hclwrite.Tokens{
{ {
Type: hclsyntax.TokenIdent, Type: hclsyntax.TokenIdent,
Bytes: []byte(fmt.Sprintf("%v(%v)", literal.FnName, strings.Join(literal.FnArgs, ", "))), Bytes: []byte(literal.String),
}, },
} })
body.SetAttributeRaw(key, tokens)
} else if literal.Index { } else if literal.Index {
tokens := hclwrite.Tokens{ tokens := hclwrite.Tokens{
{ {
@ -242,10 +239,10 @@ func writeMap(body *hclwrite.Body, key string, values map[string]cty.Value) {
errLiteralSlice := gocty.FromCtyValue(v, refLiteralSlice.Interface()) errLiteralSlice := gocty.FromCtyValue(v, refLiteralSlice.Interface())
// If this is a map of literals then do not surround the value with quotes // If this is a map of literals then do not surround the value with quotes
if literal, ok := refLiteral.Interface().(*terraformWriter.Literal); errLiteral == nil && ok { if literal, ok := refLiteral.Interface().(*terraformWriter.Literal); errLiteral == nil && ok {
if literal.FnName != "" { if literal.String != "" {
tokens = append(tokens, &hclwrite.Token{ tokens = append(tokens, &hclwrite.Token{
Type: hclsyntax.TokenIdent, Type: hclsyntax.TokenIdent,
Bytes: []byte(fmt.Sprintf("%v(%v)", literal.FnName, strings.Join(literal.FnArgs, ", "))), Bytes: []byte(literal.String),
}) })
} else if literal.Value != "" { } else if literal.Value != "" {
tokens = append(tokens, []*hclwrite.Token{ tokens = append(tokens, []*hclwrite.Token{

View File

@ -27,6 +27,8 @@ import (
// Literal represents a literal in terraform syntax // Literal represents a literal in terraform syntax
type Literal struct { type Literal struct {
// String is the Terraform representation.
String string `cty:"string"`
// Value is used to support Terraform's "${}" interpolation. // Value is used to support Terraform's "${}" interpolation.
Value string `cty:"value"` Value string `cty:"value"`
// Index to support the index of the count meta-argument. // Index to support the index of the count meta-argument.
@ -36,8 +38,6 @@ type Literal struct {
// example: {"aws_vpc", "foo", "id"} // example: {"aws_vpc", "foo", "id"}
Tokens []string `cty:"tokens"` Tokens []string `cty:"tokens"`
// FnName represents the name of a terraform function.
FnName string `cty:"fn_name"`
// FnArgs contains string representations of arguments to the function call. // FnArgs contains string representations of arguments to the function call.
// Any string arguments must be quoted. // Any string arguments must be quoted.
FnArgs []string `cty:"fn_arg"` FnArgs []string `cty:"fn_arg"`
@ -51,8 +51,8 @@ func (l *Literal) MarshalJSON() ([]byte, error) {
func LiteralFunctionExpression(functionName string, args ...string) *Literal { func LiteralFunctionExpression(functionName string, args ...string) *Literal {
return &Literal{ return &Literal{
String: fmt.Sprintf("%v(%v)", functionName, strings.Join(args, ", ")),
Value: fmt.Sprintf("${%v(%v)}", functionName, strings.Join(args, ", ")), Value: fmt.Sprintf("${%v(%v)}", functionName, strings.Join(args, ", ")),
FnName: functionName,
FnArgs: args, FnArgs: args,
} }
} }