mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
helper/schema: allow pointer values to ResourceData.Set
This commit is contained in:
parent
54b5349d26
commit
c030148259
@ -137,6 +137,18 @@ func (d *ResourceData) Partial(on bool) {
|
|||||||
// will be returned.
|
// will be returned.
|
||||||
func (d *ResourceData) Set(key string, value interface{}) error {
|
func (d *ResourceData) Set(key string, value interface{}) error {
|
||||||
d.once.Do(d.init)
|
d.once.Do(d.init)
|
||||||
|
|
||||||
|
// If the value is a pointer to a non-struct, get its value and
|
||||||
|
// use that. This allows Set to take a pointer to primitives to
|
||||||
|
// simplify the interface.
|
||||||
|
reflectVal := reflect.ValueOf(value)
|
||||||
|
if reflectVal.Kind() == reflect.Ptr {
|
||||||
|
reflectVal = reflect.Indirect(reflectVal)
|
||||||
|
if reflectVal.Kind() != reflect.Struct {
|
||||||
|
value = reflectVal.Interface()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return d.setWriter.WriteField(strings.Split(key, "."), value)
|
return d.setWriter.WriteField(strings.Split(key, "."), value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1588,6 +1588,72 @@ func TestResourceDataSet(t *testing.T) {
|
|||||||
GetKey: "ratios",
|
GetKey: "ratios",
|
||||||
GetValue: []interface{}{1.0, 2.2, 5.5},
|
GetValue: []interface{}{1.0, 2.2, 5.5},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// #13: Basic pointer
|
||||||
|
{
|
||||||
|
Schema: map[string]*Schema{
|
||||||
|
"availability_zone": &Schema{
|
||||||
|
Type: TypeString,
|
||||||
|
Optional: true,
|
||||||
|
Computed: true,
|
||||||
|
ForceNew: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
State: nil,
|
||||||
|
|
||||||
|
Diff: nil,
|
||||||
|
|
||||||
|
Key: "availability_zone",
|
||||||
|
Value: testPtrTo("foo"),
|
||||||
|
|
||||||
|
GetKey: "availability_zone",
|
||||||
|
GetValue: "foo",
|
||||||
|
},
|
||||||
|
|
||||||
|
// #14: Basic nil value
|
||||||
|
{
|
||||||
|
Schema: map[string]*Schema{
|
||||||
|
"availability_zone": &Schema{
|
||||||
|
Type: TypeString,
|
||||||
|
Optional: true,
|
||||||
|
Computed: true,
|
||||||
|
ForceNew: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
State: nil,
|
||||||
|
|
||||||
|
Diff: nil,
|
||||||
|
|
||||||
|
Key: "availability_zone",
|
||||||
|
Value: testPtrTo(nil),
|
||||||
|
|
||||||
|
GetKey: "availability_zone",
|
||||||
|
GetValue: "",
|
||||||
|
},
|
||||||
|
|
||||||
|
// #15: Basic nil pointer
|
||||||
|
{
|
||||||
|
Schema: map[string]*Schema{
|
||||||
|
"availability_zone": &Schema{
|
||||||
|
Type: TypeString,
|
||||||
|
Optional: true,
|
||||||
|
Computed: true,
|
||||||
|
ForceNew: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
State: nil,
|
||||||
|
|
||||||
|
Diff: nil,
|
||||||
|
|
||||||
|
Key: "availability_zone",
|
||||||
|
Value: nil,
|
||||||
|
|
||||||
|
GetKey: "availability_zone",
|
||||||
|
GetValue: "",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, tc := range cases {
|
for i, tc := range cases {
|
||||||
@ -2788,3 +2854,7 @@ func TestResourceDataSetId_override(t *testing.T) {
|
|||||||
t.Fatalf("bad: %#v", actual)
|
t.Fatalf("bad: %#v", actual)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testPtrTo(raw interface{}) interface{} {
|
||||||
|
return &raw
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user