Skip to content

Commit

Permalink
util: address various nits
Browse files Browse the repository at this point in the history
Signed-off-by: Hidde Beydals <hidde@hhh.computer>
  • Loading branch information
hiddeco committed May 9, 2023
1 parent acae3eb commit 6caad7f
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,16 @@ func ValuesChecksum(values chartutil.Values) string {
func OrderedValuesChecksum(values chartutil.Values) string {
var s []byte
if len(values) != 0 {
// Check sum on the formatted values
newValues := copyValues(values)
msValues := yaml.JSONObjectToYAMLObject(newValues)
// Sort
msValues := yaml.JSONObjectToYAMLObject(copyValues(values))
SortMapSlice(msValues)
// Marshal
s, _ = goyaml.Marshal(msValues)
}
// Get hash
return fmt.Sprintf("%x", sha1.Sum(s))
}

// SortMapSlice recursively sorts the given goyaml.MapSlice by key.
// This is used to ensure that the values checksum is the same regardless
// of the order of the keys in the values file.
func SortMapSlice(ms goyaml.MapSlice) {
sort.Slice(ms, func(i, j int) bool {
return fmt.Sprintf("%T.%v", ms[i].Key, ms[i].Key) < fmt.Sprintf("%T.%v", ms[j].Key, ms[j].Key)
Expand Down Expand Up @@ -99,15 +97,14 @@ func cleanUpInterfaceArray(in []interface{}) []interface{} {
}

func copyValues(in map[string]interface{}) map[string]interface{} {
// Marshal
coppiedValues, _ := goyaml.Marshal(in)
// Unmarshal
copiedValues, _ := goyaml.Marshal(in)
newValues := make(map[string]interface{})
goyaml.Unmarshal(coppiedValues, newValues)
// cleanUpInterfaceMap

_ = goyaml.Unmarshal(copiedValues, newValues)
for i, value := range newValues {
newValues[fmt.Sprint(i)] = cleanUpMapValue(value)
newValues[i] = cleanUpMapValue(value)
}

return newValues
}

Expand Down

0 comments on commit 6caad7f

Please sign in to comment.