-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Improves schema Description / MarkdownDescription (#2605)
* add conversion calls * UpdateSchemaDescription and UpdateDSSchemaDescription * only update empty Description * fix Description * set MarkdownDescripton if empty * remove redundant Description * project_ip_access_list * case SingleNestedBlock * validate blocks * reduce error message len * use reflection * don't allow both descriptions * unify description checks * simplify attr names * refactor tests * only 1 func UpdateSchemaDescription * unit testsg
- Loading branch information
Showing
36 changed files
with
332 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package conversion | ||
|
||
import ( | ||
"reflect" | ||
|
||
dsschema "github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema" | ||
) | ||
|
||
func UpdateSchemaDescription[T schema.Schema | dsschema.Schema](s *T) { | ||
UpdateAttr(s) | ||
} | ||
|
||
// UpdateAttr is exported for testing purposes only and should not be used directly. | ||
func UpdateAttr(attr any) { | ||
ptr := reflect.ValueOf(attr) | ||
if ptr.Kind() != reflect.Ptr { | ||
panic("not ptr, please fix caller") | ||
} | ||
v := ptr.Elem() | ||
if v.Kind() != reflect.Struct { | ||
panic("not struct, please fix caller") | ||
} | ||
updateDesc(v) | ||
updateMap(v, "Attributes") | ||
updateMap(v, "Blocks") | ||
updateNested(v, "NestedObject") | ||
} | ||
|
||
func updateDesc(v reflect.Value) { | ||
fDescr, fMDDescr := v.FieldByName("Description"), v.FieldByName("MarkdownDescription") | ||
if !fDescr.IsValid() || !fMDDescr.IsValid() { | ||
return | ||
} | ||
if !fDescr.CanSet() || fDescr.Kind() != reflect.String || | ||
!fMDDescr.CanSet() || fMDDescr.Kind() != reflect.String { | ||
panic("invalid desc fields, please fix caller") | ||
} | ||
strDescr, strMDDescr := fDescr.String(), fMDDescr.String() | ||
if strDescr != "" && strMDDescr != "" { | ||
panic("both descriptions exist, please fix caller: " + strDescr) | ||
} | ||
if strDescr == "" { | ||
fDescr.SetString(fMDDescr.String()) | ||
} else { | ||
fMDDescr.SetString(fDescr.String()) | ||
} | ||
} | ||
|
||
func updateMap(v reflect.Value, mapName string) { | ||
f := v.FieldByName(mapName) | ||
if !f.IsValid() { | ||
return | ||
} | ||
if f.Kind() != reflect.Map { | ||
panic("not map, please fix caller: " + mapName) | ||
} | ||
for _, k := range f.MapKeys() { | ||
v := f.MapIndex(k).Elem() | ||
newPtr := reflect.New(v.Type()) | ||
newPtr.Elem().Set(v) | ||
UpdateAttr(newPtr.Interface()) | ||
f.SetMapIndex(k, newPtr.Elem()) | ||
} | ||
} | ||
|
||
func updateNested(v reflect.Value, nestedName string) { | ||
f := v.FieldByName(nestedName) | ||
if !f.IsValid() { | ||
return | ||
} | ||
ptr := f.Addr() | ||
UpdateAttr(ptr.Interface()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
package conversion_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/resource/schema" | ||
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestUpdateSchemaDescription(t *testing.T) { | ||
s := schema.Schema{ | ||
Attributes: map[string]schema.Attribute{ | ||
"id": schema.StringAttribute{ | ||
Computed: true, | ||
}, | ||
"project_id": schema.StringAttribute{ | ||
Required: true, | ||
MarkdownDescription: "mddesc project_id", | ||
}, | ||
"nested": schema.ListNestedAttribute{ | ||
Computed: true, | ||
MarkdownDescription: "mddesc nested", | ||
NestedObject: schema.NestedAttributeObject{ | ||
Attributes: map[string]schema.Attribute{ | ||
"attr1": schema.StringAttribute{ | ||
Description: "desc attr1", | ||
Computed: true, | ||
}, | ||
"attr2": schema.StringAttribute{ | ||
MarkdownDescription: "mddesc attr2", | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
Blocks: map[string]schema.Block{ | ||
"list": schema.ListNestedBlock{ | ||
MarkdownDescription: "mddesc list", | ||
NestedObject: schema.NestedBlockObject{ | ||
Attributes: map[string]schema.Attribute{ | ||
"attr3": schema.BoolAttribute{ | ||
Optional: true, | ||
Computed: true, | ||
MarkdownDescription: "mddesc attr3", | ||
}, | ||
}, | ||
}, | ||
}, | ||
"set": schema.SetNestedBlock{ | ||
MarkdownDescription: "mddesc set", | ||
NestedObject: schema.NestedBlockObject{ | ||
Attributes: map[string]schema.Attribute{ | ||
"attr4": schema.StringAttribute{ | ||
Optional: true, | ||
MarkdownDescription: "mddesc attr4", | ||
}, | ||
"attr5": schema.Int64Attribute{ | ||
Required: true, | ||
MarkdownDescription: "mddesc attr5", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
expected := schema.Schema{ | ||
Attributes: map[string]schema.Attribute{ | ||
"id": schema.StringAttribute{ | ||
Computed: true, | ||
}, | ||
"project_id": schema.StringAttribute{ | ||
Required: true, | ||
Description: "mddesc project_id", | ||
MarkdownDescription: "mddesc project_id", | ||
}, | ||
"nested": schema.ListNestedAttribute{ | ||
Computed: true, | ||
Description: "mddesc nested", | ||
MarkdownDescription: "mddesc nested", | ||
NestedObject: schema.NestedAttributeObject{ | ||
Attributes: map[string]schema.Attribute{ | ||
"attr1": schema.StringAttribute{ | ||
Description: "desc attr1", | ||
MarkdownDescription: "desc attr1", | ||
Computed: true, | ||
}, | ||
"attr2": schema.StringAttribute{ | ||
Description: "mddesc attr2", | ||
MarkdownDescription: "mddesc attr2", | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
Blocks: map[string]schema.Block{ | ||
"list": schema.ListNestedBlock{ | ||
Description: "mddesc list", | ||
MarkdownDescription: "mddesc list", | ||
NestedObject: schema.NestedBlockObject{ | ||
Attributes: map[string]schema.Attribute{ | ||
"attr3": schema.BoolAttribute{ | ||
Optional: true, | ||
Computed: true, | ||
Description: "mddesc attr3", | ||
MarkdownDescription: "mddesc attr3", | ||
}, | ||
}, | ||
}, | ||
}, | ||
"set": schema.SetNestedBlock{ | ||
Description: "mddesc set", | ||
MarkdownDescription: "mddesc set", | ||
NestedObject: schema.NestedBlockObject{ | ||
Attributes: map[string]schema.Attribute{ | ||
"attr4": schema.StringAttribute{ | ||
Optional: true, | ||
Description: "mddesc attr4", | ||
MarkdownDescription: "mddesc attr4", | ||
}, | ||
"attr5": schema.Int64Attribute{ | ||
Required: true, | ||
Description: "mddesc attr5", | ||
MarkdownDescription: "mddesc attr5", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
conversion.UpdateSchemaDescription(&s) | ||
assert.Equal(t, expected, s) | ||
} | ||
|
||
func TestUpdateAttrPanic(t *testing.T) { | ||
testCases := map[string]any{ | ||
"not ptr, please fix caller": "no ptr", | ||
"not struct, please fix caller": conversion.Pointer("no struct"), | ||
"invalid desc fields, please fix caller": conversion.Pointer(struct { | ||
Description int | ||
MarkdownDescription int | ||
}{}), | ||
"both descriptions exist, please fix caller: description": conversion.Pointer(struct { | ||
Description string | ||
MarkdownDescription string | ||
}{ | ||
Description: "description", | ||
MarkdownDescription: "markdown description", | ||
}), | ||
"not map, please fix caller: Attributes": conversion.Pointer(struct { | ||
Attributes string | ||
}{}), | ||
} | ||
|
||
for name, tc := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
assert.PanicsWithValue(t, name, func() { | ||
conversion.UpdateAttr(tc) | ||
}) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.