fix: --state-values-set unable to set booleans (#1199)

This pr fixes auto-wrapping of booleans and integers into quotes when using --state-values-set by:

- Adding: --state-values-set-string flag for intentional string set of boolean or integer
- Changing: --state-values-set flag not wrapping now
- Removing -

Resolves https://github.com/roboll/helmfile/issues/1347

Signed-off-by: Tunahan Sezen <sezentunahan@outlook.com>
This commit is contained in:
Tunahan Sezen
2023-12-08 21:42:40 +08:00
committed by GitHub
parent 7159e7c1ea
commit c731227e9a
5 changed files with 164 additions and 11 deletions
+60 -5
View File
@@ -2,6 +2,7 @@ package maputil
import (
"fmt"
"strconv"
"strings"
)
@@ -65,7 +66,7 @@ func recursivelyStringifyMapKey(v any) (any, error) {
type arg interface {
getMap(map[string]any) map[string]any
set(map[string]any, string)
set(map[string]any, any)
}
type keyArg struct {
@@ -85,7 +86,7 @@ func (a keyArg) getMap(m map[string]any) map[string]any {
}
}
func (a keyArg) set(m map[string]any, value string) {
func (a keyArg) set(m map[string]any, value any) {
m[a.key] = value
}
@@ -125,7 +126,7 @@ func (a indexedKeyArg) getMap(m map[string]any) map[string]any {
}
}
func (a indexedKeyArg) set(m map[string]any, value string) {
func (a indexedKeyArg) set(m map[string]any, value any) {
t := a.getArray(m)
t[a.index] = value
m[a.key] = t
@@ -178,7 +179,7 @@ func ParseKey(key string) []string {
return r
}
func Set(m map[string]any, key []string, value string) {
func Set(m map[string]any, key []string, value string, stringBool bool) {
if len(key) == 0 {
panic(fmt.Errorf("bug: unexpected length of key: %d", len(key)))
}
@@ -187,5 +188,59 @@ func Set(m map[string]any, key []string, value string) {
m, key = getCursor(key[0]).getMap(m), key[1:]
}
getCursor(key[0]).set(m, value)
getCursor(key[0]).set(m, typedVal(value, stringBool))
}
func typedVal(val string, st bool) any {
// if st is true, directly return it without casting it
if st {
return val
}
if strings.EqualFold(val, "true") {
return true
}
if strings.EqualFold(val, "false") {
return false
}
if strings.EqualFold(val, "null") {
return nil
}
// handling of only zero, if val has zero prefix, it will be considered as string
if strings.EqualFold(val, "0") {
return int64(0)
}
// If this value does not start with zero, try parsing it to an int
if len(val) != 0 && val[0] != '0' {
if iv, err := strconv.ParseInt(val, 10, 64); err == nil {
return iv
}
}
return val
}
func MergeMaps(a, b map[string]interface{}) map[string]interface{} {
out := make(map[string]interface{}, len(a))
// fill the out map with the first map
for k, v := range a {
out[k] = v
}
for k, v := range b {
if v, ok := v.(map[string]interface{}); ok {
if bv, ok := out[k]; ok {
if bv, ok := bv.(map[string]interface{}); ok {
// if b and out map has a map value, merge it too
out[k] = MergeMaps(bv, v)
continue
}
}
}
out[k] = v
}
return out
}
+77 -3
View File
@@ -69,7 +69,7 @@ func TestMapUtil_KeyArg(t *testing.T) {
key := []string{"a", "b", "c"}
Set(m, key, "C")
Set(m, key, "C", false)
c := (((m["a"].(map[string]any))["b"]).(map[string]any))["c"]
@@ -83,7 +83,7 @@ func TestMapUtil_IndexedKeyArg(t *testing.T) {
key := []string{"a", "b[0]", "c"}
Set(m, key, "C")
Set(m, key, "C", false)
c := (((m["a"].(map[string]any))["b"].([]any))[0].(map[string]any))["c"]
@@ -124,7 +124,7 @@ func TestMapUtil_IndexedKeyArg2(t *testing.T) {
k := ParseKey(op[0])
v := op[1]
Set(set, k, v)
Set(set, k, v, false)
}
}
if !reflect.DeepEqual(set, c.want) {
@@ -174,3 +174,77 @@ func TestMapUtil_ParseKey(t *testing.T) {
}
}
}
func TestMapUtil_typedVal(t *testing.T) {
typedValueTest(t, "true", true)
typedValueTest(t, "null", nil)
typedValueTest(t, "0", int64(0))
typedValueTest(t, "5", int64(5))
typedValueTest(t, "05", "05")
}
func typedValueTest(t *testing.T, input string, expectedWhenNoStr any) {
returnValue := typedVal(input, true)
if returnValue != input {
t.Errorf("unexpected typed value: expected=%s, got=%s", input, returnValue)
}
returnValue = typedVal(input, false)
if returnValue != expectedWhenNoStr {
t.Errorf("unexpected typed value: expected=%s, got=%s", input, returnValue)
}
}
func TestMapUtil_MergeMaps(t *testing.T) {
map1 := map[string]interface{}{
"debug": true,
}
map2 := map[string]interface{}{
"logLevel": "info",
"replicaCount": 3,
}
map3 := map[string]interface{}{
"logLevel": "info",
"replicaCount": map[string]any{
"app1": 3,
"awesome": 4,
},
}
map4 := map[string]interface{}{
"logLevel": "info",
"replicaCount": map[string]any{
"app1": 3,
},
}
testMap := MergeMaps(map2, map4)
equal := reflect.DeepEqual(testMap, map4)
if !equal {
t.Errorf("Expected a nested map to overwrite a flat value. Expected: %v, got %v", map4, testMap)
}
testMap = MergeMaps(map4, map2)
equal = reflect.DeepEqual(testMap, map2)
if !equal {
t.Errorf("Expected a flat value to overwrite a map. Expected: %v, got %v", map2, testMap)
}
testMap = MergeMaps(map4, map3)
equal = reflect.DeepEqual(testMap, map3)
if !equal {
t.Errorf("Expected a nested map to overwrite another nested map. Expected: %v, got %v", map3, testMap)
}
testMap = MergeMaps(map1, map3)
expectedMap := map[string]interface{}{
"debug": true,
"logLevel": "info",
"replicaCount": map[string]any{
"app1": 3,
"awesome": 4,
},
}
equal = reflect.DeepEqual(testMap, expectedMap)
if !equal {
t.Errorf("Expected a map with different keys to merge properly with another map. Expected: %v, got %v", expectedMap, testMap)
}
}