Skip to content

Commit bb06972

Browse files
author
Paddy
authored
The Great Renaming of Map.AttributeType. (#105)
Maps don't have an AttributeType in cty. They have an ElementType. In my wayward youth of like a year ago, I decided this was A Mistake and in my great and everlasting wisdom, I changed it to AttributeType. I think my rationale was that elements are integer-indexed and attributes are string-indexed. My hubris has bitten me. This was a bad take. It makes diagnostics weird (why am I using an ElementKeyString instead of an AttributeName if it's an attribute?) and deviates with basically no value. Worse, it's just... wrong. Attributes are structural in nature, like properties in Go structs. They're defined by the provider developer. Elements aren't structural, like Go maps. The type has no constraint on what keys or how many keys can be used. Maps are obviously this second thing. This is the great renaming, that will break everything but will finally, finally correct my blunder.
1 parent 38190dd commit bb06972

19 files changed

+185
-182
lines changed

.changelog/105.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:breaking-change
2+
The `AttributeType` property of `tftypes.Map` has been renamed to `ElementType`.
3+
```

tfprotov5/schema.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const (
2929
// SchemaNestedBlockNestingModeMap indicates that multiple instances of
3030
// the nested block should be permitted, each with a single label, and
3131
// that they should be represented in state and config values as a
32-
// tftypes.Map, with an AttributeType of tftypes.Object. The labels on
32+
// tftypes.Map, with an ElementType of tftypes.Object. The labels on
3333
// the blocks will be used as the map keys. It is an error, therefore,
3434
// to use the same label value on multiple block instances.
3535
SchemaNestedBlockNestingModeMap SchemaNestedBlockNestingMode = 4

tfprotov6/schema.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const (
2929
// SchemaNestedBlockNestingModeMap indicates that multiple instances of
3030
// the nested block should be permitted, each with a single label, and
3131
// that they should be represented in state and config values as a
32-
// tftypes.Map, with an AttributeType of tftypes.Object. The labels on
32+
// tftypes.Map, with an ElementType of tftypes.Object. The labels on
3333
// the blocks will be used as the map keys. It is an error, therefore,
3434
// to use the same label value on multiple block instances.
3535
SchemaNestedBlockNestingModeMap SchemaNestedBlockNestingMode = 4
@@ -73,7 +73,7 @@ const (
7373

7474
// SchemaObjectNestingModeMap indicates that multiple instances of the
7575
// nested type should be permitted, and that they should be appear in state
76-
// and config values as a tftypes.Map, with an AttributeType of
76+
// and config values as a tftypes.Map, with an ElementType of
7777
// tftypes.Object.
7878
SchemaObjectNestingModeMap SchemaObjectNestingMode = 4
7979
)

tftypes/list_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func TestListUsableAs(t *testing.T) {
224224
},
225225
"list-string-map": {
226226
list: List{ElementType: String},
227-
other: Map{AttributeType: String},
227+
other: Map{ElementType: String},
228228
expected: false,
229229
},
230230
"list-string-object": {

tftypes/map.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
// Map is a Terraform type representing an unordered collection of elements,
99
// all of the same type, each identifiable with a unique string key.
1010
type Map struct {
11-
AttributeType Type
11+
ElementType Type
1212

1313
// used to make this type uncomparable
1414
// see https://golang.org/ref/spec#Comparison_operators
@@ -17,26 +17,26 @@ type Map struct {
1717
}
1818

1919
// Equal returns true if the two Maps are exactly equal. Unlike Is, passing in
20-
// a Map with no AttributeType will always return false.
20+
// a Map with no ElementType will always return false.
2121
func (m Map) Equal(o Type) bool {
2222
v, ok := o.(Map)
2323
if !ok {
2424
return false
2525
}
26-
if v.AttributeType == nil || m.AttributeType == nil {
26+
if v.ElementType == nil || m.ElementType == nil {
2727
// when doing exact comparisons, we can't compare types that
2828
// don't have element types set, so we just consider them not
2929
// equal
3030
return false
3131
}
32-
return m.AttributeType.Equal(v.AttributeType)
32+
return m.ElementType.Equal(v.ElementType)
3333
}
3434

3535
// UsableAs returns whether the two Maps are type compatible.
3636
//
3737
// If the other type is DynamicPseudoType, it will return true.
3838
// If the other type is not a Map, it will return false.
39-
// If the other Map does not have a type compatible AttributeType, it will
39+
// If the other Map does not have a type compatible ElementType, it will
4040
// return false.
4141
func (m Map) UsableAs(o Type) bool {
4242
if o.Is(DynamicPseudoType) {
@@ -46,18 +46,18 @@ func (m Map) UsableAs(o Type) bool {
4646
if !ok {
4747
return false
4848
}
49-
return m.AttributeType.UsableAs(v.AttributeType)
49+
return m.ElementType.UsableAs(v.ElementType)
5050
}
5151

5252
// Is returns whether `t` is a Map type or not. It does not perform any
53-
// AttributeType checks.
53+
// ElementType checks.
5454
func (m Map) Is(t Type) bool {
5555
_, ok := t.(Map)
5656
return ok
5757
}
5858

5959
func (m Map) String() string {
60-
return "tftypes.Map[" + m.AttributeType.String() + "]"
60+
return "tftypes.Map[" + m.ElementType.String() + "]"
6161
}
6262

6363
func (m Map) private() {}
@@ -67,13 +67,13 @@ func (m Map) supportedGoTypes() []string {
6767
}
6868

6969
// MarshalJSON returns a JSON representation of the full type signature of `m`,
70-
// including its AttributeType.
70+
// including its ElementType.
7171
//
7272
// Deprecated: this is not meant to be called by third-party code.
7373
func (m Map) MarshalJSON() ([]byte, error) {
74-
attributeType, err := m.AttributeType.MarshalJSON()
74+
attributeType, err := m.ElementType.MarshalJSON()
7575
if err != nil {
76-
return nil, fmt.Errorf("error marshaling tftypes.Map's attribute type %T to JSON: %w", m.AttributeType, err)
76+
return nil, fmt.Errorf("error marshaling tftypes.Map's attribute type %T to JSON: %w", m.ElementType, err)
7777
}
7878
return []byte(`["map",` + string(attributeType) + `]`), nil
7979
}
@@ -102,7 +102,7 @@ func valueFromMap(typ Type, in interface{}) (Value, error) {
102102
}
103103
}
104104
return Value{
105-
typ: Map{AttributeType: typ},
105+
typ: Map{ElementType: typ},
106106
value: value,
107107
}, nil
108108
default:

tftypes/map_test.go

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,35 @@ func TestMapEqual(t *testing.T) {
1212
}
1313
tests := map[string]testCase{
1414
"equal": {
15-
m1: Map{AttributeType: String},
16-
m2: Map{AttributeType: String},
15+
m1: Map{ElementType: String},
16+
m2: Map{ElementType: String},
1717
equal: true,
1818
},
1919
"unequal": {
20-
m1: Map{AttributeType: String},
21-
m2: Map{AttributeType: Number},
20+
m1: Map{ElementType: String},
21+
m2: Map{ElementType: Number},
2222
equal: false,
2323
},
2424
"equal-complex": {
25-
m1: Map{AttributeType: Object{AttributeTypes: map[string]Type{
25+
m1: Map{ElementType: Object{AttributeTypes: map[string]Type{
2626
"a": Number,
2727
"b": String,
2828
"c": Bool,
2929
}}},
30-
m2: Map{AttributeType: Object{AttributeTypes: map[string]Type{
30+
m2: Map{ElementType: Object{AttributeTypes: map[string]Type{
3131
"a": Number,
3232
"b": String,
3333
"c": Bool,
3434
}}},
3535
equal: true,
3636
},
3737
"unequal-complex": {
38-
m1: Map{AttributeType: Object{AttributeTypes: map[string]Type{
38+
m1: Map{ElementType: Object{AttributeTypes: map[string]Type{
3939
"a": Number,
4040
"b": String,
4141
"c": Bool,
4242
}}},
43-
m2: Map{AttributeType: Object{AttributeTypes: map[string]Type{
43+
m2: Map{ElementType: Object{AttributeTypes: map[string]Type{
4444
"a": Number,
4545
"b": String,
4646
"c": Bool,
@@ -49,17 +49,17 @@ func TestMapEqual(t *testing.T) {
4949
equal: false,
5050
},
5151
"unequal-empty": {
52-
m1: Map{AttributeType: String},
52+
m1: Map{ElementType: String},
5353
m2: Map{},
5454
equal: false,
5555
},
5656
"unequal-complex-empty": {
57-
m1: Map{AttributeType: Object{AttributeTypes: map[string]Type{
57+
m1: Map{ElementType: Object{AttributeTypes: map[string]Type{
5858
"a": Number,
5959
"b": String,
6060
"c": Bool,
6161
}}},
62-
m2: Map{AttributeType: Object{}},
62+
m2: Map{ElementType: Object{}},
6363
equal: false,
6464
},
6565
}
@@ -90,35 +90,35 @@ func TestMapIs(t *testing.T) {
9090
}
9191
tests := map[string]testCase{
9292
"equal": {
93-
m1: Map{AttributeType: String},
94-
m2: Map{AttributeType: String},
93+
m1: Map{ElementType: String},
94+
m2: Map{ElementType: String},
9595
equal: true,
9696
},
9797
"different-attributetype": {
98-
m1: Map{AttributeType: String},
99-
m2: Map{AttributeType: Number},
98+
m1: Map{ElementType: String},
99+
m2: Map{ElementType: Number},
100100
equal: true,
101101
},
102102
"equal-complex": {
103-
m1: Map{AttributeType: Object{AttributeTypes: map[string]Type{
103+
m1: Map{ElementType: Object{AttributeTypes: map[string]Type{
104104
"a": Number,
105105
"b": String,
106106
"c": Bool,
107107
}}},
108-
m2: Map{AttributeType: Object{AttributeTypes: map[string]Type{
108+
m2: Map{ElementType: Object{AttributeTypes: map[string]Type{
109109
"a": Number,
110110
"b": String,
111111
"c": Bool,
112112
}}},
113113
equal: true,
114114
},
115115
"different-attributetype-complex": {
116-
m1: Map{AttributeType: Object{AttributeTypes: map[string]Type{
116+
m1: Map{ElementType: Object{AttributeTypes: map[string]Type{
117117
"a": Number,
118118
"b": String,
119119
"c": Bool,
120120
}}},
121-
m2: Map{AttributeType: Object{AttributeTypes: map[string]Type{
121+
m2: Map{ElementType: Object{AttributeTypes: map[string]Type{
122122
"a": Number,
123123
"b": String,
124124
"c": Bool,
@@ -127,21 +127,21 @@ func TestMapIs(t *testing.T) {
127127
equal: true,
128128
},
129129
"equal-empty": {
130-
m1: Map{AttributeType: String},
130+
m1: Map{ElementType: String},
131131
m2: Map{},
132132
equal: true,
133133
},
134134
"equal-complex-empty": {
135-
m1: Map{AttributeType: Object{AttributeTypes: map[string]Type{
135+
m1: Map{ElementType: Object{AttributeTypes: map[string]Type{
136136
"a": Number,
137137
"b": String,
138138
"c": Bool,
139139
}}},
140-
m2: Map{AttributeType: Object{}},
140+
m2: Map{ElementType: Object{}},
141141
equal: true,
142142
},
143143
"equal-complex-nil": {
144-
m1: Map{AttributeType: Object{AttributeTypes: map[string]Type{
144+
m1: Map{ElementType: Object{AttributeTypes: map[string]Type{
145145
"a": Number,
146146
"b": String,
147147
"c": Bool,
@@ -173,72 +173,72 @@ func TestMapUsableAs(t *testing.T) {
173173
}
174174
tests := map[string]testCase{
175175
"map-map-string-map-string": {
176-
m: Map{AttributeType: Map{AttributeType: String}},
177-
other: Map{AttributeType: String},
176+
m: Map{ElementType: Map{ElementType: String}},
177+
other: Map{ElementType: String},
178178
expected: false,
179179
},
180180
"map-map-string-map-map-string": {
181-
m: Map{AttributeType: Map{AttributeType: String}},
182-
other: Map{AttributeType: Map{AttributeType: String}},
181+
m: Map{ElementType: Map{ElementType: String}},
182+
other: Map{ElementType: Map{ElementType: String}},
183183
expected: true,
184184
},
185185
"map-map-string-dpt": {
186-
m: Map{AttributeType: Map{AttributeType: String}},
186+
m: Map{ElementType: Map{ElementType: String}},
187187
other: DynamicPseudoType,
188188
expected: true,
189189
},
190190
"map-map-string-map-dpt": {
191-
m: Map{AttributeType: Map{AttributeType: String}},
192-
other: Map{AttributeType: DynamicPseudoType},
191+
m: Map{ElementType: Map{ElementType: String}},
192+
other: Map{ElementType: DynamicPseudoType},
193193
expected: true,
194194
},
195195
"map-map-string-map-map-dpt": {
196-
m: Map{AttributeType: Map{AttributeType: String}},
197-
other: Map{AttributeType: Map{AttributeType: DynamicPseudoType}},
196+
m: Map{ElementType: Map{ElementType: String}},
197+
other: Map{ElementType: Map{ElementType: DynamicPseudoType}},
198198
expected: true,
199199
},
200200
"map-string-dpt": {
201-
m: Map{AttributeType: String},
201+
m: Map{ElementType: String},
202202
other: DynamicPseudoType,
203203
expected: true,
204204
},
205205
"map-string-list-string": {
206-
m: Map{AttributeType: String},
206+
m: Map{ElementType: String},
207207
other: List{ElementType: String},
208208
expected: false,
209209
},
210210
"map-string-map-bool": {
211-
m: Map{AttributeType: String},
212-
other: Map{AttributeType: Bool},
211+
m: Map{ElementType: String},
212+
other: Map{ElementType: Bool},
213213
expected: false,
214214
},
215215
"map-string-map-dpt": {
216-
m: Map{AttributeType: String},
217-
other: Map{AttributeType: DynamicPseudoType},
216+
m: Map{ElementType: String},
217+
other: Map{ElementType: DynamicPseudoType},
218218
expected: true,
219219
},
220220
"map-string-map-string": {
221-
m: Map{AttributeType: String},
222-
other: Map{AttributeType: String},
221+
m: Map{ElementType: String},
222+
other: Map{ElementType: String},
223223
expected: true,
224224
},
225225
"map-string-object": {
226-
m: Map{AttributeType: String},
226+
m: Map{ElementType: String},
227227
other: Object{AttributeTypes: map[string]Type{"test": String}},
228228
expected: false,
229229
},
230230
"map-string-primitive": {
231-
m: Map{AttributeType: String},
231+
m: Map{ElementType: String},
232232
other: String,
233233
expected: false,
234234
},
235235
"map-string-set-string": {
236-
m: Map{AttributeType: String},
236+
m: Map{ElementType: String},
237237
other: Set{ElementType: String},
238238
expected: false,
239239
},
240240
"map-string-tuple-string": {
241-
m: Map{AttributeType: String},
241+
m: Map{ElementType: String},
242242
other: Tuple{ElementTypes: []Type{String}},
243243
expected: false,
244244
},

tftypes/object.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (o Object) Equal(other Type) bool {
8585
// If the other type is not a Object, it will return false.
8686
// If the other Object does not have matching AttributeTypes length, it will
8787
// return false.
88-
// If the other Object does not have a type compatible AttributeType for every
88+
// If the other Object does not have a type compatible ElementType for every
8989
// nested attribute, it will return false.
9090
//
9191
// If the current type contains OptionalAttributes, it will panic.

tftypes/set_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func TestSetUsableAs(t *testing.T) {
219219
},
220220
"set-string-map": {
221221
set: Set{ElementType: String},
222-
other: Map{AttributeType: String},
222+
other: Map{ElementType: String},
223223
expected: false,
224224
},
225225
"set-string-object": {

tftypes/tuple_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func TestTupleUsableAs(t *testing.T) {
219219
},
220220
"tuple-string-map": {
221221
tuple: Tuple{ElementTypes: []Type{String}},
222-
other: Map{AttributeType: String},
222+
other: Map{ElementType: String},
223223
expected: false,
224224
},
225225
"tuple-string-object": {

tftypes/type.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func (t *jsonType) UnmarshalJSON(buf []byte) error {
143143
return err
144144
}
145145
t.t = Map{
146-
AttributeType: ety.t,
146+
ElementType: ety.t,
147147
}
148148
case "set":
149149
var ety jsonType

0 commit comments

Comments
 (0)