Merge pull request #32118 from box4wangjing/main

refactor: use slices.Backward to simplify the code
pull/32124/head
Terry Howe 4 months ago committed by GitHub
commit b2786f15f2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -19,6 +19,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"log/slog" "log/slog"
"slices"
"strings" "strings"
chart "helm.sh/helm/v4/internal/chart/v3" chart "helm.sh/helm/v4/internal/chart/v3"
@ -242,8 +243,8 @@ func set(path []string, data map[string]any) map[string]any {
return nil return nil
} }
cur := data cur := data
for i := len(path) - 1; i >= 0; i-- { for _, v := range slices.Backward(path) {
cur = map[string]any{path[i]: cur} cur = map[string]any{v: cur}
} }
return cur return cur
} }

@ -150,8 +150,8 @@ func (cfg *Configuration) execHookWithDelayedShutdown(rl *release.Release, hook
return func() error { return func() error {
// If all hooks are successful, check the annotation of each hook to determine whether the hook should be deleted // If all hooks are successful, check the annotation of each hook to determine whether the hook should be deleted
// or output should be logged under succeeded condition. If so, then clear the corresponding resource object in each hook // or output should be logged under succeeded condition. If so, then clear the corresponding resource object in each hook
for i := len(executingHooks) - 1; i >= 0; i-- { for _, v := range slices.Backward(executingHooks) {
h := executingHooks[i] h := v
if err := cfg.outputLogsByPolicy(h, rl.Namespace, release.HookOutputOnSucceeded); err != nil { if err := cfg.outputLogsByPolicy(h, rl.Namespace, release.HookOutputOnSucceeded); err != nil {
// We log here as we still want to attempt hook resource deletion even if output logging fails. // We log here as we still want to attempt hook resource deletion even if output logging fails.
log.Printf("error outputting logs for hook failure: %v", err) log.Printf("error outputting logs for hook failure: %v", err)

@ -19,6 +19,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"log/slog" "log/slog"
"slices"
"strings" "strings"
"helm.sh/helm/v4/internal/copystructure" "helm.sh/helm/v4/internal/copystructure"
@ -242,8 +243,8 @@ func set(path []string, data map[string]any) map[string]any {
return nil return nil
} }
cur := data cur := data
for i := len(path) - 1; i >= 0; i-- { for _, v := range slices.Backward(path) {
cur = map[string]any{path[i]: cur} cur = map[string]any{v: cur}
} }
return cur return cur
} }

@ -20,6 +20,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"slices"
"strconv" "strconv"
"time" "time"
@ -207,8 +208,8 @@ func getHistory(client *action.History, name string) (releaseHistory, error) {
} }
func getReleaseHistory(rls []*release.Release) (history releaseHistory) { func getReleaseHistory(rls []*release.Release) (history releaseHistory) {
for i := len(rls) - 1; i >= 0; i-- { for _, v := range slices.Backward(rls) {
r := rls[i] r := v
c := formatChartName(r.Chart) c := formatChartName(r.Chart)
s := r.Info.Status.String() s := r.Info.Status.String()
v := r.Version v := r.Version

@ -25,6 +25,7 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"slices"
"sort" "sort"
"strings" "strings"
"time" "time"
@ -356,21 +357,21 @@ func loadIndex(data []byte, source string) (*IndexFile, error) {
} }
for name, cvs := range i.Entries { for name, cvs := range i.Entries {
for idx := len(cvs) - 1; idx >= 0; idx-- { for idx, v := range slices.Backward(cvs) {
if cvs[idx] == nil { if v == nil {
slog.Warn(fmt.Sprintf("skipping loading invalid entry for chart %q from %s: empty entry", name, source)) slog.Warn(fmt.Sprintf("skipping loading invalid entry for chart %q from %s: empty entry", name, source))
cvs = append(cvs[:idx], cvs[idx+1:]...) cvs = append(cvs[:idx], cvs[idx+1:]...)
continue continue
} }
// When metadata section missing, initialize with no data // When metadata section missing, initialize with no data
if cvs[idx].Metadata == nil { if v.Metadata == nil {
cvs[idx].Metadata = &chart.Metadata{} v.Metadata = &chart.Metadata{}
} }
if cvs[idx].APIVersion == "" { if v.APIVersion == "" {
cvs[idx].APIVersion = chart.APIVersionV1 v.APIVersion = chart.APIVersionV1
} }
if err := cvs[idx].Validate(); ignoreSkippableChartValidationError(err) != nil { if err := v.Validate(); ignoreSkippableChartValidationError(err) != nil {
slog.Warn(fmt.Sprintf("skipping loading invalid entry for chart %q %q from %s: %s", name, cvs[idx].Version, source, err)) slog.Warn(fmt.Sprintf("skipping loading invalid entry for chart %q %q from %s: %s", name, v.Version, source, err))
cvs = append(cvs[:idx], cvs[idx+1:]...) cvs = append(cvs[:idx], cvs[idx+1:]...)
} }
} }

Loading…
Cancel
Save