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 fd2bb48
Show file tree
Hide file tree
Showing 5 changed files with 99 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
54 changes: 54 additions & 0 deletions tfconfig/test-fixtures/variable-types/variable-types.out.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,60 @@
"filename": "test-fixtures/variable-types/variable-types.tf",
"line": 8
}
},
"string_default_empty": {
"name": "string_default_empty",
"type": "string",
"default": "",
"pos": {
"filename": "test-fixtures/variable-types/variable-types.tf",
"line": 14
}
},
"string_default_null": {
"name": "string_default_null",
"type": "string",
"default": null,
"pos": {
"filename": "test-fixtures/variable-types/variable-types.tf",
"line": 19
}
},
"list_default_empty": {
"name": "list_default_empty",
"type": "list(string)",
"default": [],
"pos": {
"filename": "test-fixtures/variable-types/variable-types.tf",
"line": 24
}
},
"object_default_empty": {
"name": "object_default_empty",
"type": "object({})",
"default": {},
"pos": {
"filename": "test-fixtures/variable-types/variable-types.tf",
"line": 29
}
},
"number_default_zero": {
"name": "number_default_zero",
"type": "number",
"default": 0,
"pos": {
"filename": "test-fixtures/variable-types/variable-types.tf",
"line": 34
}
},
"bool_default_false": {
"name": "bool_default_false",
"type": "bool",
"default": false,
"pos": {
"filename": "test-fixtures/variable-types/variable-types.tf",
"line": 39
}
}
},
"outputs": {},
Expand Down
30 changes: 30 additions & 0 deletions tfconfig/test-fixtures/variable-types/variable-types.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,33 @@ variable "map" {
# with older configurations.
type = "map"
}

variable "string_default_empty" {
type = string
default = ""
}

variable "string_default_null" {
type = string
default = null
}

variable "list_default_empty" {
type = list(string)
default = []
}

variable "object_default_empty" {
type = object({})
default = {}
}

variable "number_default_zero" {
type = number
default = 0
}

variable "bool_default_false" {
type = bool
default = false
}
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 fd2bb48

Please sign in to comment.