Skip to content

Commit 586dfc5

Browse files
authored
chore: Converts resource blocks to resource attributes in data source schema auto-generation (#2904)
* convert blocks to attributes * plural test with blocks * refactor * remove unneeded checks * apply feedback
1 parent 06140c9 commit 586dfc5

File tree

2 files changed

+264
-72
lines changed

2 files changed

+264
-72
lines changed

internal/common/conversion/schema_generation.go

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package conversion
22

33
import (
4+
"maps"
45
"reflect"
56
"slices"
67

@@ -22,20 +23,17 @@ type PluralDataSourceSchemaRequest struct {
2223
}
2324

2425
func DataSourceSchemaFromResource(rs schema.Schema, req *DataSourceSchemaRequest) dsschema.Schema {
25-
blocks := convertBlocks(rs.Blocks, req.RequiredFields)
2626
attrs := convertAttrs(rs.Attributes, req.RequiredFields)
27+
maps.Copy(attrs, convertBlocksToAttrs(rs.Blocks, req.RequiredFields))
2728
overrideFields(attrs, req.OverridenFields)
28-
ds := dsschema.Schema{Attributes: attrs, Blocks: blocks}
29+
ds := dsschema.Schema{Attributes: attrs}
2930
UpdateSchemaDescription(&ds)
3031
return ds
3132
}
3233

3334
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-
}
3835
attrs := convertAttrs(rs.Attributes, nil)
36+
maps.Copy(attrs, convertBlocksToAttrs(rs.Blocks, nil))
3937
overrideFields(attrs, req.OverridenFields)
4038
rootAttrs := convertAttrs(rs.Attributes, req.RequiredFields)
4139
for name := range rootAttrs {
@@ -76,12 +74,14 @@ var convertMappings = map[string]reflect.Type{
7674
"Int64Attribute": reflect.TypeOf(dsschema.Int64Attribute{}),
7775
"Float64Attribute": reflect.TypeOf(dsschema.Float64Attribute{}),
7876
"MapAttribute": reflect.TypeOf(dsschema.MapAttribute{}),
77+
"ListAttribute": reflect.TypeOf(dsschema.ListAttribute{}),
78+
"SetAttribute": reflect.TypeOf(dsschema.SetAttribute{}),
7979
"SingleNestedAttribute": reflect.TypeOf(dsschema.SingleNestedAttribute{}),
8080
"ListNestedAttribute": reflect.TypeOf(dsschema.ListNestedAttribute{}),
8181
"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{}),
8585
}
8686

8787
var convertNestedMappings = map[string]reflect.Type{
@@ -91,9 +91,6 @@ var convertNestedMappings = map[string]reflect.Type{
9191

9292
func convertAttrs(rsAttrs map[string]schema.Attribute, requiredFields []string) map[string]dsschema.Attribute {
9393
const ignoreField = "timeouts"
94-
if rsAttrs == nil {
95-
return nil
96-
}
9794
dsAttrs := make(map[string]dsschema.Attribute, len(rsAttrs))
9895
for name, attr := range rsAttrs {
9996
if name == ignoreField {
@@ -104,15 +101,12 @@ func convertAttrs(rsAttrs map[string]schema.Attribute, requiredFields []string)
104101
return dsAttrs
105102
}
106103

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))
112106
for name, block := range rsBlocks {
113-
dsBlocks[name] = convertElement(name, block, requiredFields).(dsschema.Block)
107+
dsAttrs[name] = convertElement(name, block, requiredFields).(dsschema.Attribute)
114108
}
115-
return dsBlocks
109+
return dsAttrs
116110
}
117111

118112
func convertElement(name string, element any, requiredFields []string) any {
@@ -131,8 +125,8 @@ func convertElement(name string, element any, requiredFields []string) any {
131125
vDest := reflect.New(tDest).Elem()
132126
vDest.FieldByName("MarkdownDescription").Set(vSrc.FieldByName("MarkdownDescription"))
133127
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)
136130
}
137131
if fComputed := vDest.FieldByName("Computed"); fComputed.CanSet() {
138132
fComputed.SetBool(computed)
@@ -143,23 +137,35 @@ func convertElement(name string, element any, requiredFields []string) any {
143137
if fElementType := vDest.FieldByName("ElementType"); fElementType.CanSet() {
144138
fElementType.Set(vSrc.FieldByName("ElementType"))
145139
}
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+
150142
if fNested := vDest.FieldByName("NestedObject"); fNested.CanSet() {
151143
tNested := convertNestedMappings[fNested.Type().Name()]
152144
if tNested == nil {
153145
panic("nested type not support yet, add it to convertNestedMappings: " + fNested.Type().Name())
154146
}
155-
attrsSrc := vSrc.FieldByName("NestedObject").FieldByName("Attributes").Interface().(map[string]schema.Attribute)
156147
vNested := reflect.New(tNested).Elem()
157-
vNested.FieldByName("Attributes").Set(reflect.ValueOf(convertAttrs(attrsSrc, nil)))
148+
fillNestedAttrs(vNested, vSrc.FieldByName("NestedObject"))
158149
fNested.Set(vNested)
159150
}
160151
return vDest.Interface()
161152
}
162153

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+
163169
func overrideFields(attrs, overridenFields map[string]dsschema.Attribute) {
164170
for name, attr := range overridenFields {
165171
if attr == nil {

0 commit comments

Comments
 (0)