ensureSingleSelectOptionBefore ensures an option exists before a specific option If beforeName is not found in the options list, the desired option is appended to the end
(options []singleSelectOption, desired singleSelectOption, beforeName string)
| 725 | // ensureSingleSelectOptionBefore ensures an option exists before a specific option |
| 726 | // If beforeName is not found in the options list, the desired option is appended to the end |
| 727 | func ensureSingleSelectOptionBefore(options []singleSelectOption, desired singleSelectOption, beforeName string) ([]singleSelectOption, bool) { |
| 728 | var existing *singleSelectOption |
| 729 | without := make([]singleSelectOption, 0, len(options)) |
| 730 | |
| 731 | // Find if the desired option already exists and collect other options |
| 732 | for _, opt := range options { |
| 733 | if opt.Name == desired.Name { |
| 734 | if existing == nil { |
| 735 | copyOpt := opt |
| 736 | existing = ©Opt |
| 737 | } |
| 738 | continue |
| 739 | } |
| 740 | without = append(without, opt) |
| 741 | } |
| 742 | |
| 743 | // Determine what to insert |
| 744 | toInsert := desired |
| 745 | if existing != nil { |
| 746 | toInsert = *existing |
| 747 | toInsert.Color = desired.Color |
| 748 | if desired.Description != "" { |
| 749 | toInsert.Description = desired.Description |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | // Find insertion point (before the specified option, or at end if not found) |
| 754 | insertAt := len(without) |
| 755 | for i, opt := range without { |
| 756 | if opt.Name == beforeName { |
| 757 | insertAt = i |
| 758 | break |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | // Build the updated list with the option inserted |
| 763 | withInserted := make([]singleSelectOption, 0, len(without)+1) |
| 764 | withInserted = append(withInserted, without[:insertAt]...) |
| 765 | withInserted = append(withInserted, toInsert) |
| 766 | withInserted = append(withInserted, without[insertAt:]...) |
| 767 | |
| 768 | // Check if anything changed |
| 769 | return withInserted, !singleSelectOptionsEqual(options, withInserted) |
| 770 | } |
| 771 | |
| 772 | // singleSelectOptionsEqual checks if two option slices are equal |
| 773 | func singleSelectOptionsEqual(a, b []singleSelectOption) bool { |