Skip to content

Commit

Permalink
Add support for MapNestedAttributes.
Browse files Browse the repository at this point in the history
Fill in tests for maps.

Add MapNestedAttributes and tests for it.
  • Loading branch information
paddycarver committed Jul 21, 2021
1 parent dacb915 commit f89b01b
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .changelog/79.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
Added support for MapNestedAttributes.
```
24 changes: 22 additions & 2 deletions internal/proto6/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ func TestSchema(t *testing.T) {
}},
Optional: true,
},
// TODO: add map support when it lands
"map": {
Type: types.MapType{ElemType: types.NumberType},
Computed: true,
},
// TODO: add tuple support when it lands
// TODO: add set support when it lands
},
Expand All @@ -98,6 +101,11 @@ func TestSchema(t *testing.T) {
Type: tftypes.List{ElementType: tftypes.String},
Required: true,
},
{
Name: "map",
Type: tftypes.Map{AttributeType: tftypes.Number},
Computed: true,
},
{
Name: "object",
Type: tftypes.Object{AttributeTypes: map[string]tftypes.Type{
Expand Down Expand Up @@ -572,6 +580,19 @@ func TestAttribute(t *testing.T) {
Optional: true,
},
},
"attr-map": {
name: "map",
attr: schema.Attribute{
Type: types.MapType{ElemType: types.StringType},
Optional: true,
},
path: tftypes.NewAttributePath(),
expected: &tfprotov6.SchemaAttribute{
Name: "map",
Type: tftypes.Map{AttributeType: tftypes.String},
Optional: true,
},
},
"attr-object": {
name: "object",
attr: schema.Attribute{
Expand All @@ -593,7 +614,6 @@ func TestAttribute(t *testing.T) {
Optional: true,
},
},
// TODO: add map attribute when we support it
// TODO: add set attribute when we support it
// TODO: add tuple attribute when we support it
"required": {
Expand Down
5 changes: 3 additions & 2 deletions schema/nested_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,9 @@ func (m mapNestedAttributes) GetMaxItems() int64 {

// AttributeType returns an attr.Type corresponding to the nested attributes.
func (m mapNestedAttributes) AttributeType() attr.Type {
// TODO fill in implementation when types.MapType is available
return nil
return types.MapType{
ElemType: m.nestedAttributes.AttributeType(),
}
}

func (m mapNestedAttributes) ApplyTerraform5AttributePathStep(step tftypes.AttributePathStep) (interface{}, error) {
Expand Down
52 changes: 50 additions & 2 deletions tfsdk/serve_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ func (t *testServeProvider) GetSchema(_ context.Context) (schema.Schema, []*tfpr
Type: types.ObjectType{},
Optional: true,
},
// TODO: add maps when we support them
"map": {
Type: types.MapType{ElemType: types.NumberType},
Optional: true,
},
// TODO: add sets when we support them
// TODO: add tuples when we support them
"single-nested-attributes": {
Expand Down Expand Up @@ -167,6 +170,20 @@ func (t *testServeProvider) GetSchema(_ context.Context) (schema.Schema, []*tfpr
}, schema.ListNestedAttributesOptions{}),
Optional: true,
},
"map-nested-attributes": {
Attributes: schema.MapNestedAttributes(map[string]schema.Attribute{
"foo": {
Type: types.StringType,
Optional: true,
Computed: true,
},
"bar": {
Type: types.NumberType,
Required: true,
},
}, schema.MapNestedAttributesOptions{}),
Optional: true,
},
},
}, nil
}
Expand Down Expand Up @@ -248,6 +265,33 @@ var testServeProviderProviderSchema = &tfprotov6.Schema{
},
Optional: true,
},
{
Name: "map",
Type: tftypes.Map{
AttributeType: tftypes.Number,
},
Optional: true,
},
{
Name: "map-nested-attributes",
Optional: true,
NestedType: &tfprotov6.SchemaObject{
Nesting: tfprotov6.SchemaObjectNestingModeMap,
Attributes: []*tfprotov6.SchemaAttribute{
{
Name: "bar",
Type: tftypes.Number,
Required: true,
},
{
Name: "foo",
Type: tftypes.String,
Optional: true,
Computed: true,
},
},
},
},
{
Name: "number",
Type: tftypes.Number,
Expand Down Expand Up @@ -314,7 +358,6 @@ var testServeProviderProviderSchema = &tfprotov6.Schema{
Type: tftypes.String,
Optional: true,
},
// TODO: add maps when we support them
// TODO: add sets when we support them
// TODO: add tuples when we support them
},
Expand All @@ -339,6 +382,7 @@ var testServeProviderProviderType = tftypes.Object{
"bar": tftypes.Bool,
"baz": tftypes.Number,
}}},
"map": tftypes.Map{AttributeType: tftypes.Number},
"object": tftypes.Object{AttributeTypes: map[string]tftypes.Type{
"foo": tftypes.String,
"bar": tftypes.Bool,
Expand All @@ -354,6 +398,10 @@ var testServeProviderProviderType = tftypes.Object{
"foo": tftypes.String,
"bar": tftypes.Number,
}}},
"map-nested-attributes": tftypes.Map{AttributeType: tftypes.Object{AttributeTypes: map[string]tftypes.Type{
"foo": tftypes.String,
"bar": tftypes.Number,
}}},
},
}

Expand Down
48 changes: 48 additions & 0 deletions tfsdk/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,30 @@ func TestServerConfigureProvider(t *testing.T) {
"baz": tftypes.NewValue(tftypes.Number, 8675309),
}),
}),
"map": tftypes.NewValue(tftypes.Map{AttributeType: tftypes.Number}, map[string]tftypes.Value{
"foo": tftypes.NewValue(tftypes.Number, 123),
"bar": tftypes.NewValue(tftypes.Number, 456),
"baz": tftypes.NewValue(tftypes.Number, 789),
}),
"map-nested-attributes": tftypes.NewValue(tftypes.Map{AttributeType: tftypes.Object{AttributeTypes: map[string]tftypes.Type{
"bar": tftypes.Number,
"foo": tftypes.String,
}}}, map[string]tftypes.Value{
"hello": tftypes.NewValue(tftypes.Object{AttributeTypes: map[string]tftypes.Type{
"bar": tftypes.Number,
"foo": tftypes.String,
}}, map[string]tftypes.Value{
"bar": tftypes.NewValue(tftypes.Number, 123456),
"foo": tftypes.NewValue(tftypes.String, "world"),
}),
"goodnight": tftypes.NewValue(tftypes.Object{AttributeTypes: map[string]tftypes.Type{
"bar": tftypes.Number,
"foo": tftypes.String,
}}, map[string]tftypes.Value{
"bar": tftypes.NewValue(tftypes.Number, 56789),
"foo": tftypes.NewValue(tftypes.String, "moon"),
}),
}),
"object": tftypes.NewValue(tftypes.Object{AttributeTypes: map[string]tftypes.Type{
"foo": tftypes.String,
"bar": tftypes.Bool,
Expand Down Expand Up @@ -422,6 +446,30 @@ func TestServerConfigureProvider(t *testing.T) {
"foo": tftypes.String,
"bar": tftypes.Number,
}}}, tftypes.UnknownValue),
"map": tftypes.NewValue(tftypes.Map{AttributeType: tftypes.Number}, map[string]tftypes.Value{
"foo": tftypes.NewValue(tftypes.Number, 123),
"bar": tftypes.NewValue(tftypes.Number, 456),
"baz": tftypes.NewValue(tftypes.Number, 789),
}),
"map-nested-attributes": tftypes.NewValue(tftypes.Map{AttributeType: tftypes.Object{AttributeTypes: map[string]tftypes.Type{
"bar": tftypes.Number,
"foo": tftypes.String,
}}}, map[string]tftypes.Value{
"hello": tftypes.NewValue(tftypes.Object{AttributeTypes: map[string]tftypes.Type{
"bar": tftypes.Number,
"foo": tftypes.String,
}}, map[string]tftypes.Value{
"bar": tftypes.NewValue(tftypes.Number, 123456),
"foo": tftypes.NewValue(tftypes.String, "world"),
}),
"goodnight": tftypes.NewValue(tftypes.Object{AttributeTypes: map[string]tftypes.Type{
"bar": tftypes.Number,
"foo": tftypes.String,
}}, map[string]tftypes.Value{
"bar": tftypes.NewValue(tftypes.Number, 56789),
"foo": tftypes.NewValue(tftypes.String, "moon"),
}),
}),
}),
},
}
Expand Down

0 comments on commit f89b01b

Please sign in to comment.