hcl icon indicating copy to clipboard operation
hcl copied to clipboard

gohcl: skip encoding additional nil fields

Open jedevc opened this issue 3 years ago • 3 comments

This patch skips encoding nil slices, maps and interfaces, in addition to the already skipped nil pointers.

The scenario I've run into this in is something like the following:

type X struct {
	A *string  `hcl:"a"`
	B []string `hcl:"b"`
}

...

f := hclwrite.NewEmptyFile()
block := gohcl.EncodeAsBlock(X{}, "test")
f.Body().AppendBlock(block)
fmt.Println(string(f.Bytes()))

Without the patch I get:

test {
  b = null
}

While with the patch, I get:

test {
}

Hopefully this small change is something worth incorporating!

jedevc avatar Apr 13 '22 09:04 jedevc

CLA assistant check
All committers have signed the CLA.

hashicorp-cla avatar Apr 13 '22 09:04 hashicorp-cla

Is there anything currently blocking this?

jedevc avatar Apr 27 '22 14:04 jedevc

I think the change does a good thing. Any chance it can be merged?

yuri-tceretian avatar Aug 30 '23 16:08 yuri-tceretian