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

gohcl and optional attributes #24

Closed
sigil66 opened this issue Mar 6, 2018 · 3 comments

Comments

Projects
None yet
3 participants
@sigil66
Copy link

commented Mar 6, 2018

Given the tags documented here:

https://godoc.org/github.com/hashicorp/hcl2/gohcl

How does one deal with optional attributes?

@jen20

This comment has been minimized.

Copy link

commented Mar 7, 2018

Looks to me like this is implemented but not documented as a valid tag attribute in Godoc.

@nicholasjackson

This comment has been minimized.

Copy link
Contributor

commented Mar 7, 2018

There are two options one is to use the optional tag with value types like so...

type Action struct {
	Output         string `hcl:"output"`
	OutputProvider providers.Provider

	Template string `hcl:"template,optional"`
}
action {
  output = "abc"
}

The other is you can use reference types like...

type Action struct {
	Output         string `hcl:"output"`
	OutputProvider providers.Provider

	Template *string `hcl:"template"`
}
action {
  output = "abc"
}

In the instance of an optional value you will end up with a type which has the default empty value so "" in the case of string. If you want to check nil then you need to use the reference type.

How come I have never seen that doc file before (probably because I didn't look) will update the docs in the morning (https://github.com/hashicorp/hcl2/blob/master/gohcl/doc.go)

@sigil66

This comment has been minimized.

Copy link
Author

commented Mar 8, 2018

Perfect, thank you for the explanation!

@sigil66 sigil66 closed this Mar 8, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.