Skip to content

Commit 65f2b92

Browse files
author
Constantin
committed
Put the creation and sorting of sectioned Value Rows in its own functions
This looks better, because we shouldn't create a table in a sorting function
1 parent 604ab7f commit 65f2b92

File tree

2 files changed

+50
-36
lines changed

2 files changed

+50
-36
lines changed

pkg/document/model.go

+48-34
Original file line numberDiff line numberDiff line change
@@ -40,43 +40,14 @@ type section struct {
4040
SectionItems []valueRow
4141
}
4242

43-
func sortValueRows(valueRows []valueRow) ([]valueRow, []section) {
43+
func sortValueRows(valueRows []valueRow) {
4444
sortOrder := viper.GetString("sort-values-order")
4545

4646
if sortOrder != FileSortOrder && sortOrder != AlphaNumSortOrder {
4747
log.Warnf("Invalid sort order provided %s, defaulting to %s", sortOrder, AlphaNumSortOrder)
4848
sortOrder = AlphaNumSortOrder
4949
}
5050

51-
var valueRowsSectionSorted []section
52-
valueRowsSectionSorted = append(valueRowsSectionSorted, section{
53-
SectionName: "General",
54-
SectionItems: []valueRow{},
55-
})
56-
57-
for _, row := range valueRows {
58-
if row.Section == "" {
59-
valueRowsSectionSorted[0].SectionItems = append(valueRowsSectionSorted[0].SectionItems, row)
60-
continue
61-
}
62-
63-
containsSection := false
64-
for i, section := range valueRowsSectionSorted {
65-
if section.SectionName == row.Section {
66-
containsSection = true
67-
valueRowsSectionSorted[i].SectionItems = append(valueRowsSectionSorted[i].SectionItems, row)
68-
break
69-
}
70-
}
71-
72-
if !containsSection {
73-
valueRowsSectionSorted = append(valueRowsSectionSorted, section{
74-
SectionName: row.Section,
75-
SectionItems: []valueRow{row},
76-
})
77-
}
78-
}
79-
8051
sort.Slice(valueRows, func(i, j int) bool {
8152
// Globals sort above non-globals.
8253
if valueRows[i].IsGlobal != valueRows[j].IsGlobal {
@@ -109,8 +80,17 @@ func sortValueRows(valueRows []valueRow) ([]valueRow, []section) {
10980
panic("cannot get here")
11081
}
11182
})
83+
}
84+
85+
func sortSectionedValueRows(sectionedValueRows []section) {
86+
sortOrder := viper.GetString("sort-values-order")
87+
88+
if sortOrder != FileSortOrder && sortOrder != AlphaNumSortOrder {
89+
log.Warnf("Invalid sort order provided %s, defaulting to %s", sortOrder, AlphaNumSortOrder)
90+
sortOrder = AlphaNumSortOrder
91+
}
11292

113-
for _, section := range valueRowsSectionSorted {
93+
for _, section := range sectionedValueRows {
11494
sort.Slice(section.SectionItems, func(i, j int) bool {
11595
// Globals sort above non-globals.
11696
if section.SectionItems[i].IsGlobal != section.SectionItems[j].IsGlobal {
@@ -144,7 +124,6 @@ func sortValueRows(valueRows []valueRow) ([]valueRow, []section) {
144124
}
145125
})
146126
}
147-
return valueRows, valueRowsSectionSorted
148127
}
149128

150129
func getUnsortedValueRows(document *yaml.Node, descriptions map[string]helm.ChartValueDescription) ([]valueRow, error) {
@@ -164,6 +143,39 @@ func getUnsortedValueRows(document *yaml.Node, descriptions map[string]helm.Char
164143
return createValueRowsFromField("", nil, document.Content[0], descriptions, true)
165144
}
166145

146+
func getUnsortedSectionedValueRows(valueRows []valueRow) []section {
147+
var valueRowsSectionSorted []section
148+
valueRowsSectionSorted = append(valueRowsSectionSorted, section{
149+
SectionName: "General",
150+
SectionItems: []valueRow{},
151+
})
152+
153+
for _, row := range valueRows {
154+
if row.Section == "" {
155+
valueRowsSectionSorted[0].SectionItems = append(valueRowsSectionSorted[0].SectionItems, row)
156+
continue
157+
}
158+
159+
containsSection := false
160+
for i, section := range valueRowsSectionSorted {
161+
if section.SectionName == row.Section {
162+
containsSection = true
163+
valueRowsSectionSorted[i].SectionItems = append(valueRowsSectionSorted[i].SectionItems, row)
164+
break
165+
}
166+
}
167+
168+
if !containsSection {
169+
valueRowsSectionSorted = append(valueRowsSectionSorted, section{
170+
SectionName: row.Section,
171+
SectionItems: []valueRow{row},
172+
})
173+
}
174+
}
175+
176+
return valueRowsSectionSorted
177+
}
178+
167179
func getChartTemplateData(info helm.ChartDocumentationInfo, helmDocsVersion string, dependencyValues []DependencyValues) (chartTemplateData, error) {
168180
valuesTableRows, err := getUnsortedValueRows(info.ChartValues, info.ChartValuesDescriptions)
169181
if err != nil {
@@ -206,7 +218,9 @@ func getChartTemplateData(info helm.ChartDocumentationInfo, helmDocsVersion stri
206218
}
207219
}
208220

209-
valueRowsSorted, valueRowsSectionSorted := sortValueRows(valuesTableRows)
221+
sortValueRows(valuesTableRows)
222+
valueRowsSectionSorted := getUnsortedSectionedValueRows(valuesTableRows)
223+
sortSectionedValueRows(valueRowsSectionSorted)
210224

211225
files, err := getFiles(info.ChartDirectory)
212226
if err != nil {
@@ -216,7 +230,7 @@ func getChartTemplateData(info helm.ChartDocumentationInfo, helmDocsVersion stri
216230
return chartTemplateData{
217231
ChartDocumentationInfo: info,
218232
HelmDocsVersion: helmDocsVersion,
219-
Values: valueRowsSorted,
233+
Values: valuesTableRows,
220234
Sections: valueRowsSectionSorted,
221235
Files: files,
222236
}, nil

pkg/document/values_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ func getSortedValuesTableRows(helmValues *yaml.Node, descriptions map[string]hel
1919
return nil, err
2020
}
2121

22-
valueRowsSorted, _ := sortValueRows(valueRows)
22+
sortValueRows(valueRows)
2323

24-
return valueRowsSorted, nil
24+
return valueRows, nil
2525
}
2626

2727
func parseYamlValues(yamlValues string) *yaml.Node {

0 commit comments

Comments
 (0)