Skip to content

Commit e491505

Browse files
added code to delete the keys whose values are set to empty
1 parent 8d0e83c commit e491505

2 files changed

Lines changed: 95 additions & 2 deletions

File tree

pkg/util/strvals/strvals.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"bytes"
1818
"fmt"
1919
"io"
20+
"reflect"
2021
"strconv"
2122
"strings"
2223

@@ -250,7 +251,18 @@ func set(data map[string]interface{}, key string, val interface{}) {
250251
if len(key) == 0 {
251252
return
252253
}
253-
data[key] = val
254+
255+
rt := reflect.TypeOf(val)
256+
switch rt.Kind() {
257+
case reflect.String:
258+
if len(val.(string)) < 1 {
259+
data[key] = nil
260+
} else {
261+
data[key] = val
262+
}
263+
default:
264+
data[key] = val
265+
}
254266
}
255267

256268
func setIndex(list []interface{}, index int, val interface{}) (l2 []interface{}, err error) {
@@ -271,7 +283,16 @@ func setIndex(list []interface{}, index int, val interface{}) (l2 []interface{},
271283
copy(newlist, list)
272284
list = newlist
273285
}
274-
list[index] = val
286+
switch reflect.TypeOf(val).Kind() {
287+
case reflect.String:
288+
if len(val.(string)) > 0 {
289+
list[index] = val
290+
} else {
291+
list[index] = nil
292+
}
293+
default:
294+
list[index] = val
295+
}
275296
return list, nil
276297
}
277298

pkg/util/strvals/strvals_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package strvals
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"github.com/pkg/errors"
7+
"gotest.tools/assert"
8+
"log"
9+
"testing"
10+
)
11+
12+
func TestSetStringFlag(t *testing.T) {
13+
rawConfig := getActualRawConfig()
14+
s := "deployments.dev.helm.values.containers[0]="
15+
err := ParseIntoString(s, rawConfig)
16+
if err != nil {
17+
fmt.Println(errors.Wrap(err, "parsing --set-string flag"))
18+
log.Fatal(err)
19+
}
20+
b, err := json.Marshal(rawConfig)
21+
if err != nil {
22+
fmt.Println(err)
23+
}
24+
fmt.Println("output : " + string(b))
25+
assert.DeepEqual(t, rawConfig, getExpectedRawConfigForSetString())
26+
}
27+
28+
func TestSetFlag(t *testing.T) {
29+
s := "deployments.dev.helm.values.containers[1].image="
30+
rawConfig := getActualRawConfig()
31+
err := ParseInto(s, rawConfig)
32+
if err != nil {
33+
fmt.Println(errors.Wrap(err, "parsing --set flag"))
34+
log.Fatal(err)
35+
}
36+
b, err := json.Marshal(rawConfig)
37+
if err != nil {
38+
fmt.Println(err)
39+
}
40+
fmt.Println("output : " + string(b))
41+
assert.DeepEqual(t, rawConfig, getExpectedRawConfigForSet())
42+
}
43+
44+
func getExpectedRawConfigForSet() map[string]interface{} {
45+
jsonStr := "{\"deployments\":{\"dev\":{\"helm\":{\"values\":{\"containers\":[{\"image\":\"alpine\"},{\"image\":null}]}}}},\"name\":\"run-pipelines-demo\",\"pipelines\":{\"deploy\":\"create_deployments --all\",\"dev\":\"run_pipelines deploy --set deployments.dev.helm.values.containers[0].image=nginx --set-string deployments.dev.helm.values.containers[0].name=mynginx\"},\"version\":\"v2beta1\"}"
46+
rawConfig := map[string]interface{}{}
47+
err := json.Unmarshal([]byte(jsonStr), &rawConfig)
48+
if err != nil {
49+
fmt.Println(err)
50+
}
51+
return rawConfig
52+
}
53+
54+
func getExpectedRawConfigForSetString() map[string]interface{} {
55+
jsonStr := "{\"deployments\":{\"dev\":{\"helm\":{\"values\":{\"containers\":[null,{\"image\":\"ns\"}]}}}},\"name\":\"run-pipelines-demo\",\"pipelines\":{\"deploy\":\"create_deployments --all\",\"dev\":\"run_pipelines deploy --set deployments.dev.helm.values.containers[0].image=nginx --set-string deployments.dev.helm.values.containers[0].name=mynginx\"},\"version\":\"v2beta1\"}"
56+
rawConfig := map[string]interface{}{}
57+
err := json.Unmarshal([]byte(jsonStr), &rawConfig)
58+
if err != nil {
59+
fmt.Println(err)
60+
}
61+
return rawConfig
62+
}
63+
64+
func getActualRawConfig() map[string]interface{} {
65+
jsonStr := "{\n \"deployments\": {\n \"dev\": { \"helm\": { \"values\": { \"containers\": [{ \"image\": \"alpine\" },{ \"image\": \"ns\" }] } } }\n },\n \"name\": \"run-pipelines-demo\",\n \"pipelines\": {\n \"deploy\": \"create_deployments --all\",\n \"dev\": \"run_pipelines deploy --set deployments.dev.helm.values.containers[0].image=nginx --set-string deployments.dev.helm.values.containers[0].name=mynginx\"\n },\n \"version\": \"v2beta1\"\n}\n"
66+
rawConfig := map[string]interface{}{}
67+
err := json.Unmarshal([]byte(jsonStr), &rawConfig)
68+
if err != nil {
69+
fmt.Println(err)
70+
}
71+
return rawConfig
72+
}

0 commit comments

Comments
 (0)