Skip to content

Commit

Permalink
Group nested attributes by optional, required and computed value
Browse files Browse the repository at this point in the history
  • Loading branch information
alessiodionisi authored and detro committed Jul 5, 2022
1 parent 02e3188 commit c0143dd
Showing 1 changed file with 35 additions and 11 deletions.
46 changes: 35 additions & 11 deletions schemamd/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,31 +475,55 @@ func writeObjectChildren(w io.Writer, parents []string, ty cty.Type, group group
}

func writeNestedAttributeChildren(w io.Writer, parents []string, nestedAttributes *tfjson.SchemaNestedAttributeType, group groupFilter) error {
_, err := io.WriteString(w, group.nestedTitle+"\n\n")
if err != nil {
return err
}

sortedNames := []string{}
for n := range nestedAttributes.Attributes {
sortedNames = append(sortedNames, n)
}
sort.Strings(sortedNames)
nestedTypes := []nestedType{}

groups := map[int][]string{}
for _, name := range sortedNames {
att := nestedAttributes.Attributes[name]
path := append(parents, name)

nt, err := writeAttribute(w, path, att, group)
for i, gf := range groupFilters {
if gf.filterAttribute(att) {
groups[i] = append(groups[i], name)
}
}
}

nestedTypes := []nestedType{}

for i, gf := range groupFilters {
names, ok := groups[i]
if !ok || len(names) == 0 {
continue
}

_, err := io.WriteString(w, gf.nestedTitle+"\n\n")
if err != nil {
return fmt.Errorf("unable to render attribute %q: %w", name, err)
return err
}

nestedTypes = append(nestedTypes, nt...)
for _, name := range names {
att := nestedAttributes.Attributes[name]
path := append(parents, name)

nt, err := writeAttribute(w, path, att, group)
if err != nil {
return fmt.Errorf("unable to render attribute %q: %w", name, err)
}

nestedTypes = append(nestedTypes, nt...)
}

_, err = io.WriteString(w, "\n")
if err != nil {
return err
}
}

_, err = io.WriteString(w, "\n")
_, err := io.WriteString(w, "\n")
if err != nil {
return err
}
Expand Down

0 comments on commit c0143dd

Please sign in to comment.