Skip to content

gohcl: skip encoding additional nil fields#526

Open
jedevc wants to merge 1 commit intohashicorp:mainfrom
jedevc:gohcl-encode-empty
Open

gohcl: skip encoding additional nil fields#526
jedevc wants to merge 1 commit intohashicorp:mainfrom
jedevc:gohcl-encode-empty

Conversation

@jedevc
Copy link
Copy Markdown

@jedevc jedevc commented Apr 13, 2022

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!

@hashicorp-cla
Copy link
Copy Markdown

hashicorp-cla commented Apr 13, 2022

CLA assistant check
All committers have signed the CLA.

@jedevc jedevc force-pushed the gohcl-encode-empty branch from 2d7a781 to 0de7ac6 Compare April 21, 2022 13:54
This patch skips encoding nil slices, maps and interfaces, in addition
to the already skipped nil pointers.

Signed-off-by: Justin Chadwell <me@jedevc.com>
@jedevc
Copy link
Copy Markdown
Author

jedevc commented Apr 27, 2022

Is there anything currently blocking this?

@yuri-tceretian
Copy link
Copy Markdown

yuri-tceretian commented Aug 30, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants