Skip to content

Commit

Permalink
fix: disambiguate variable defaults with "empty" values from undefined
Browse files Browse the repository at this point in the history
- the default is wrapped in a struct, so that the wrapper being nil
  can indicate that `default` was undefined. if a default was defined,
  the Value field is serialized
  • Loading branch information
jstewmon committed Jul 12, 2019
1 parent 9c24e68 commit 78a9767
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion tfconfig/load_hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func loadModule(dir string) (*Module, Diagnostics) {
// guaranteed valid by ctyjson.Marshal.
panic(fmt.Errorf("failed to re-parse default value from JSON: %s", err))
}
v.Default = def
v.Default = &Default{Value: def}
}
}

Expand Down
2 changes: 1 addition & 1 deletion tfconfig/load_legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func loadModuleLegacyHCL(dir string) (*Module, Diagnostics) {
Name: name,
Type: block.Type,
Description: block.Description,
Default: block.Default,
Default: &Default{Value: block.Default},
Pos: sourcePosLegacyHCL(item.Pos(), filename),
}
if _, exists := mod.Variables[name]; exists {
Expand Down
14 changes: 13 additions & 1 deletion tfconfig/variable.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package tfconfig

import (
"encoding/json"
)

type Default struct {
Value interface{}
}

// Variable represents a single variable from a Terraform module.
type Variable struct {
Name string `json:"name"`
Expand All @@ -10,7 +18,11 @@ type Variable struct {
// the native Go type system. The conversion from the value given in
// configuration may be slightly lossy. Only values that can be
// serialized by json.Marshal will be included here.
Default interface{} `json:"default,omitempty"`
Default *Default `json:"default,omitempty"`

Pos SourcePos `json:"pos"`
}

func (d *Default) MarshalJSON() ([]byte, error) {
return json.Marshal(&d.Value)
}

0 comments on commit 78a9767

Please sign in to comment.