-
Notifications
You must be signed in to change notification settings - Fork 1
/
type_attribute.go.tmpl
156 lines (132 loc) · 5.23 KB
/
type_attribute.go.tmpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// code generated by go generate - look at type_attribute.go.tmpl for source file
package superschema
import (
"context"
schemaD "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
schemaR "github.com/hashicorp/terraform-plugin-framework/resource/schema"
)
var _ Attribute = {{ .TypeName }}Attribute{}
type {{ .TypeName }}Attribute struct {
Deprecated *Deprecated
Common *schemaR.{{ .TypeName }}Attribute
Resource *schemaR.{{ .TypeName }}Attribute
DataSource *schemaD.{{ .TypeName }}Attribute
{{- if or (eq .TypeName "ListNested") (eq .TypeName "SetNested") (eq .TypeName "SingleNested") (eq .TypeName "MapNested") }}
Attributes Attributes
{{- end }}
}
// IsResource returns true if the attribute is a resource attribute.
func (s {{ .TypeName }}Attribute) IsResource() bool {
return s.Resource != nil || s.Common != nil
}
// IsDataSource returns true if the attribute is a data source attribute.
func (s {{ .TypeName }}Attribute) IsDataSource() bool {
return s.DataSource != nil || s.Common != nil
}
//nolint:dupl
func (s {{ .TypeName }}Attribute) GetResource(ctx context.Context) schemaR.Attribute {
var (
common schemaR.{{ .TypeName }}Attribute
resource schemaR.{{ .TypeName }}Attribute
)
if s.Common != nil {
common = *s.Common
}
if s.Resource != nil {
resource = *s.Resource
}
a := schemaR.{{ .TypeName }}Attribute{
Required: computeIsRequired(common, resource),
Optional: computeIsOptional(common, resource),
Computed: computeIsComputed(common, resource),
Sensitive: computeIsSensitive(common, resource),
MarkdownDescription: computeMarkdownDescription(common, resource),
Description: computeDescription(common, resource),
DeprecationMessage: computeDeprecationMessage(common, resource),
{{- if or (eq .TypeName "List") (eq .TypeName "Set") (eq .TypeName "Map") }}
ElementType: common.ElementType,
{{- end }}
{{- if or (eq .TypeName "ListNested") (eq .TypeName "SetNested") (eq .TypeName "MapNested") }}
NestedObject: schemaR.NestedAttributeObject{
Attributes: s.Attributes.process(ctx, resourceT).(map[string]schemaR.Attribute),
},
{{- end }}
{{- if (eq .TypeName "SingleNested") }}
Attributes: s.Attributes.process(ctx, resourceT).(map[string]schemaR.Attribute),
{{- end }}
}
{{- if or (eq .TypeName "List") (eq .TypeName "Set") (eq .TypeName "Map") }}
if s.Resource != nil {
if s.Resource.ElementType != nil {
a.ElementType = s.Resource.ElementType
}
}
{{- end }}
a.Validators = append(a.Validators, common.Validators...)
a.Validators = append(a.Validators, resource.Validators...)
a.PlanModifiers = append(a.PlanModifiers, common.PlanModifiers...)
a.PlanModifiers = append(a.PlanModifiers, resource.PlanModifiers...)
defaultVDescription := ""
if s.Resource != nil {
if s.Resource.Default != nil {
a.Default = s.Resource.Default
defaultVDescription = s.Resource.Default.MarkdownDescription(ctx)
}
}
deprecationMessage := ""
if s.Deprecated != nil {
a.DeprecationMessage = s.Deprecated.DeprecationMessage
deprecationMessage = s.Deprecated.computeDeprecatedDocumentation()
}
a.MarkdownDescription = genResourceAttrDescription(ctx, a.MarkdownDescription, defaultVDescription, deprecationMessage, a.Validators, a.PlanModifiers)
return a
}
//nolint:dupl
func (s {{ .TypeName }}Attribute) GetDataSource(ctx context.Context) schemaD.Attribute {
var (
common schemaR.{{ .TypeName }}Attribute
dataSource schemaD.{{ .TypeName }}Attribute
)
if s.Common != nil {
common = *s.Common
}
if s.DataSource != nil {
dataSource = *s.DataSource
}
a := schemaD.{{ .TypeName }}Attribute{
Required: computeIsRequired(common, dataSource),
Optional: computeIsOptional(common, dataSource),
Computed: computeIsComputed(common, dataSource),
Sensitive: computeIsSensitive(common, dataSource),
MarkdownDescription: computeMarkdownDescription(common, dataSource),
Description: computeDescription(common, dataSource),
DeprecationMessage: computeDeprecationMessage(common, dataSource),
{{- if or (eq .TypeName "List") (eq .TypeName "Set") (eq .TypeName "Map") }}
ElementType: common.ElementType,
{{- end }}
{{- if or (eq .TypeName "ListNested") (eq .TypeName "SetNested") (eq .TypeName "MapNested") }}
NestedObject: schemaD.NestedAttributeObject{
Attributes: s.Attributes.process(ctx, dataSourceT).(map[string]schemaD.Attribute),
},
{{- end }}
{{- if (eq .TypeName "SingleNested") }}
Attributes: s.Attributes.process(ctx, dataSourceT).(map[string]schemaD.Attribute),
{{- end }}
}
{{- if or (eq .TypeName "List") (eq .TypeName "Set") (eq .TypeName "Map") }}
if s.DataSource != nil {
if s.DataSource.ElementType != nil {
a.ElementType = s.DataSource.ElementType
}
}
{{- end }}
a.Validators = append(a.Validators, common.Validators...)
a.Validators = append(a.Validators, dataSource.Validators...)
deprecationMessage := ""
if s.Deprecated != nil {
a.DeprecationMessage = s.Deprecated.DeprecationMessage
deprecationMessage = s.Deprecated.computeDeprecatedDocumentation()
}
a.MarkdownDescription = genDataSourceAttrDescription(ctx, a.MarkdownDescription, deprecationMessage, a.Validators)
return a
}