Skip to content

Commit 6a6634d

Browse files
authored
mmv1: add FieldType() helper for documentation generation (#15724)
1 parent 3a2816f commit 6a6634d

File tree

3 files changed

+175
-20
lines changed

3 files changed

+175
-20
lines changed

mmv1/api/type.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,31 @@ func (t *Type) GetDescription() string {
876876
return strings.TrimSpace(strings.TrimRight(t.Description, "\n"))
877877
}
878878

879+
func (t *Type) FieldType() []string {
880+
ret := []string{}
881+
if t.Required {
882+
ret = append(ret, "Required")
883+
} else if !t.Output {
884+
ret = append(ret, "Optional")
885+
} else if t.Output && t.ParentMetadata != nil {
886+
ret = append(ret, "Output")
887+
}
888+
889+
if t.WriteOnlyLegacy || t.WriteOnly {
890+
ret = append(ret, "Write-Only")
891+
}
892+
893+
if t.MinVersion == "beta" && t.ResourceMetadata.MinVersion != "beta" {
894+
ret = append(ret, "[Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html)")
895+
}
896+
897+
if t.DeprecationMessage != "" {
898+
ret = append(ret, "Deprecated")
899+
}
900+
901+
return ret
902+
}
903+
879904
// TODO rewrite: validation
880905
// class Array < Composite
881906
// check :item_type, type: [::String, NestedObject, ResourceRef, Enum], required: true

mmv1/api/type_test.go

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,154 @@ func TestTypeMinVersionObj(t *testing.T) {
8989
}
9090
}
9191

92+
func TestTypeFieldType(t *testing.T) {
93+
t.Parallel()
94+
95+
cases := []struct {
96+
description string
97+
obj Type
98+
expected []string
99+
}{
100+
{
101+
description: "Required field",
102+
obj: Type{
103+
Required: true,
104+
},
105+
expected: []string{"Required"},
106+
},
107+
{
108+
description: "Optional field",
109+
obj: Type{
110+
Required: false,
111+
Output: false,
112+
},
113+
expected: []string{"Optional"},
114+
},
115+
{
116+
description: "Output field with parent",
117+
obj: Type{
118+
Required: false,
119+
Output: true,
120+
ParentMetadata: &Type{},
121+
},
122+
expected: []string{"Output"},
123+
},
124+
{
125+
description: "Output field without parent",
126+
obj: Type{
127+
Required: false,
128+
Output: true,
129+
ParentMetadata: nil,
130+
},
131+
expected: []string{},
132+
},
133+
{
134+
description: "WriteOnlyLegacy field",
135+
obj: Type{
136+
WriteOnlyLegacy: true,
137+
},
138+
expected: []string{"Optional", "Write-Only"},
139+
},
140+
{
141+
description: "WriteOnly field",
142+
obj: Type{
143+
WriteOnly: true,
144+
},
145+
expected: []string{"Optional", "Write-Only"},
146+
},
147+
{
148+
description: "Beta field in GA resource",
149+
obj: Type{
150+
MinVersion: "beta",
151+
ResourceMetadata: &Resource{
152+
MinVersion: "ga",
153+
},
154+
},
155+
expected: []string{"Optional", "[Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html)"},
156+
},
157+
{
158+
description: "Beta field in Beta resource",
159+
obj: Type{
160+
MinVersion: "beta",
161+
ResourceMetadata: &Resource{
162+
MinVersion: "beta",
163+
},
164+
},
165+
expected: []string{"Optional"},
166+
},
167+
{
168+
description: "GA field in GA resource",
169+
obj: Type{
170+
MinVersion: "ga",
171+
ResourceMetadata: &Resource{
172+
MinVersion: "ga",
173+
},
174+
},
175+
expected: []string{"Optional"},
176+
},
177+
{
178+
description: "Deprecated field",
179+
obj: Type{
180+
DeprecationMessage: "This field is deprecated.",
181+
},
182+
expected: []string{"Optional", "Deprecated"},
183+
},
184+
{
185+
description: "All fields set for a required property",
186+
obj: Type{
187+
Required: true,
188+
WriteOnly: true,
189+
MinVersion: "beta",
190+
ResourceMetadata: &Resource{MinVersion: "ga"},
191+
DeprecationMessage: "This field is deprecated.",
192+
},
193+
expected: []string{"Required", "Write-Only", "[Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html)", "Deprecated"},
194+
},
195+
{
196+
description: "All fields set for an optional property",
197+
obj: Type{
198+
WriteOnly: true,
199+
MinVersion: "beta",
200+
ResourceMetadata: &Resource{MinVersion: "ga"},
201+
DeprecationMessage: "This field is deprecated.",
202+
},
203+
expected: []string{"Optional", "Write-Only", "[Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html)", "Deprecated"},
204+
},
205+
{
206+
description: "Output and deprecated",
207+
obj: Type{
208+
Output: true,
209+
ParentMetadata: &Type{},
210+
DeprecationMessage: "This field is deprecated.",
211+
},
212+
expected: []string{"Output", "Deprecated"},
213+
},
214+
{
215+
description: "Required and deprecated",
216+
obj: Type{
217+
Required: true,
218+
ParentMetadata: &Type{},
219+
DeprecationMessage: "This field is deprecated.",
220+
},
221+
expected: []string{"Required", "Deprecated"},
222+
},
223+
}
224+
225+
for _, tc := range cases {
226+
tc := tc
227+
228+
t.Run(tc.description, func(t *testing.T) {
229+
t.Parallel()
230+
231+
fieldType := tc.obj.FieldType()
232+
233+
if got, want := fieldType, tc.expected; !reflect.DeepEqual(got, want) {
234+
t.Errorf("expected %v to be %v", got, want)
235+
}
236+
})
237+
}
238+
}
239+
92240
func TestTypeExcludeIfNotInVersion(t *testing.T) {
93241
t.Parallel()
94242

mmv1/templates/terraform/property_documentation.html.markdown.tmpl

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,7 @@
11
{{ "" }}
22
* `{{ underscore $.Name }}` -
3-
{{- if and (eq $.MinVersion "beta") (not (eq $.ResourceMetadata.MinVersion "beta")) }}
4-
{{- if $.Required }}
5-
(Required, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html){{ if $.DeprecationMessage }}, Deprecated{{ end }})
6-
{{- else if not $.Output }}
7-
(Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html){{ if $.DeprecationMessage }}, Deprecated{{ end }})
8-
{{- else if and $.Output $.ParentMetadata }}
9-
(Output, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html){{ if $.DeprecationMessage }}, Deprecated{{ end }})
10-
{{- else }}
11-
([Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html){{ if $.DeprecationMessage }}, Deprecated{{ end }})
12-
{{- end}}
13-
{{- else }}
14-
{{- if $.Required }}
15-
(Required{{ if $.DeprecationMessage }}, Deprecated{{ end }})
16-
{{- else if not $.Output }}
17-
(Optional{{ if $.DeprecationMessage }}, Deprecated{{ end }})
18-
{{- else if and $.Output $.ParentMetadata }}
19-
(Output{{ if $.DeprecationMessage }}, Deprecated{{ end }})
20-
{{- else if $.DeprecationMessage }}
21-
(Deprecated)
22-
{{- end}}
3+
{{- if not (eq (len $.FieldType) 0) }}
4+
({{- join $.FieldType ", " -}})
235
{{- end }}
246
{{- $.ResourceMetadata.FormatDocDescription $.GetDescription true -}}
257
{{- if and (and ($.IsA "Array") ($.ItemType.IsA "Enum")) (and (not $.Output) (not $.ItemType.ExcludeDocsValues))}}

0 commit comments

Comments
 (0)