Skip to content

Commit

Permalink
name the sort func
Browse files Browse the repository at this point in the history
  • Loading branch information
mafredri committed Nov 13, 2024
1 parent 457cbbe commit ad8fe14
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,31 +333,32 @@ func (optSet *OptionSet) SetDefaults() error {
groupByValue[opt.Value] = append(groupByValue[opt.Value], opt)
}

for _, opts := range groupByValue {
// Sort the options by priority and whether or not a default is
// set. This won't affect the value but represents correctness
// from whence the value originated.
slices.SortFunc(opts, func(a, b *Option) int {
if a.ValueSource != b.ValueSource {
for _, vs := range valueSourcePriority {
if a.ValueSource == vs {
return -1
}
if b.ValueSource == vs {
return 1
}
sortOptionByValueSourcePriorityOrDefault := func(a, b *Option) int {
if a.ValueSource != b.ValueSource {
for _, vs := range valueSourcePriority {
if a.ValueSource == vs {
return -1
}
}
if a.Default != b.Default {
if a.Default == "" {
if b.ValueSource == vs {
return 1
}
if b.Default == "" {
return -1
}
}
return 0
})
}
if a.Default != b.Default {
if a.Default == "" {
return 1
}
if b.Default == "" {
return -1
}
}
return 0
}
for _, opts := range groupByValue {
// Sort the options by priority and whether or not a default is
// set. This won't affect the value but represents correctness
// from whence the value originated.
slices.SortFunc(opts, sortOptionByValueSourcePriorityOrDefault)

// If the first option has a value source, then we don't need to
// set the default, but mark the source for all options.
Expand Down

0 comments on commit ad8fe14

Please # to comment.