mapstructure-to-hcl2: make basic named types pointers ( optional )

This commit is contained in:
Adrien Delorme 2020-01-07 12:46:27 +01:00
parent d8c3385814
commit 13ea97a1df
3 changed files with 165 additions and 153 deletions

View File

@ -38,7 +38,7 @@ type FlatConfig struct {
Cloud *string `mapstructure:"cloud" required:"false" cty:"cloud"`
ImageName *string `mapstructure:"image_name" required:"true" cty:"image_name"`
ImageMetadata map[string]string `mapstructure:"metadata" required:"false" cty:"metadata"`
ImageVisibility images.ImageVisibility `mapstructure:"image_visibility" required:"false" cty:"image_visibility"`
ImageVisibility *images.ImageVisibility `mapstructure:"image_visibility" required:"false" cty:"image_visibility"`
ImageMembers []string `mapstructure:"image_members" required:"false" cty:"image_members"`
ImageDiskFormat *string `mapstructure:"image_disk_format" required:"false" cty:"image_disk_format"`
ImageTags []string `mapstructure:"image_tags" required:"false" cty:"image_tags"`

View File

@ -450,7 +450,7 @@ func getMapstructureSquashedStruct(topPkg *types.Package, utStruct *types.Struct
field = types.NewField(field.Pos(), field.Pkg(), field.Name(), types.NewPointer(types.Typ[types.Bool]), field.Embedded())
case "github.com/hashicorp/packer/provisioner/powershell.ExecutionPolicy": // TODO(azr): unhack this situation
field = types.NewField(field.Pos(), field.Pkg(), field.Name(), types.NewPointer(types.Typ[types.String]), field.Embedded())
}
default:
if str, isStruct := f.Underlying().(*types.Struct); isStruct {
obj := flattenNamed(f, str)
field = types.NewField(field.Pos(), field.Pkg(), field.Name(), obj, field.Embedded())
@ -467,6 +467,10 @@ func getMapstructureSquashedStruct(topPkg *types.Package, utStruct *types.Struct
}
}
}
if _, isBasic := f.Underlying().(*types.Basic); isBasic {
field = makePointer(field)
}
}
case *types.Slice:
if f, fNamed := f.Elem().(*types.Named); fNamed {
if str, isStruct := f.Underlying().(*types.Struct); isStruct {

View File

@ -17,6 +17,8 @@ type FlatMockConfig struct {
Duration *string `mapstructure:"duration" cty:"duration"`
MapStringString map[string]string `mapstructure:"map_string_string" cty:"map_string_string"`
SliceString []string `mapstructure:"slice_string" cty:"slice_string"`
NamedMapStringString NamedMapStringString `mapstructure:"named_map_string_string" cty:"named_map_string_string"`
NamedString *NamedString `mapstructure:"named_string" cty:"named_string"`
Nested *FlatNestedMockConfig `mapstructure:"nested" cty:"nested"`
NestedSlice []FlatNestedMockConfig `mapstructure:"nested_slice" cty:"nested_slice"`
}
@ -41,6 +43,8 @@ func (*FlatMockConfig) HCL2Spec() map[string]hcldec.Spec {
"duration": &hcldec.AttrSpec{Name: "duration", Type: cty.String, Required: false},
"map_string_string": &hcldec.BlockAttrsSpec{TypeName: "map_string_string", ElementType: cty.String, Required: false},
"slice_string": &hcldec.AttrSpec{Name: "slice_string", Type: cty.List(cty.String), Required: false},
"named_map_string_string": &hcldec.BlockAttrsSpec{TypeName: "named_map_string_string", ElementType: cty.String, Required: false},
"named_string": &hcldec.AttrSpec{Name: "named_string", Type: cty.String, Required: false},
"nested": &hcldec.BlockSpec{TypeName: "nested", Nested: hcldec.ObjectSpec((*FlatNestedMockConfig)(nil).HCL2Spec())},
"nested_slice": &hcldec.BlockListSpec{TypeName: "nested_slice", Nested: hcldec.ObjectSpec((*FlatNestedMockConfig)(nil).HCL2Spec())},
}
@ -58,6 +62,8 @@ type FlatNestedMockConfig struct {
Duration *string `mapstructure:"duration" cty:"duration"`
MapStringString map[string]string `mapstructure:"map_string_string" cty:"map_string_string"`
SliceString []string `mapstructure:"slice_string" cty:"slice_string"`
NamedMapStringString NamedMapStringString `mapstructure:"named_map_string_string" cty:"named_map_string_string"`
NamedString *NamedString `mapstructure:"named_string" cty:"named_string"`
}
// FlatMapstructure returns a new FlatNestedMockConfig.
@ -80,6 +86,8 @@ func (*FlatNestedMockConfig) HCL2Spec() map[string]hcldec.Spec {
"duration": &hcldec.AttrSpec{Name: "duration", Type: cty.String, Required: false},
"map_string_string": &hcldec.BlockAttrsSpec{TypeName: "map_string_string", ElementType: cty.String, Required: false},
"slice_string": &hcldec.AttrSpec{Name: "slice_string", Type: cty.List(cty.String), Required: false},
"named_map_string_string": &hcldec.BlockAttrsSpec{TypeName: "named_map_string_string", ElementType: cty.String, Required: false},
"named_string": &hcldec.AttrSpec{Name: "named_string", Type: cty.String, Required: false},
}
return s
}