Skip to content

Commit fa8d1fe

Browse files
authored
tfprotov5+6: Add ValueType implementation to identity schemas (#497)
1 parent 45caeec commit fa8d1fe

4 files changed

+332
-0
lines changed

tfprotov5/resource_identity_schema.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,37 @@ type ResourceIdentitySchema struct {
2121
IdentityAttributes []*ResourceIdentitySchemaAttribute
2222
}
2323

24+
// ValueType returns the tftypes.Type for a ResourceIdentitySchema.
25+
//
26+
// If ResourceIdentitySchema is missing, an empty Object is returned.
27+
func (s *ResourceIdentitySchema) ValueType() tftypes.Type {
28+
if s == nil {
29+
return tftypes.Object{
30+
AttributeTypes: map[string]tftypes.Type{},
31+
}
32+
}
33+
34+
attributeTypes := map[string]tftypes.Type{}
35+
36+
for _, attribute := range s.IdentityAttributes {
37+
if attribute == nil {
38+
continue
39+
}
40+
41+
attributeType := attribute.ValueType()
42+
43+
if attributeType == nil {
44+
continue
45+
}
46+
47+
attributeTypes[attribute.Name] = attributeType
48+
}
49+
50+
return tftypes.Object{
51+
AttributeTypes: attributeTypes,
52+
}
53+
}
54+
2455
// ResourceIdentitySchemaAttribute represents one value of data within
2556
// resource identity.
2657
// These are always used in resource identity comparisons.
@@ -56,3 +87,14 @@ type ResourceIdentitySchemaAttribute struct {
5687
// Description is a human-readable description of the attribute.
5788
Description string
5889
}
90+
91+
// ValueType returns the tftypes.Type for a ResourceIdentitySchemaAttribute.
92+
//
93+
// If ResourceIdentitySchemaAttribute is missing, nil is returned.
94+
func (s *ResourceIdentitySchemaAttribute) ValueType() tftypes.Type {
95+
if s == nil {
96+
return nil
97+
}
98+
99+
return s.Type
100+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package tfprotov5_test
5+
6+
import (
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
10+
"github.com/hashicorp/terraform-plugin-go/tftypes"
11+
)
12+
13+
func TestResourceIdentitySchemaValueType(t *testing.T) {
14+
t.Parallel()
15+
16+
testCases := map[string]struct {
17+
identitySchema *tfprotov5.ResourceIdentitySchema
18+
expected tftypes.Type
19+
}{
20+
"nil": {
21+
identitySchema: nil,
22+
expected: tftypes.Object{
23+
AttributeTypes: map[string]tftypes.Type{},
24+
},
25+
},
26+
"Attribute-String": {
27+
identitySchema: &tfprotov5.ResourceIdentitySchema{
28+
IdentityAttributes: []*tfprotov5.ResourceIdentitySchemaAttribute{
29+
{
30+
Name: "test_string_attribute",
31+
Type: tftypes.String,
32+
},
33+
},
34+
},
35+
expected: tftypes.Object{
36+
AttributeTypes: map[string]tftypes.Type{
37+
"test_string_attribute": tftypes.String,
38+
},
39+
},
40+
},
41+
}
42+
43+
for name, testCase := range testCases {
44+
t.Run(name, func(t *testing.T) {
45+
t.Parallel()
46+
47+
got := testCase.identitySchema.ValueType()
48+
49+
if testCase.expected == nil {
50+
if got == nil {
51+
return
52+
}
53+
54+
t.Fatalf("expected nil, got: %s", got)
55+
}
56+
57+
if !testCase.expected.Equal(got) {
58+
t.Errorf("expected %s, got: %s", testCase.expected, got)
59+
}
60+
})
61+
}
62+
}
63+
64+
func TestResourceIdentitySchemaAttributeValueType(t *testing.T) {
65+
t.Parallel()
66+
67+
testCases := map[string]struct {
68+
identitySchemaAttribute *tfprotov5.ResourceIdentitySchemaAttribute
69+
expected tftypes.Type
70+
}{
71+
"nil": {
72+
identitySchemaAttribute: nil,
73+
expected: nil,
74+
},
75+
"Bool": {
76+
identitySchemaAttribute: &tfprotov5.ResourceIdentitySchemaAttribute{
77+
Type: tftypes.Bool,
78+
},
79+
expected: tftypes.Bool,
80+
},
81+
"List-String": {
82+
identitySchemaAttribute: &tfprotov5.ResourceIdentitySchemaAttribute{
83+
Type: tftypes.List{
84+
ElementType: tftypes.String,
85+
},
86+
},
87+
expected: tftypes.List{
88+
ElementType: tftypes.String,
89+
},
90+
},
91+
"Number": {
92+
identitySchemaAttribute: &tfprotov5.ResourceIdentitySchemaAttribute{
93+
Type: tftypes.Number,
94+
},
95+
expected: tftypes.Number,
96+
},
97+
"String": {
98+
identitySchemaAttribute: &tfprotov5.ResourceIdentitySchemaAttribute{
99+
Type: tftypes.String,
100+
},
101+
expected: tftypes.String,
102+
},
103+
}
104+
105+
for name, testCase := range testCases {
106+
t.Run(name, func(t *testing.T) {
107+
t.Parallel()
108+
109+
got := testCase.identitySchemaAttribute.ValueType()
110+
111+
if testCase.expected == nil {
112+
if got == nil {
113+
return
114+
}
115+
116+
t.Fatalf("expected nil, got: %s", got)
117+
}
118+
119+
if !testCase.expected.Equal(got) {
120+
t.Errorf("expected %s, got: %s", testCase.expected, got)
121+
}
122+
})
123+
}
124+
}

tfprotov6/resource_identity_schema.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,37 @@ type ResourceIdentitySchema struct {
2121
IdentityAttributes []*ResourceIdentitySchemaAttribute
2222
}
2323

24+
// ValueType returns the tftypes.Type for a ResourceIdentitySchema.
25+
//
26+
// If ResourceIdentitySchema is missing, an empty Object is returned.
27+
func (s *ResourceIdentitySchema) ValueType() tftypes.Type {
28+
if s == nil {
29+
return tftypes.Object{
30+
AttributeTypes: map[string]tftypes.Type{},
31+
}
32+
}
33+
34+
attributeTypes := map[string]tftypes.Type{}
35+
36+
for _, attribute := range s.IdentityAttributes {
37+
if attribute == nil {
38+
continue
39+
}
40+
41+
attributeType := attribute.ValueType()
42+
43+
if attributeType == nil {
44+
continue
45+
}
46+
47+
attributeTypes[attribute.Name] = attributeType
48+
}
49+
50+
return tftypes.Object{
51+
AttributeTypes: attributeTypes,
52+
}
53+
}
54+
2455
// ResourceIdentitySchemaAttribute represents one value of data within
2556
// resource identity.
2657
// These are always used in resource identity comparisons.
@@ -56,3 +87,14 @@ type ResourceIdentitySchemaAttribute struct {
5687
// Description is a human-readable description of the attribute.
5788
Description string
5889
}
90+
91+
// ValueType returns the tftypes.Type for a ResourceIdentitySchemaAttribute.
92+
//
93+
// If ResourceIdentitySchemaAttribute is missing, nil is returned.
94+
func (s *ResourceIdentitySchemaAttribute) ValueType() tftypes.Type {
95+
if s == nil {
96+
return nil
97+
}
98+
99+
return s.Type
100+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package tfprotov6_test
5+
6+
import (
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
10+
"github.com/hashicorp/terraform-plugin-go/tftypes"
11+
)
12+
13+
func TestResourceIdentitySchemaValueType(t *testing.T) {
14+
t.Parallel()
15+
16+
testCases := map[string]struct {
17+
identitySchema *tfprotov6.ResourceIdentitySchema
18+
expected tftypes.Type
19+
}{
20+
"nil": {
21+
identitySchema: nil,
22+
expected: tftypes.Object{
23+
AttributeTypes: map[string]tftypes.Type{},
24+
},
25+
},
26+
"Attribute-String": {
27+
identitySchema: &tfprotov6.ResourceIdentitySchema{
28+
IdentityAttributes: []*tfprotov6.ResourceIdentitySchemaAttribute{
29+
{
30+
Name: "test_string_attribute",
31+
Type: tftypes.String,
32+
},
33+
},
34+
},
35+
expected: tftypes.Object{
36+
AttributeTypes: map[string]tftypes.Type{
37+
"test_string_attribute": tftypes.String,
38+
},
39+
},
40+
},
41+
}
42+
43+
for name, testCase := range testCases {
44+
t.Run(name, func(t *testing.T) {
45+
t.Parallel()
46+
47+
got := testCase.identitySchema.ValueType()
48+
49+
if testCase.expected == nil {
50+
if got == nil {
51+
return
52+
}
53+
54+
t.Fatalf("expected nil, got: %s", got)
55+
}
56+
57+
if !testCase.expected.Equal(got) {
58+
t.Errorf("expected %s, got: %s", testCase.expected, got)
59+
}
60+
})
61+
}
62+
}
63+
64+
func TestResourceIdentitySchemaAttributeValueType(t *testing.T) {
65+
t.Parallel()
66+
67+
testCases := map[string]struct {
68+
identitySchemaAttribute *tfprotov6.ResourceIdentitySchemaAttribute
69+
expected tftypes.Type
70+
}{
71+
"nil": {
72+
identitySchemaAttribute: nil,
73+
expected: nil,
74+
},
75+
"Bool": {
76+
identitySchemaAttribute: &tfprotov6.ResourceIdentitySchemaAttribute{
77+
Type: tftypes.Bool,
78+
},
79+
expected: tftypes.Bool,
80+
},
81+
"List-String": {
82+
identitySchemaAttribute: &tfprotov6.ResourceIdentitySchemaAttribute{
83+
Type: tftypes.List{
84+
ElementType: tftypes.String,
85+
},
86+
},
87+
expected: tftypes.List{
88+
ElementType: tftypes.String,
89+
},
90+
},
91+
"Number": {
92+
identitySchemaAttribute: &tfprotov6.ResourceIdentitySchemaAttribute{
93+
Type: tftypes.Number,
94+
},
95+
expected: tftypes.Number,
96+
},
97+
"String": {
98+
identitySchemaAttribute: &tfprotov6.ResourceIdentitySchemaAttribute{
99+
Type: tftypes.String,
100+
},
101+
expected: tftypes.String,
102+
},
103+
}
104+
105+
for name, testCase := range testCases {
106+
t.Run(name, func(t *testing.T) {
107+
t.Parallel()
108+
109+
got := testCase.identitySchemaAttribute.ValueType()
110+
111+
if testCase.expected == nil {
112+
if got == nil {
113+
return
114+
}
115+
116+
t.Fatalf("expected nil, got: %s", got)
117+
}
118+
119+
if !testCase.expected.Equal(got) {
120+
t.Errorf("expected %s, got: %s", testCase.expected, got)
121+
}
122+
})
123+
}
124+
}

0 commit comments

Comments
 (0)