hashicorp / hcl2

Former temporary home for experimental new version of HCL
https://github.com/hashicorp/hcl
Mozilla Public License 2.0
373 stars 66 forks source link

Added struct tag optional for gohcl #13

Closed nicholasjackson closed 6 years ago

nicholasjackson commented 6 years ago

Added optional tag for attributes on structs. In the following example, MiddleName is marked as optional and after decoding the field MiddleName on the Person object would be an empty string. For situations where HCL is used for configuration optional rather than a pointer is more explicit. It allows parsing of config with validation without needing to mix value and reference types for simple objects.

struct Person struct {
  FirstName     string `hcl:"first_name"`
  MiddleName string `hcl:"middle_name,optional"`
  Surname       string `hcl:"surname"`
}
person {
  first_name = "Nic"
  surname    = "Jackson"
}

I was not sure where to place the docs for this, happy to add docs as an additional commit.

Signed-off-by: nicholasjackson jackson.nic@gmail.com

apparentlymart commented 6 years ago

Thanks! This looks great to me.

There is currently no explicit documentation on the mapping rules here since I'd intended to let the implementation "settle" first and then document it, but haven't yet got to that point. I'll write complete docs for this as part of our pass of finalizing this implementation as non-experimental. For now, I'm going to merge this as-is.

This PR also made me realize that we aren't running Travis-CI tests for PRs on this repo since it moved over here into the hashicorp organization. I've now switched that on for future PRs, but for this one I pulled it locally and ran the tests myself:

$ go test ./...
?       github.com/hashicorp/hcl2/cmd/hcldec    [no test files]
?       github.com/hashicorp/hcl2/cmd/hclfmt    [no test files]
ok      github.com/hashicorp/hcl2/ext/dynblock  0.007s
ok      github.com/hashicorp/hcl2/ext/include   0.012s
ok      github.com/hashicorp/hcl2/ext/transform 0.006s
ok      github.com/hashicorp/hcl2/ext/userfunc  0.034s
ok      github.com/hashicorp/hcl2/gohcl 0.010s
ok      github.com/hashicorp/hcl2/hcl   0.003s
ok      github.com/hashicorp/hcl2/hcl/hclsyntax 0.031s
?       github.com/hashicorp/hcl2/hcl/hclsyntax/fuzz/config [no test files]
?       github.com/hashicorp/hcl2/hcl/hclsyntax/fuzz/expr   [no test files]
?       github.com/hashicorp/hcl2/hcl/hclsyntax/fuzz/template   [no test files]
?       github.com/hashicorp/hcl2/hcl/hclsyntax/fuzz/traversal  [no test files]
ok      github.com/hashicorp/hcl2/hcl/integrationtest   0.003s
ok      github.com/hashicorp/hcl2/hcl/json  0.004s
?       github.com/hashicorp/hcl2/hcl/json/fuzz/config  [no test files]
ok      github.com/hashicorp/hcl2/hcldec    0.003s
?       github.com/hashicorp/hcl2/hcled [no test files]
?       github.com/hashicorp/hcl2/hclparse  [no test files]
ok      github.com/hashicorp/hcl2/hcltest   0.002s
ok      github.com/hashicorp/hcl2/hclwrite  0.008s