1
1
package conversion
2
2
3
3
import (
4
+ "maps"
4
5
"reflect"
5
6
"slices"
6
7
@@ -22,20 +23,17 @@ type PluralDataSourceSchemaRequest struct {
22
23
}
23
24
24
25
func DataSourceSchemaFromResource (rs schema.Schema , req * DataSourceSchemaRequest ) dsschema.Schema {
25
- blocks := convertBlocks (rs .Blocks , req .RequiredFields )
26
26
attrs := convertAttrs (rs .Attributes , req .RequiredFields )
27
+ maps .Copy (attrs , convertBlocksToAttrs (rs .Blocks , req .RequiredFields ))
27
28
overrideFields (attrs , req .OverridenFields )
28
- ds := dsschema.Schema {Attributes : attrs , Blocks : blocks }
29
+ ds := dsschema.Schema {Attributes : attrs }
29
30
UpdateSchemaDescription (& ds )
30
31
return ds
31
32
}
32
33
33
34
func PluralDataSourceSchemaFromResource (rs schema.Schema , req * PluralDataSourceSchemaRequest ) dsschema.Schema {
34
- blocks := convertBlocks (rs .Blocks , nil )
35
- if len (blocks ) > 0 {
36
- panic ("blocks not supported yet in auto-generated plural data source schema as they can't go in ListNestedAttribute" )
37
- }
38
35
attrs := convertAttrs (rs .Attributes , nil )
36
+ maps .Copy (attrs , convertBlocksToAttrs (rs .Blocks , nil ))
39
37
overrideFields (attrs , req .OverridenFields )
40
38
rootAttrs := convertAttrs (rs .Attributes , req .RequiredFields )
41
39
for name := range rootAttrs {
@@ -76,12 +74,14 @@ var convertMappings = map[string]reflect.Type{
76
74
"Int64Attribute" : reflect .TypeOf (dsschema.Int64Attribute {}),
77
75
"Float64Attribute" : reflect .TypeOf (dsschema.Float64Attribute {}),
78
76
"MapAttribute" : reflect .TypeOf (dsschema.MapAttribute {}),
77
+ "ListAttribute" : reflect .TypeOf (dsschema.ListAttribute {}),
78
+ "SetAttribute" : reflect .TypeOf (dsschema.SetAttribute {}),
79
79
"SingleNestedAttribute" : reflect .TypeOf (dsschema.SingleNestedAttribute {}),
80
80
"ListNestedAttribute" : reflect .TypeOf (dsschema.ListNestedAttribute {}),
81
81
"SetNestedAttribute" : reflect .TypeOf (dsschema.SetNestedAttribute {}),
82
- "ListAttribute " : reflect .TypeOf (dsschema.ListAttribute {}),
83
- "SetNestedBlock " : reflect .TypeOf (dsschema.SetNestedBlock {}),
84
- "SetAttribute " : reflect .TypeOf (dsschema.SetAttribute {}),
82
+ "SingleNestedBlock " : reflect .TypeOf (dsschema.SingleNestedAttribute {}),
83
+ "ListNestedBlock " : reflect .TypeOf (dsschema.ListNestedAttribute {}),
84
+ "SetNestedBlock " : reflect .TypeOf (dsschema.SetNestedAttribute {}),
85
85
}
86
86
87
87
var convertNestedMappings = map [string ]reflect.Type {
@@ -91,9 +91,6 @@ var convertNestedMappings = map[string]reflect.Type{
91
91
92
92
func convertAttrs (rsAttrs map [string ]schema.Attribute , requiredFields []string ) map [string ]dsschema.Attribute {
93
93
const ignoreField = "timeouts"
94
- if rsAttrs == nil {
95
- return nil
96
- }
97
94
dsAttrs := make (map [string ]dsschema.Attribute , len (rsAttrs ))
98
95
for name , attr := range rsAttrs {
99
96
if name == ignoreField {
@@ -104,15 +101,12 @@ func convertAttrs(rsAttrs map[string]schema.Attribute, requiredFields []string)
104
101
return dsAttrs
105
102
}
106
103
107
- func convertBlocks (rsBlocks map [string ]schema.Block , requiredFields []string ) map [string ]dsschema.Block {
108
- if rsBlocks == nil {
109
- return nil
110
- }
111
- dsBlocks := make (map [string ]dsschema.Block , len (rsBlocks ))
104
+ func convertBlocksToAttrs (rsBlocks map [string ]schema.Block , requiredFields []string ) map [string ]dsschema.Attribute {
105
+ dsAttrs := make (map [string ]dsschema.Attribute , len (rsBlocks ))
112
106
for name , block := range rsBlocks {
113
- dsBlocks [name ] = convertElement (name , block , requiredFields ).(dsschema.Block )
107
+ dsAttrs [name ] = convertElement (name , block , requiredFields ).(dsschema.Attribute )
114
108
}
115
- return dsBlocks
109
+ return dsAttrs
116
110
}
117
111
118
112
func convertElement (name string , element any , requiredFields []string ) any {
@@ -131,8 +125,8 @@ func convertElement(name string, element any, requiredFields []string) any {
131
125
vDest := reflect .New (tDest ).Elem ()
132
126
vDest .FieldByName ("MarkdownDescription" ).Set (vSrc .FieldByName ("MarkdownDescription" ))
133
127
vDest .FieldByName ("DeprecationMessage" ).Set (vSrc .FieldByName ("DeprecationMessage" ))
134
- if fSensitive := vDest .FieldByName ("Sensitive" ); fSensitive .CanSet () {
135
- fSensitive .Set (vSrc . FieldByName ( "Sensitive" ) )
128
+ if fSensitive , sSensitive := vDest .FieldByName ("Sensitive" ), vSrc . FieldByName ( "Sensitive" ) ; fSensitive .CanSet () && sSensitive . IsValid () {
129
+ fSensitive .Set (sSensitive )
136
130
}
137
131
if fComputed := vDest .FieldByName ("Computed" ); fComputed .CanSet () {
138
132
fComputed .SetBool (computed )
@@ -143,23 +137,35 @@ func convertElement(name string, element any, requiredFields []string) any {
143
137
if fElementType := vDest .FieldByName ("ElementType" ); fElementType .CanSet () {
144
138
fElementType .Set (vSrc .FieldByName ("ElementType" ))
145
139
}
146
- if fAttributes := vDest .FieldByName ("Attributes" ); fAttributes .CanSet () {
147
- attrsSrc := vSrc .FieldByName ("Attributes" ).Interface ().(map [string ]schema.Attribute )
148
- fAttributes .Set (reflect .ValueOf (convertAttrs (attrsSrc , nil )))
149
- }
140
+ fillNestedAttrs (vDest , vSrc )
141
+
150
142
if fNested := vDest .FieldByName ("NestedObject" ); fNested .CanSet () {
151
143
tNested := convertNestedMappings [fNested .Type ().Name ()]
152
144
if tNested == nil {
153
145
panic ("nested type not support yet, add it to convertNestedMappings: " + fNested .Type ().Name ())
154
146
}
155
- attrsSrc := vSrc .FieldByName ("NestedObject" ).FieldByName ("Attributes" ).Interface ().(map [string ]schema.Attribute )
156
147
vNested := reflect .New (tNested ).Elem ()
157
- vNested .FieldByName ("Attributes" ). Set ( reflect . ValueOf ( convertAttrs ( attrsSrc , nil ) ))
148
+ fillNestedAttrs ( vNested , vSrc .FieldByName ("NestedObject" ))
158
149
fNested .Set (vNested )
159
150
}
160
151
return vDest .Interface ()
161
152
}
162
153
154
+ func fillNestedAttrs (vDest , vSrc reflect.Value ) {
155
+ fAttributes := vDest .FieldByName ("Attributes" )
156
+ if ! fAttributes .CanSet () {
157
+ return
158
+ }
159
+ attrsSrc := vSrc .FieldByName ("Attributes" ).Interface ().(map [string ]schema.Attribute )
160
+ attrSrcDS := convertAttrs (attrsSrc , nil )
161
+ if fBlocks := vSrc .FieldByName ("Blocks" ); fBlocks .IsValid () {
162
+ blocksSrc := fBlocks .Interface ().(map [string ]schema.Block )
163
+ blockSrcDS := convertBlocksToAttrs (blocksSrc , nil )
164
+ maps .Copy (attrSrcDS , blockSrcDS )
165
+ }
166
+ fAttributes .Set (reflect .ValueOf (attrSrcDS ))
167
+ }
168
+
163
169
func overrideFields (attrs , overridenFields map [string ]dsschema.Attribute ) {
164
170
for name , attr := range overridenFields {
165
171
if attr == nil {
0 commit comments