Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hcl encoding should skip nil pointer values #15

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions pkg/internal/hcl/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ func Encode(wr io.Writer, args EncodeArgs) error {

// Encode provider blocks
if len(args.Providers) > 0 {
fileBody.AppendUnstructuredTokens(hclwrite.TokensForIdentifier("// Provider blocks"))
fileBody.AppendUnstructuredTokens(
hclwrite.TokensForIdentifier("// Provider blocks"),
)
fileBody.AppendNewline()
}
for _, provider := range args.Providers {
Expand All @@ -100,7 +102,9 @@ func Encode(wr io.Writer, args EncodeArgs) error {
}
// Encode data blocks
if len(args.DataSources) > 0 {
fileBody.AppendUnstructuredTokens(hclwrite.TokensForIdentifier("// Data blocks"))
fileBody.AppendUnstructuredTokens(
hclwrite.TokensForIdentifier("// Data blocks"),
)
fileBody.AppendNewline()
}
for _, data := range args.DataSources {
Expand All @@ -125,7 +129,9 @@ func Encode(wr io.Writer, args EncodeArgs) error {
}
// Encode resource blocks
if len(args.Resources) > 0 {
fileBody.AppendUnstructuredTokens(hclwrite.TokensForIdentifier("// Resource blocks"))
fileBody.AppendUnstructuredTokens(
hclwrite.TokensForIdentifier("// Resource blocks"),
)
fileBody.AppendNewline()
}
for _, resource := range args.Resources {
Expand Down Expand Up @@ -252,6 +258,10 @@ func encodeStruct(
body.SetAttributeRaw(tagName, tokens)
}
default:
// If the field is a nil pointer, we do not want to render it.
if fv.Kind() == reflect.Ptr && fv.IsNil() {
continue
}
if fv.CanInterface() && fv.Interface() != nil {
ctyVal, err := impliedCtyValue(fv)
if err != nil {
Expand Down Expand Up @@ -293,11 +303,13 @@ func encodeStruct(
}

if len(labels) > 0 {
// When working against the top-level Go struct, no HCL block exists, so `hcl:",label"` tags
// are not allowed here.
// When working against the top-level Go struct, no HCL block exists, so
// `hcl:",label"` tags are not allowed here.
// Only on the root Go struct is the block nil.
if block == nil {
return fmt.Errorf("cannot set hcl label tag on struct without a block")
return fmt.Errorf(
"cannot set hcl label tag on struct without a block",
)
}
block.SetLabels(labels)
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/internal/hcl/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ package hcl

import (
"bytes"
"reflect"
"testing"

tu "github.com/golingon/lingon/pkg/testutil"
"github.com/hashicorp/hcl/v2/hclsimple"
"github.com/hashicorp/hcl/v2/hclwrite"
)

type TerraformBlock struct {
Expand Down Expand Up @@ -228,3 +230,14 @@ func TestEncodeRaw(t *testing.T) {
t.Error(tu.Callers(), diff)
}
}

func TestEncode_StructWithNil(t *testing.T) {
type StructWithNil struct {
ShouldBeNil *string `hcl:"should_be_nil"`
}
block := hclwrite.NewBlock("block", nil)
err := encodeStruct(reflect.ValueOf(StructWithNil{}), block, block.Body())
tu.AssertNoError(t, err)

tu.AssertEqual(t, len(block.Body().Attributes()), 0)
}