Skip to content

Commit 3d9ae62

Browse files
committed
add tests for updated Set and SetAtPath functionality
1 parent 2794272 commit 3d9ae62

File tree

3 files changed

+89
-8
lines changed

3 files changed

+89
-8
lines changed

internal/fwschemadata/data_set_at_path.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,45 @@ func (d *Data) SetAtPath(ctx context.Context, path path.Path, val interface{}) d
3131
ctx = logging.FrameworkWithAttributePath(ctx, path.String())
3232

3333
if v, ok := val.(tftypes.Value); ok {
34-
atPath, err := d.Schema.AttributeAtPath(ctx, path)
35-
if err != nil {
34+
atPath, atPathDiags := d.Schema.AttributeAtPath(ctx, path)
35+
36+
diags.Append(atPathDiags...)
37+
38+
if diags.HasError() {
39+
return diags
40+
}
41+
42+
attrType := atPath.GetType().TerraformType(ctx)
43+
44+
if !attrType.Equal(v.Type()) {
3645
diags.AddAttributeError(
3746
path,
3847
d.Description.Title()+" Write Error",
3948
"An unexpected error was encountered trying to write the "+d.Description.String()+". This is always an error in the provider. Please report the following to the provider developer:\n\n"+
40-
fmt.Sprintf("Error: %s does not exist at path", path.String()),
49+
fmt.Sprintf("Error: Type of provided value does not match type of %s", path.String()),
4150
)
4251
return diags
4352
}
4453

45-
attrType := atPath.GetType().TerraformType(ctx)
54+
transformFunc, transformFuncDiags := d.SetAtPathTransformFunc(ctx, path, v, nil)
55+
diags.Append(transformFuncDiags...)
4656

47-
if !attrType.Equal(v.Type()) {
57+
if diags.HasError() {
58+
return diags
59+
}
60+
61+
tfVal, err := tftypes.Transform(d.TerraformValue, transformFunc)
62+
if err != nil {
4863
diags.AddAttributeError(
4964
path,
5065
d.Description.Title()+" Write Error",
51-
"An unexpected error was encountered trying to write the "+d.Description.String()+". This is always an error in the provider. Please report the following to the provider developer:\n\n"+
52-
fmt.Sprintf("Error: Type of provided value does not match type of %s", path.String()),
66+
"An unexpected error was encountered trying to write an attribute to the "+d.Description.String()+". This is always an error in the provider. Please report the following to the provider developer:\n\n"+
67+
"Error: Cannot transform data: "+err.Error(),
5368
)
5469
return diags
5570
}
5671

57-
d.TerraformValue = v
72+
d.TerraformValue = tfVal
5873

5974
return diags
6075
}

internal/fwschemadata/data_set_at_path_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2924,6 +2924,39 @@ func TestDataSetAtPath(t *testing.T) {
29242924
"other": tftypes.NewValue(tftypes.DynamicPseudoType, nil),
29252925
}),
29262926
},
2927+
"write-tftypes-value": {
2928+
data: fwschemadata.Data{
2929+
TerraformValue: tftypes.NewValue(tftypes.Object{
2930+
AttributeTypes: map[string]tftypes.Type{
2931+
"test": tftypes.String,
2932+
"other": tftypes.String,
2933+
},
2934+
}, nil),
2935+
Schema: testschema.Schema{
2936+
Attributes: map[string]fwschema.Attribute{
2937+
"test": testschema.Attribute{
2938+
Type: types.StringType,
2939+
Required: true,
2940+
},
2941+
"other": testschema.Attribute{
2942+
Type: types.StringType,
2943+
Required: true,
2944+
},
2945+
},
2946+
},
2947+
},
2948+
path: path.Root("test"),
2949+
val: tftypes.NewValue(tftypes.String, "newvalue"),
2950+
expected: tftypes.NewValue(tftypes.Object{
2951+
AttributeTypes: map[string]tftypes.Type{
2952+
"test": tftypes.String,
2953+
"other": tftypes.String,
2954+
},
2955+
}, map[string]tftypes.Value{
2956+
"test": tftypes.NewValue(tftypes.String, "newvalue"),
2957+
"other": tftypes.NewValue(tftypes.String, nil),
2958+
}),
2959+
},
29272960
"AttrTypeWithValidateError": {
29282961
data: fwschemadata.Data{
29292962
TerraformValue: tftypes.NewValue(tftypes.Object{

internal/fwschemadata/data_set_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,39 @@ func TestDataSet(t *testing.T) {
139139
),
140140
}),
141141
},
142+
"write-tftypes-values": {
143+
data: fwschemadata.Data{
144+
TerraformValue: tftypes.NewValue(tftypes.Object{
145+
AttributeTypes: map[string]tftypes.Type{
146+
"name": tftypes.String,
147+
},
148+
}, map[string]tftypes.Value{
149+
"name": tftypes.NewValue(tftypes.String, "oldvalue"),
150+
}),
151+
Schema: testschema.Schema{
152+
Attributes: map[string]fwschema.Attribute{
153+
"name": testschema.Attribute{
154+
Type: types.StringType,
155+
Required: true,
156+
},
157+
},
158+
},
159+
},
160+
val: tftypes.NewValue(tftypes.Object{
161+
AttributeTypes: map[string]tftypes.Type{
162+
"name": tftypes.String,
163+
},
164+
}, map[string]tftypes.Value{
165+
"name": tftypes.NewValue(tftypes.String, "newvalue"),
166+
}),
167+
expected: tftypes.NewValue(tftypes.Object{
168+
AttributeTypes: map[string]tftypes.Type{
169+
"name": tftypes.String,
170+
},
171+
}, map[string]tftypes.Value{
172+
"name": tftypes.NewValue(tftypes.String, "newvalue"),
173+
}),
174+
},
142175
"overwrite": {
143176
data: fwschemadata.Data{
144177
TerraformValue: tftypes.Value{},

0 commit comments

Comments
 (0)