Skip to content

Commit c0143dd

Browse files
alessiodionisidetro
authored andcommitted
Group nested attributes by optional, required and computed value
1 parent 02e3188 commit c0143dd

1 file changed

Lines changed: 35 additions & 11 deletions

File tree

schemamd/render.go

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -475,31 +475,55 @@ func writeObjectChildren(w io.Writer, parents []string, ty cty.Type, group group
475475
}
476476

477477
func writeNestedAttributeChildren(w io.Writer, parents []string, nestedAttributes *tfjson.SchemaNestedAttributeType, group groupFilter) error {
478-
_, err := io.WriteString(w, group.nestedTitle+"\n\n")
479-
if err != nil {
480-
return err
481-
}
482-
483478
sortedNames := []string{}
484479
for n := range nestedAttributes.Attributes {
485480
sortedNames = append(sortedNames, n)
486481
}
487482
sort.Strings(sortedNames)
488-
nestedTypes := []nestedType{}
489483

484+
groups := map[int][]string{}
490485
for _, name := range sortedNames {
491486
att := nestedAttributes.Attributes[name]
492-
path := append(parents, name)
493487

494-
nt, err := writeAttribute(w, path, att, group)
488+
for i, gf := range groupFilters {
489+
if gf.filterAttribute(att) {
490+
groups[i] = append(groups[i], name)
491+
}
492+
}
493+
}
494+
495+
nestedTypes := []nestedType{}
496+
497+
for i, gf := range groupFilters {
498+
names, ok := groups[i]
499+
if !ok || len(names) == 0 {
500+
continue
501+
}
502+
503+
_, err := io.WriteString(w, gf.nestedTitle+"\n\n")
495504
if err != nil {
496-
return fmt.Errorf("unable to render attribute %q: %w", name, err)
505+
return err
497506
}
498507

499-
nestedTypes = append(nestedTypes, nt...)
508+
for _, name := range names {
509+
att := nestedAttributes.Attributes[name]
510+
path := append(parents, name)
511+
512+
nt, err := writeAttribute(w, path, att, group)
513+
if err != nil {
514+
return fmt.Errorf("unable to render attribute %q: %w", name, err)
515+
}
516+
517+
nestedTypes = append(nestedTypes, nt...)
518+
}
519+
520+
_, err = io.WriteString(w, "\n")
521+
if err != nil {
522+
return err
523+
}
500524
}
501525

502-
_, err = io.WriteString(w, "\n")
526+
_, err := io.WriteString(w, "\n")
503527
if err != nil {
504528
return err
505529
}

0 commit comments

Comments
 (0)