Skip to content

Commit 7d54b35

Browse files
committed
Factor out sorting value rows and add dedicated default section
1 parent 65f2b92 commit 7d54b35

File tree

1 file changed

+30
-50
lines changed

1 file changed

+30
-50
lines changed

pkg/document/model.go

Lines changed: 30 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,21 @@ type chartTemplateData struct {
3131
helm.ChartDocumentationInfo
3232
HelmDocsVersion string
3333
Values []valueRow
34-
Sections []section
34+
Sections sections
3535
Files files
3636
}
3737

38+
type sections struct {
39+
DefaultSection section
40+
Sections []section
41+
}
42+
3843
type section struct {
3944
SectionName string
4045
SectionItems []valueRow
4146
}
4247

43-
func sortValueRows(valueRows []valueRow) {
44-
sortOrder := viper.GetString("sort-values-order")
45-
46-
if sortOrder != FileSortOrder && sortOrder != AlphaNumSortOrder {
47-
log.Warnf("Invalid sort order provided %s, defaulting to %s", sortOrder, AlphaNumSortOrder)
48-
sortOrder = AlphaNumSortOrder
49-
}
50-
48+
func sortValueRowsByOrder(valueRows []valueRow, sortOrder string) {
5149
sort.Slice(valueRows, func(i, j int) bool {
5250
// Globals sort above non-globals.
5351
if valueRows[i].IsGlobal != valueRows[j].IsGlobal {
@@ -82,47 +80,29 @@ func sortValueRows(valueRows []valueRow) {
8280
})
8381
}
8482

85-
func sortSectionedValueRows(sectionedValueRows []section) {
83+
func sortValueRows(valueRows []valueRow) {
8684
sortOrder := viper.GetString("sort-values-order")
8785

8886
if sortOrder != FileSortOrder && sortOrder != AlphaNumSortOrder {
8987
log.Warnf("Invalid sort order provided %s, defaulting to %s", sortOrder, AlphaNumSortOrder)
9088
sortOrder = AlphaNumSortOrder
9189
}
9290

93-
for _, section := range sectionedValueRows {
94-
sort.Slice(section.SectionItems, func(i, j int) bool {
95-
// Globals sort above non-globals.
96-
if section.SectionItems[i].IsGlobal != section.SectionItems[j].IsGlobal {
97-
return section.SectionItems[i].IsGlobal
98-
}
91+
sortValueRowsByOrder(valueRows, sortOrder)
92+
}
9993

100-
// Group by dependency for non-globals.
101-
if !section.SectionItems[i].IsGlobal && !section.SectionItems[j].IsGlobal {
102-
// Values for the main chart sort above values for dependencies.
103-
if (section.SectionItems[i].Dependency == "") != (section.SectionItems[j].Dependency == "") {
104-
return section.SectionItems[i].Dependency == ""
105-
}
94+
func sortSectionedValueRows(sectionedValueRows sections) {
95+
sortOrder := viper.GetString("sort-values-order")
10696

107-
// Group dependency values together.
108-
if section.SectionItems[i].Dependency != section.SectionItems[j].Dependency {
109-
return section.SectionItems[i].Dependency < section.SectionItems[j].Dependency
110-
}
111-
}
97+
if sortOrder != FileSortOrder && sortOrder != AlphaNumSortOrder {
98+
log.Warnf("Invalid sort order provided %s, defaulting to %s", sortOrder, AlphaNumSortOrder)
99+
sortOrder = AlphaNumSortOrder
100+
}
112101

113-
// Sort the remaining values within the same section.SectionItems using the configured sort order.
114-
switch sortOrder {
115-
case FileSortOrder:
116-
if section.SectionItems[i].LineNumber == section.SectionItems[j].LineNumber {
117-
return section.SectionItems[i].Column < section.SectionItems[j].Column
118-
}
119-
return section.SectionItems[i].LineNumber < section.SectionItems[j].LineNumber
120-
case AlphaNumSortOrder:
121-
return section.SectionItems[i].Key < section.SectionItems[j].Key
122-
default:
123-
panic("cannot get here")
124-
}
125-
})
102+
sortValueRowsByOrder(sectionedValueRows.DefaultSection.SectionItems, sortOrder)
103+
104+
for _, section := range sectionedValueRows.Sections {
105+
sortValueRowsByOrder(section.SectionItems, sortOrder)
126106
}
127107
}
128108

@@ -143,30 +123,30 @@ func getUnsortedValueRows(document *yaml.Node, descriptions map[string]helm.Char
143123
return createValueRowsFromField("", nil, document.Content[0], descriptions, true)
144124
}
145125

146-
func getUnsortedSectionedValueRows(valueRows []valueRow) []section {
147-
var valueRowsSectionSorted []section
148-
valueRowsSectionSorted = append(valueRowsSectionSorted, section{
149-
SectionName: "General",
126+
func getSectionedValueRows(valueRows []valueRow) sections {
127+
var valueRowsSectionSorted sections
128+
valueRowsSectionSorted.DefaultSection = section{
129+
SectionName: "Other Values",
150130
SectionItems: []valueRow{},
151-
})
131+
}
152132

153133
for _, row := range valueRows {
154134
if row.Section == "" {
155-
valueRowsSectionSorted[0].SectionItems = append(valueRowsSectionSorted[0].SectionItems, row)
135+
valueRowsSectionSorted.DefaultSection.SectionItems = append(valueRowsSectionSorted.DefaultSection.SectionItems, row)
156136
continue
157137
}
158138

159139
containsSection := false
160-
for i, section := range valueRowsSectionSorted {
140+
for i, section := range valueRowsSectionSorted.Sections {
161141
if section.SectionName == row.Section {
162142
containsSection = true
163-
valueRowsSectionSorted[i].SectionItems = append(valueRowsSectionSorted[i].SectionItems, row)
143+
valueRowsSectionSorted.Sections[i].SectionItems = append(valueRowsSectionSorted.Sections[i].SectionItems, row)
164144
break
165145
}
166146
}
167147

168148
if !containsSection {
169-
valueRowsSectionSorted = append(valueRowsSectionSorted, section{
149+
valueRowsSectionSorted.Sections = append(valueRowsSectionSorted.Sections, section{
170150
SectionName: row.Section,
171151
SectionItems: []valueRow{row},
172152
})
@@ -219,7 +199,7 @@ func getChartTemplateData(info helm.ChartDocumentationInfo, helmDocsVersion stri
219199
}
220200

221201
sortValueRows(valuesTableRows)
222-
valueRowsSectionSorted := getUnsortedSectionedValueRows(valuesTableRows)
202+
valueRowsSectionSorted := getSectionedValueRows(valuesTableRows)
223203
sortSectionedValueRows(valueRowsSectionSorted)
224204

225205
files, err := getFiles(info.ChartDirectory)

0 commit comments

Comments
 (0)