Repository navigation
Expand file tree
/
Copy pathdocs_rendering.go
More file actions
413 lines (335 loc) · 11.8 KB
/
Copy pathdocs_rendering.go
File metadata and controls
413 lines (335 loc) · 11.8 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package tfdocsextras
import (
"strings"
"github.com/terraform-docs/terraform-docs/terraform"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
type MetadataAttribute struct {
Name string `json:"name"`
Content string `json:"content"`
}
type ArgumentMetadata struct {
Attributes []MetadataAttribute `json:"attributes,omitempty"`
Enumerations []string `json:"enumerations,omitempty"`
Examples []MetadataAttribute `json:"examples,omitempty"`
Links []MetadataAttribute `json:"links,omitempty"`
RegexPattern string `json:"regex_pattern,omitempty"`
RegexExamples []string `json:"regex_examples,omitempty"`
}
type Argument struct {
ParentType ArgGroupType `json:"-"`
Type string `json:"type"`
ComplexType *string `json:"complex_type,omitempty"`
Name string `json:"name"`
DefaultValue *string `json:"default_value"`
Required bool `json:"required"`
Sensitive bool `json:"sensitive"`
Description string `json:"description"`
ArgumentMetadata
}
func (r *Argument) GetAnchor() string {
if r.ComplexType == nil {
return ""
}
return anchorFor(*r.ComplexType)
}
// objectPathSeparator joins an object type name to the one it is nested in.
// A dot cannot appear in an HCL identifier, so a path built out of dots is
// unambiguous, and it reads the way the attribute itself would be referenced.
const objectPathSeparator = "."
// outputObjectPrefix namespaces the object types reached from outputs. Input
// names are unique among inputs, and output names among outputs, but a module
// may well have an output named after the input it passes through - and both
// would otherwise claim the same object type name, with the second silently
// overwriting the first.
const outputObjectPrefix = "output" + objectPathSeparator
// joinObjectPath qualifies a nested object type name with the path of the one
// it is declared in, so that two objects with the same field name - say a
// `details` inside each of two variables - stay distinct.
func joinObjectPath(parent string, name string) string {
if parent == "" {
return name
}
return parent + objectPathSeparator + name
}
// requalifyObjectType rewrites the `object(name)` inside a type expression to
// carry the nested type's full path, keeping the rendered type and the anchor
// it links to in agreement.
func requalifyObjectType(dataType string, name string, path string) string {
return strings.Replace(dataType, "object("+name+")", "object("+path+")", 1)
}
// anchorFor derives the fragment identifier a markdown renderer gives to a
// heading: lowercased, spaces turned into dashes, and anything that is not a
// letter, digit, dash or underscore dropped. Object type names are paths now,
// and the dots in them do not survive into the anchor.
func anchorFor(name string) string {
var anchor strings.Builder
for _, char := range strings.ToLower(name) {
switch {
case char >= 'a' && char <= 'z', char >= '0' && char <= '9', char == '-', char == '_':
anchor.WriteRune(char)
case char == ' ':
anchor.WriteRune('-')
}
}
return anchor.String()
}
func (r *Argument) GetParentType() [2]string {
if r.ComplexType == nil {
return [2]string{"", ""}
}
values := strings.Split(r.Type, *r.ComplexType)
if len(values) == 2 {
return [2]string{values[0], values[1]}
}
return [2]string{"", ""}
}
func (r *Argument) GetMetadata() *ArgumentMetadata {
return &r.ArgumentMetadata
}
func (r *Argument) HasDefaultValue() bool {
if r.Sensitive || r.Required {
return false
}
return r.DefaultValue != nil && *r.DefaultValue != ""
}
func (r *Argument) HasNonEmptyObjectDefault() bool {
if r.ComplexType == nil || !r.HasDefaultValue() {
return false
}
if !strings.HasPrefix(strings.TrimSpace(r.Type), "object(") {
return false
}
defaultValue := strings.TrimSpace(*r.DefaultValue)
return defaultValue != "{}" && defaultValue != "null"
}
type ArgGroupType int
const (
Anonymous ArgGroupType = iota
RequiredInput
OptionalInput
Output
)
type ArgumentGroup struct {
Type ArgGroupType `json:"-"`
Required bool `json:"required"`
Description string `json:"description"`
Rows []Argument `json:"rows"`
ArgumentMetadata
}
func (d *ArgumentGroup) GetMetadata() *ArgumentMetadata {
return &d.ArgumentMetadata
}
type ModuleManifest struct {
RequiredInputs ArgumentGroup `json:"required_inputs"`
OptionalInputs ArgumentGroup `json:"optional_inputs"`
Outputs ArgumentGroup `json:"outputs"`
Objects map[string]ArgumentGroup `json:"objects"`
ReferenceLinks map[string]string `json:"reference_links"`
}
func newArgumentGroup(grpType ArgGroupType) ArgumentGroup {
return ArgumentGroup{
Type: grpType,
Description: "",
Required: false,
ArgumentMetadata: ArgumentMetadata{
Attributes: []MetadataAttribute{},
Enumerations: []string{},
Examples: []MetadataAttribute{},
Links: []MetadataAttribute{},
RegexPattern: "",
RegexExamples: []string{},
},
Rows: []Argument{},
}
}
func newArgument(parentType ArgGroupType, typeStr, name, description string) Argument {
return Argument{
ParentType: parentType,
Type: typeStr,
Name: name,
DefaultValue: nil,
Required: false,
Sensitive: false,
Description: description,
ArgumentMetadata: ArgumentMetadata{
Attributes: []MetadataAttribute{},
Enumerations: []string{},
Examples: []MetadataAttribute{},
Links: []MetadataAttribute{},
RegexPattern: "",
RegexExamples: []string{},
},
}
}
func newModuleManifest() *ModuleManifest {
return &ModuleManifest{
RequiredInputs: newArgumentGroup(RequiredInput),
OptionalInputs: newArgumentGroup(OptionalInput),
Objects: make(map[string]ArgumentGroup),
ReferenceLinks: make(map[string]string),
Outputs: newArgumentGroup(Output),
}
}
func processDirectives(directives []DocDirective, manifest *ModuleManifest, data *ArgumentGroup, row *Argument) {
var metadata *ArgumentMetadata
if data != nil {
metadata = data.GetMetadata()
} else if row != nil {
metadata = row.GetMetadata()
}
if metadata == nil {
return
}
for _, attr := range directives {
if (attr.Parsed.Flags&IsInvalid) != 0 || attr.Parsed.Type == DirType {
continue
}
switch attr.Parsed.Type {
case DirEnum:
metadata.Enumerations = append(metadata.Enumerations, attr.Parsed.Args...)
case DirExample:
metadata.Examples = append(metadata.Examples, MetadataAttribute{
Name: attr.Parsed.Args[0],
Content: getArgOrDefault(attr.Parsed.Args, 1),
})
case DirLink:
if (attr.Parsed.Flags & IsReferenceLink) != 0 {
manifest.ReferenceLinks[attr.Parsed.Args[0]] = attr.Parsed.Args[1]
} else if (attr.Parsed.Flags & IsNamedLink) != 0 {
metadata.Links = append(metadata.Links, MetadataAttribute{
Name: attr.Parsed.Args[0],
Content: getArgOrDefault(attr.Parsed.Args, 1),
})
}
case DirRegex:
if len(attr.Parsed.Args) >= 1 {
metadata.RegexPattern = attr.Parsed.Args[0]
metadata.RegexExamples = append(metadata.RegexExamples, attr.Parsed.Args[1:]...)
}
default:
caser := cases.Title(language.English)
metadata.Attributes = append(metadata.Attributes, MetadataAttribute{
Name: caser.String(attr.Name),
Content: attr.RawContent,
})
}
}
}
func getArgOrDefault(args []string, index int) string {
if len(args) > index {
return args[index]
}
return ""
}
// recordNested records the object type declared by group under 'path', its
// fully qualified name, and recurses into the object types declared inside it.
func recordNested(group StructProperty, path string, manifest *ModuleManifest) {
if group.NestedDataType == nil {
return
}
if len(group.Properties) > 0 {
data := newArgumentGroup(Anonymous)
data.Required = !group.Optional
data.Description = strings.Join(group.Documentation.Content, "\n")
processDirectives(group.Documentation.Directives, manifest, &data, nil)
for _, field := range group.Properties {
row := newArgument(data.Type, field.DataTypeStr, field.Name, strings.Join(field.Documentation.Content, "\n"))
if field.DefaultValue != nil {
row.DefaultValue = field.DefaultValue
}
if field.NestedDataType != nil {
nested := joinObjectPath(path, *field.NestedDataType)
row.ComplexType = &nested
row.Type = requalifyObjectType(field.DataTypeStr, *field.NestedDataType, nested)
}
row.Required = !field.Optional
processDirectives(field.Documentation.Directives, manifest, nil, &row)
data.Rows = append(data.Rows, row)
}
manifest.Objects[path] = data
}
for _, field := range group.Properties {
if field.NestedDataType == nil {
continue
}
recordNested(field, joinObjectPath(path, *field.NestedDataType), manifest)
}
}
func processStructAndNested(extras *DocumentedStruct, path string, manifest *ModuleManifest) {
if extras == nil {
return
}
recordNested(extras.StructProperty, path, manifest)
}
func createArgumentFromDocBlock(argType ArgGroupType, name string, typeStr string, docBlk PropertyDocBlock, extras *DocumentedStruct, path string, manifest *ModuleManifest) Argument {
tableRow := newArgument(argType, typeStr, name, strings.Join(docBlk.Content, "\n"))
processDirectives(docBlk.Directives, manifest, nil, &tableRow)
if extras != nil && extras.StructProperty.NestedDataType != nil {
tableRow.Type = requalifyObjectType(extras.StructProperty.DataTypeStr, *extras.StructProperty.NestedDataType, path)
tableRow.ComplexType = &path
}
return tableRow
}
func findTypeDirective(directives []DocDirective) *DocDirective {
for _, directive := range directives {
if directive.Name == "type" {
return &directive
}
}
return nil
}
func ParseModuleArgsIntoManifest(inputs []*terraform.Input, outputs []*terraform.Output) *ModuleManifest {
templateData := newModuleManifest()
for _, input := range inputs {
var extras *DocumentedStruct
if input.Type != "" {
if documented, err := ParseIntoDocumentedStruct(string(input.Type), input.Name); err == nil && documented != nil {
extras = documented
}
}
docBlk := parseStringIntoDocBlock(string(input.Description))
defaultValue := input.GetValue()
argType := OptionalInput
if input.Required {
argType = RequiredInput
}
// An input's object types are rooted at the input name, which is
// already unique among inputs.
path := input.Name
tableRow := createArgumentFromDocBlock(argType, input.Name, string(input.Type), docBlk, extras, path, templateData)
tableRow.DefaultValue = &defaultValue
tableRow.Required = input.Required
if tableRow.Required {
templateData.RequiredInputs.Rows = append(templateData.RequiredInputs.Rows, tableRow)
} else {
templateData.OptionalInputs.Rows = append(templateData.OptionalInputs.Rows, tableRow)
}
processStructAndNested(extras, path, templateData)
}
for _, output := range outputs {
outputType := "unknown"
if output.Description == "" {
tableRow := newArgument(Output, outputType, output.Name, "")
tableRow.Sensitive = output.Sensitive
templateData.Outputs.Rows = append(templateData.Outputs.Rows, tableRow)
continue
}
docBlk := parseStringIntoDocBlock(string(output.Description))
typeDef := findTypeDirective(docBlk.Directives)
var extras *DocumentedStruct
if typeDef != nil && len(typeDef.Parsed.Args) > 0 {
outputType = typeDef.Parsed.Args[0]
extras, _ = ParseIntoDocumentedStruct(outputType, output.Name)
}
// An output may share its name with the input it passes through, so
// its object types are namespaced to keep the two apart.
path := outputObjectPrefix + output.Name
tableRow := createArgumentFromDocBlock(Output, output.Name, outputType, docBlk, extras, path, templateData)
tableRow.Sensitive = output.Sensitive
templateData.Outputs.Rows = append(templateData.Outputs.Rows, tableRow)
processStructAndNested(extras, path, templateData)
}
return templateData
}