Skip to content

Commit

Permalink
Factor out sorting value rows and add dedicated default section
Browse files Browse the repository at this point in the history
  • Loading branch information
Haepaxlog committed Nov 8, 2023
1 parent 65f2b92 commit 7d54b35
Showing 1 changed file with 30 additions and 50 deletions.
80 changes: 30 additions & 50 deletions pkg/document/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,21 @@ type chartTemplateData struct {
helm.ChartDocumentationInfo
HelmDocsVersion string
Values []valueRow
Sections []section
Sections sections
Files files
}

type sections struct {
DefaultSection section
Sections []section
}

type section struct {
SectionName string
SectionItems []valueRow
}

func sortValueRows(valueRows []valueRow) {
sortOrder := viper.GetString("sort-values-order")

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

func sortValueRowsByOrder(valueRows []valueRow, sortOrder string) {
sort.Slice(valueRows, func(i, j int) bool {
// Globals sort above non-globals.
if valueRows[i].IsGlobal != valueRows[j].IsGlobal {
Expand Down Expand Up @@ -82,47 +80,29 @@ func sortValueRows(valueRows []valueRow) {
})
}

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

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

for _, section := range sectionedValueRows {
sort.Slice(section.SectionItems, func(i, j int) bool {
// Globals sort above non-globals.
if section.SectionItems[i].IsGlobal != section.SectionItems[j].IsGlobal {
return section.SectionItems[i].IsGlobal
}
sortValueRowsByOrder(valueRows, sortOrder)
}

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

// Group dependency values together.
if section.SectionItems[i].Dependency != section.SectionItems[j].Dependency {
return section.SectionItems[i].Dependency < section.SectionItems[j].Dependency
}
}
if sortOrder != FileSortOrder && sortOrder != AlphaNumSortOrder {
log.Warnf("Invalid sort order provided %s, defaulting to %s", sortOrder, AlphaNumSortOrder)
sortOrder = AlphaNumSortOrder
}

// Sort the remaining values within the same section.SectionItems using the configured sort order.
switch sortOrder {
case FileSortOrder:
if section.SectionItems[i].LineNumber == section.SectionItems[j].LineNumber {
return section.SectionItems[i].Column < section.SectionItems[j].Column
}
return section.SectionItems[i].LineNumber < section.SectionItems[j].LineNumber
case AlphaNumSortOrder:
return section.SectionItems[i].Key < section.SectionItems[j].Key
default:
panic("cannot get here")
}
})
sortValueRowsByOrder(sectionedValueRows.DefaultSection.SectionItems, sortOrder)

for _, section := range sectionedValueRows.Sections {
sortValueRowsByOrder(section.SectionItems, sortOrder)
}
}

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

func getUnsortedSectionedValueRows(valueRows []valueRow) []section {
var valueRowsSectionSorted []section
valueRowsSectionSorted = append(valueRowsSectionSorted, section{
SectionName: "General",
func getSectionedValueRows(valueRows []valueRow) sections {
var valueRowsSectionSorted sections
valueRowsSectionSorted.DefaultSection = section{
SectionName: "Other Values",
SectionItems: []valueRow{},
})
}

for _, row := range valueRows {
if row.Section == "" {
valueRowsSectionSorted[0].SectionItems = append(valueRowsSectionSorted[0].SectionItems, row)
valueRowsSectionSorted.DefaultSection.SectionItems = append(valueRowsSectionSorted.DefaultSection.SectionItems, row)
continue
}

containsSection := false
for i, section := range valueRowsSectionSorted {
for i, section := range valueRowsSectionSorted.Sections {
if section.SectionName == row.Section {
containsSection = true
valueRowsSectionSorted[i].SectionItems = append(valueRowsSectionSorted[i].SectionItems, row)
valueRowsSectionSorted.Sections[i].SectionItems = append(valueRowsSectionSorted.Sections[i].SectionItems, row)
break
}
}

if !containsSection {
valueRowsSectionSorted = append(valueRowsSectionSorted, section{
valueRowsSectionSorted.Sections = append(valueRowsSectionSorted.Sections, section{
SectionName: row.Section,
SectionItems: []valueRow{row},
})
Expand Down Expand Up @@ -219,7 +199,7 @@ func getChartTemplateData(info helm.ChartDocumentationInfo, helmDocsVersion stri
}

sortValueRows(valuesTableRows)
valueRowsSectionSorted := getUnsortedSectionedValueRows(valuesTableRows)
valueRowsSectionSorted := getSectionedValueRows(valuesTableRows)
sortSectionedValueRows(valueRowsSectionSorted)

files, err := getFiles(info.ChartDirectory)
Expand Down

0 comments on commit 7d54b35

Please sign in to comment.