diff --git a/tfconfig/load_hcl.go b/tfconfig/load_hcl.go index 72b5d4a..8186bca 100644 --- a/tfconfig/load_hcl.go +++ b/tfconfig/load_hcl.go @@ -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} } } diff --git a/tfconfig/load_legacy.go b/tfconfig/load_legacy.go index 86ffdf1..319b352 100644 --- a/tfconfig/load_legacy.go +++ b/tfconfig/load_legacy.go @@ -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 { diff --git a/tfconfig/test-fixtures/variable-types/variable-types.out.json b/tfconfig/test-fixtures/variable-types/variable-types.out.json index 9ebfc3e..4669e3d 100644 --- a/tfconfig/test-fixtures/variable-types/variable-types.out.json +++ b/tfconfig/test-fixtures/variable-types/variable-types.out.json @@ -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": {}, diff --git a/tfconfig/test-fixtures/variable-types/variable-types.tf b/tfconfig/test-fixtures/variable-types/variable-types.tf index 030413d..26caa92 100644 --- a/tfconfig/test-fixtures/variable-types/variable-types.tf +++ b/tfconfig/test-fixtures/variable-types/variable-types.tf @@ -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 +} diff --git a/tfconfig/variable.go b/tfconfig/variable.go index 0f73fc9..1955d07 100644 --- a/tfconfig/variable.go +++ b/tfconfig/variable.go @@ -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"` @@ -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) +}