Skip to content

Commit 8c5175a

Browse files
committed
update to tuple index
1 parent 13be0fc commit 8c5175a

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

internal/reflect/slice.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ func reflectSlice(ctx context.Context, typ attr.Type, val tftypes.Value, target
139139
targetValue := reflect.Zero(elemType)
140140

141141
// update our path so we can have nice errors
142-
// utilizing `AtListIndex` because tuple paths are also based on the underlying `PathStepElementKeyInt`
143-
valPath := path.AtListIndex(pos)
142+
valPath := path.AtTupleIndex(pos)
144143

145144
// reflect the value into our new target
146145
val, valDiags := BuildValue(ctx, elemAttrType, value, targetValue, opts, valPath)
@@ -280,8 +279,7 @@ func FromSlice(ctx context.Context, typ attr.Type, val reflect.Value, path path.
280279
}
281280

282281
for i := 0; i < val.Len(); i++ {
283-
// utilizing `AtListIndex` because tuple paths are also based on the underlying `PathStepElementKeyInt`
284-
valPath := path.AtListIndex(i)
282+
valPath := path.AtTupleIndex(i)
285283

286284
val, valDiags := FromValue(ctx, elemAttrType, val.Index(i).Interface(), valPath)
287285
diags.Append(valDiags...)

path/path.go

+12
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ func (p Path) AtListIndex(index int) Path {
5252
return copiedPath
5353
}
5454

55+
// AtTupleIndex returns a copied path with a new tuple index step at the end.
56+
// The returned path is safe to modify without affecting the original.
57+
//
58+
// Tuple indices are 0-based. The first element of a tuple is 0.
59+
func (p Path) AtTupleIndex(index int) Path {
60+
copiedPath := p.Copy()
61+
62+
copiedPath.steps.Append(PathStepElementKeyInt(index))
63+
64+
return copiedPath
65+
}
66+
5567
// AtMapKey returns a copied path with a new map key step at the end.
5668
// The returned path is safe to modify without affecting the original.
5769
func (p Path) AtMapKey(key string) Path {

path/path_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,46 @@ func TestPathAtListIndex(t *testing.T) {
5252
}
5353
}
5454

55+
func TestPathAtTupleIndex(t *testing.T) {
56+
t.Parallel()
57+
58+
testCases := map[string]struct {
59+
path path.Path
60+
index int
61+
expected path.Path
62+
}{
63+
"empty": {
64+
path: path.Empty(),
65+
index: 1,
66+
expected: path.Empty().AtTupleIndex(1),
67+
},
68+
"shallow": {
69+
path: path.Root("test"),
70+
index: 1,
71+
expected: path.Root("test").AtTupleIndex(1),
72+
},
73+
"deep": {
74+
path: path.Root("test1").AtTupleIndex(0).AtName("test2"),
75+
index: 1,
76+
expected: path.Root("test1").AtTupleIndex(0).AtName("test2").AtTupleIndex(1),
77+
},
78+
}
79+
80+
for name, testCase := range testCases {
81+
name, testCase := name, testCase
82+
83+
t.Run(name, func(t *testing.T) {
84+
t.Parallel()
85+
86+
got := testCase.path.AtTupleIndex(testCase.index)
87+
88+
if diff := cmp.Diff(got, testCase.expected); diff != "" {
89+
t.Errorf("unexpected difference: %s", diff)
90+
}
91+
})
92+
}
93+
}
94+
5595
func TestPathAtMapKey(t *testing.T) {
5696
t.Parallel()
5797

0 commit comments

Comments
 (0)