Skip to content
This repository was archived by the owner on Dec 30, 2024. It is now read-only.

Commit c9602ba

Browse files
authored
feat: add NestedObjectValueOf (#59)
1 parent 8309841 commit c9602ba

10 files changed

+860
-7
lines changed

.changelog/59.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```release-note:dependency
2+
deps: bumps github.com/FrangipaneTeam/terraform-plugin-framework-supertypes from v0.1.0 to v0.2.0
3+
```
4+
5+
```release-note:feature
6+
`Attributs` - Add `SuperListNestedAttributeOf`, `SuperSetNestedAttributeOf`, `SuperMapNestedAttributeOf` and `SuperSingleNestedAttributeOf` to create nested attributes with generic struct.
7+
```

.golangci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ linters-settings:
115115
extra-rules: true
116116

117117
revive:
118-
revive:
119118
ignore-generated-header: true
120119
severity: error
121120
rules:

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/FrangipaneTeam/terraform-plugin-framework-superschema
33
go 1.20
44

55
require (
6-
github.com/FrangipaneTeam/terraform-plugin-framework-supertypes v0.1.0
6+
github.com/FrangipaneTeam/terraform-plugin-framework-supertypes v0.2.0
77
github.com/hashicorp/terraform-plugin-framework v1.4.2
88
github.com/hashicorp/terraform-plugin-framework-timeouts v0.4.1
99
github.com/iancoleman/strcase v0.3.0

go.sum

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
github.com/FrangipaneTeam/terraform-plugin-framework-supertypes v0.1.0 h1:QHcnFyAQy/yWcBargdCt8Eprnh7G7vOYOw7z/4LOlM8=
2-
github.com/FrangipaneTeam/terraform-plugin-framework-supertypes v0.1.0/go.mod h1:GGgBv/UI8efmEok0VJWSxp3m6GdkETqzJf5SWH83aDs=
1+
github.com/FrangipaneTeam/terraform-plugin-framework-supertypes v0.2.0 h1:lcJY8AEbpbYp/M/jPUdYZArsZKAkM2LECSYDfKOiIiQ=
2+
github.com/FrangipaneTeam/terraform-plugin-framework-supertypes v0.2.0/go.mod h1:Aux7edspqsudNKr9YFgCkAgUwFj44RyOV123XolOzjY=
33
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
44
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
55
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -34,8 +34,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
3434
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
3535
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
3636
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
37-
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
3837
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
38+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
3939
github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9znI5mJU=
4040
github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
4141
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=

superlist_nested_attribute.go

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,147 @@ func (s SuperListNestedAttribute) GetDataSource(ctx context.Context) schemaD.Att
160160
a.MarkdownDescription = genDataSourceAttrDescription(ctx, a.MarkdownDescription, deprecationMessage, a.Validators)
161161
return a
162162
}
163+
164+
// * SuperTypeOf
165+
166+
var _ Attribute = SuperListNestedAttributeOf[struct{}]{}
167+
168+
type SuperListNestedAttributeOf[T any] struct {
169+
Deprecated *Deprecated
170+
Common *schemaR.ListNestedAttribute
171+
Resource *schemaR.ListNestedAttribute
172+
DataSource *schemaD.ListNestedAttribute
173+
Attributes Attributes
174+
}
175+
176+
// IsResource returns true if the attribute is a resource attribute.
177+
func (s SuperListNestedAttributeOf[T]) IsResource() bool {
178+
return s.Resource != nil || s.Common != nil
179+
}
180+
181+
// IsDataSource returns true if the attribute is a data source attribute.
182+
func (s SuperListNestedAttributeOf[T]) IsDataSource() bool {
183+
return s.DataSource != nil || s.Common != nil
184+
}
185+
186+
//nolint:dupl
187+
func (s SuperListNestedAttributeOf[T]) GetResource(ctx context.Context) schemaR.Attribute {
188+
var (
189+
common schemaR.ListNestedAttribute
190+
resource schemaR.ListNestedAttribute
191+
)
192+
193+
if s.Common != nil {
194+
common = *s.Common
195+
}
196+
197+
if s.Resource != nil {
198+
resource = *s.Resource
199+
}
200+
201+
a := schemaR.ListNestedAttribute{
202+
Required: computeIsRequired(common, resource),
203+
Optional: computeIsOptional(common, resource),
204+
Computed: computeIsComputed(common, resource),
205+
Sensitive: computeIsSensitive(common, resource),
206+
MarkdownDescription: computeMarkdownDescription(common, resource),
207+
Description: computeDescription(common, resource),
208+
DeprecationMessage: computeDeprecationMessage(common, resource),
209+
NestedObject: schemaR.NestedAttributeObject{
210+
Attributes: s.Attributes.process(ctx, resourceT).(map[string]schemaR.Attribute),
211+
},
212+
}
213+
214+
a.Validators = append(a.Validators, common.Validators...)
215+
a.Validators = append(a.Validators, resource.Validators...)
216+
a.PlanModifiers = append(a.PlanModifiers, common.PlanModifiers...)
217+
a.PlanModifiers = append(a.PlanModifiers, resource.PlanModifiers...)
218+
219+
defaultVDescription := ""
220+
221+
if s.Common != nil {
222+
if s.Common.CustomType != nil {
223+
a.CustomType = s.Common.CustomType
224+
}
225+
}
226+
227+
if s.Resource != nil {
228+
if s.Resource.Default != nil {
229+
a.Default = s.Resource.Default
230+
defaultVDescription = s.Resource.Default.MarkdownDescription(ctx)
231+
}
232+
if s.Resource.CustomType != nil {
233+
a.CustomType = s.Resource.CustomType
234+
}
235+
}
236+
// * If user has not provided a custom type, we will use the default supertypes
237+
if a.CustomType == nil {
238+
a.CustomType = supertypes.NewListNestedObjectTypeOf[T](ctx)
239+
}
240+
241+
deprecationMessage := ""
242+
if s.Deprecated != nil {
243+
a.DeprecationMessage = s.Deprecated.DeprecationMessage
244+
deprecationMessage = s.Deprecated.computeDeprecatedDocumentation()
245+
}
246+
247+
a.MarkdownDescription = genResourceAttrDescription(ctx, a.MarkdownDescription, defaultVDescription, deprecationMessage, a.Validators, a.PlanModifiers)
248+
return a
249+
}
250+
251+
//nolint:dupl
252+
func (s SuperListNestedAttributeOf[T]) GetDataSource(ctx context.Context) schemaD.Attribute {
253+
var (
254+
common schemaR.ListNestedAttribute
255+
dataSource schemaD.ListNestedAttribute
256+
)
257+
258+
if s.Common != nil {
259+
common = *s.Common
260+
}
261+
262+
if s.DataSource != nil {
263+
dataSource = *s.DataSource
264+
}
265+
266+
a := schemaD.ListNestedAttribute{
267+
Required: computeIsRequired(common, dataSource),
268+
Optional: computeIsOptional(common, dataSource),
269+
Computed: computeIsComputed(common, dataSource),
270+
Sensitive: computeIsSensitive(common, dataSource),
271+
MarkdownDescription: computeMarkdownDescription(common, dataSource),
272+
Description: computeDescription(common, dataSource),
273+
DeprecationMessage: computeDeprecationMessage(common, dataSource),
274+
NestedObject: schemaD.NestedAttributeObject{
275+
Attributes: s.Attributes.process(ctx, dataSourceT).(map[string]schemaD.Attribute),
276+
},
277+
}
278+
279+
a.Validators = append(a.Validators, common.Validators...)
280+
a.Validators = append(a.Validators, dataSource.Validators...)
281+
282+
if s.Common != nil {
283+
if s.Common.CustomType != nil {
284+
a.CustomType = s.Common.CustomType
285+
}
286+
}
287+
288+
if s.DataSource != nil {
289+
if s.DataSource.CustomType != nil {
290+
a.CustomType = s.DataSource.CustomType
291+
}
292+
}
293+
// * If user has not provided a custom type, we will use the default supertypes
294+
if a.CustomType == nil {
295+
a.CustomType = supertypes.NewListNestedObjectTypeOf[T](ctx)
296+
}
297+
298+
deprecationMessage := ""
299+
if s.Deprecated != nil {
300+
a.DeprecationMessage = s.Deprecated.DeprecationMessage
301+
deprecationMessage = s.Deprecated.computeDeprecatedDocumentation()
302+
}
303+
304+
a.MarkdownDescription = genDataSourceAttrDescription(ctx, a.MarkdownDescription, deprecationMessage, a.Validators)
305+
return a
306+
}

supermap_nested_attribute.go

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,147 @@ func (s SuperMapNestedAttribute) GetDataSource(ctx context.Context) schemaD.Attr
160160
a.MarkdownDescription = genDataSourceAttrDescription(ctx, a.MarkdownDescription, deprecationMessage, a.Validators)
161161
return a
162162
}
163+
164+
// * SuperTypeOf
165+
166+
var _ Attribute = SuperMapNestedAttributeOf[struct{}]{}
167+
168+
type SuperMapNestedAttributeOf[T any] struct {
169+
Deprecated *Deprecated
170+
Common *schemaR.MapNestedAttribute
171+
Resource *schemaR.MapNestedAttribute
172+
DataSource *schemaD.MapNestedAttribute
173+
Attributes Attributes
174+
}
175+
176+
// IsResource returns true if the attribute is a resource attribute.
177+
func (s SuperMapNestedAttributeOf[T]) IsResource() bool {
178+
return s.Resource != nil || s.Common != nil
179+
}
180+
181+
// IsDataSource returns true if the attribute is a data source attribute.
182+
func (s SuperMapNestedAttributeOf[T]) IsDataSource() bool {
183+
return s.DataSource != nil || s.Common != nil
184+
}
185+
186+
//nolint:dupl
187+
func (s SuperMapNestedAttributeOf[T]) GetResource(ctx context.Context) schemaR.Attribute {
188+
var (
189+
common schemaR.MapNestedAttribute
190+
resource schemaR.MapNestedAttribute
191+
)
192+
193+
if s.Common != nil {
194+
common = *s.Common
195+
}
196+
197+
if s.Resource != nil {
198+
resource = *s.Resource
199+
}
200+
201+
a := schemaR.MapNestedAttribute{
202+
Required: computeIsRequired(common, resource),
203+
Optional: computeIsOptional(common, resource),
204+
Computed: computeIsComputed(common, resource),
205+
Sensitive: computeIsSensitive(common, resource),
206+
MarkdownDescription: computeMarkdownDescription(common, resource),
207+
Description: computeDescription(common, resource),
208+
DeprecationMessage: computeDeprecationMessage(common, resource),
209+
NestedObject: schemaR.NestedAttributeObject{
210+
Attributes: s.Attributes.process(ctx, resourceT).(map[string]schemaR.Attribute),
211+
},
212+
}
213+
214+
a.Validators = append(a.Validators, common.Validators...)
215+
a.Validators = append(a.Validators, resource.Validators...)
216+
a.PlanModifiers = append(a.PlanModifiers, common.PlanModifiers...)
217+
a.PlanModifiers = append(a.PlanModifiers, resource.PlanModifiers...)
218+
219+
defaultVDescription := ""
220+
221+
if s.Common != nil {
222+
if s.Common.CustomType != nil {
223+
a.CustomType = s.Common.CustomType
224+
}
225+
}
226+
227+
if s.Resource != nil {
228+
if s.Resource.Default != nil {
229+
a.Default = s.Resource.Default
230+
defaultVDescription = s.Resource.Default.MarkdownDescription(ctx)
231+
}
232+
if s.Resource.CustomType != nil {
233+
a.CustomType = s.Resource.CustomType
234+
}
235+
}
236+
// * If user has not provided a custom type, we will use the default supertypes
237+
if a.CustomType == nil {
238+
a.CustomType = supertypes.NewMapNestedObjectTypeOf[T](ctx)
239+
}
240+
241+
deprecationMessage := ""
242+
if s.Deprecated != nil {
243+
a.DeprecationMessage = s.Deprecated.DeprecationMessage
244+
deprecationMessage = s.Deprecated.computeDeprecatedDocumentation()
245+
}
246+
247+
a.MarkdownDescription = genResourceAttrDescription(ctx, a.MarkdownDescription, defaultVDescription, deprecationMessage, a.Validators, a.PlanModifiers)
248+
return a
249+
}
250+
251+
//nolint:dupl
252+
func (s SuperMapNestedAttributeOf[T]) GetDataSource(ctx context.Context) schemaD.Attribute {
253+
var (
254+
common schemaR.MapNestedAttribute
255+
dataSource schemaD.MapNestedAttribute
256+
)
257+
258+
if s.Common != nil {
259+
common = *s.Common
260+
}
261+
262+
if s.DataSource != nil {
263+
dataSource = *s.DataSource
264+
}
265+
266+
a := schemaD.MapNestedAttribute{
267+
Required: computeIsRequired(common, dataSource),
268+
Optional: computeIsOptional(common, dataSource),
269+
Computed: computeIsComputed(common, dataSource),
270+
Sensitive: computeIsSensitive(common, dataSource),
271+
MarkdownDescription: computeMarkdownDescription(common, dataSource),
272+
Description: computeDescription(common, dataSource),
273+
DeprecationMessage: computeDeprecationMessage(common, dataSource),
274+
NestedObject: schemaD.NestedAttributeObject{
275+
Attributes: s.Attributes.process(ctx, dataSourceT).(map[string]schemaD.Attribute),
276+
},
277+
}
278+
279+
a.Validators = append(a.Validators, common.Validators...)
280+
a.Validators = append(a.Validators, dataSource.Validators...)
281+
282+
if s.Common != nil {
283+
if s.Common.CustomType != nil {
284+
a.CustomType = s.Common.CustomType
285+
}
286+
}
287+
288+
if s.DataSource != nil {
289+
if s.DataSource.CustomType != nil {
290+
a.CustomType = s.DataSource.CustomType
291+
}
292+
}
293+
// * If user has not provided a custom type, we will use the default supertypes
294+
if a.CustomType == nil {
295+
a.CustomType = supertypes.NewMapNestedObjectTypeOf[T](ctx)
296+
}
297+
298+
deprecationMessage := ""
299+
if s.Deprecated != nil {
300+
a.DeprecationMessage = s.Deprecated.DeprecationMessage
301+
deprecationMessage = s.Deprecated.computeDeprecatedDocumentation()
302+
}
303+
304+
a.MarkdownDescription = genDataSourceAttrDescription(ctx, a.MarkdownDescription, deprecationMessage, a.Validators)
305+
return a
306+
}

0 commit comments

Comments
 (0)