From f78ffeb6b76d9eb3eabcf43bd03420906dc9d6fa Mon Sep 17 00:00:00 2001 From: noah Date: Thu, 3 Nov 2022 13:45:20 +0900 Subject: [PATCH 1/2] Fix the bug for `GetArgs` function --- pkg/resources/tagparam.go | 21 +++++++----- pkg/resources/tagparam_test.go | 63 ++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 9 deletions(-) create mode 100644 pkg/resources/tagparam_test.go diff --git a/pkg/resources/tagparam.go b/pkg/resources/tagparam.go index b9a391db..59e8044a 100644 --- a/pkg/resources/tagparam.go +++ b/pkg/resources/tagparam.go @@ -17,7 +17,6 @@ package resources import ( "fmt" "reflect" - "strconv" "strings" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -28,30 +27,34 @@ func GetArgs(input interface{}) []string { value := strctVal(input) elements := StructElements(value) for _, element := range elements { - var val string + var arg string tagArgFormat, _ := parseTagWithName(element.Field.Tag.Get("thanos")) if tagArgFormat != "" { switch i := element.Value.Interface().(type) { case v1.Duration: if i.Duration != 0 { - val = i.String() + arg = fmt.Sprintf(tagArgFormat, i.String()) } case int: if i != 0 { - strconv.Itoa(i) + arg = fmt.Sprintf(tagArgFormat, i) } case string: - val = i + arg = fmt.Sprintf(tagArgFormat, i) case bool: // Bool params are switches don't need to render value if i { - args = append(args, tagArgFormat) + arg = tagArgFormat + } + case *bool: + if *i { + arg = tagArgFormat } default: - val = "" + arg = "" } - if val != "" { - args = append(args, fmt.Sprintf(tagArgFormat, val)) + if arg != "" { + args = append(args, arg) } } } diff --git a/pkg/resources/tagparam_test.go b/pkg/resources/tagparam_test.go new file mode 100644 index 00000000..d05c2171 --- /dev/null +++ b/pkg/resources/tagparam_test.go @@ -0,0 +1,63 @@ +package resources + +import ( + "reflect" + "testing" +) + +func Test_GetArgs(t *testing.T) { + t.Run("Return string type value.", func(t *testing.T) { + args := GetArgs(struct { + StringValue string `thanos:"arg=%s"` + }{ + StringValue: "val", + }) + + wanted := []string{"arg=val"} + if !reflect.DeepEqual(args, wanted) { + t.Fatalf("GetArgs != %v, wanted %v", args, wanted) + } + + }) + + t.Run("Return int type value.", func(t *testing.T) { + args := GetArgs(struct { + IntValue int `thanos:"arg=%d"` + }{ + IntValue: 1, + }) + + wanted := []string{"arg=1"} + if !reflect.DeepEqual(args, wanted) { + t.Fatalf("GetArgs != %v, wanted %v", args, wanted) + } + + }) + + t.Run("Return bool type value.", func(t *testing.T) { + args := GetArgs(struct { + BoolValue bool `thanos:"arg"` + }{ + BoolValue: true, + }) + + wanted := []string{"arg"} + if !reflect.DeepEqual(args, wanted) { + t.Fatalf("GetArgs != %v, wanted %v", args, wanted) + } + }) + + t.Run("Return bool pointer type value.", func(t *testing.T) { + b := true + args := GetArgs(struct { + BoolValue *bool `thanos:"arg"` + }{ + BoolValue: &b, + }) + + wanted := []string{"arg"} + if !reflect.DeepEqual(args, wanted) { + t.Fatalf("GetArgs != %v, wanted %v", args, wanted) + } + }) +} From 41ad0e708e39ce3683a01104b3e045ae625e54b5 Mon Sep 17 00:00:00 2001 From: noah Date: Thu, 3 Nov 2022 13:53:51 +0900 Subject: [PATCH 2/2] Fix the comment for the tests --- pkg/resources/tagparam_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/resources/tagparam_test.go b/pkg/resources/tagparam_test.go index d05c2171..7a9ad838 100644 --- a/pkg/resources/tagparam_test.go +++ b/pkg/resources/tagparam_test.go @@ -6,7 +6,7 @@ import ( ) func Test_GetArgs(t *testing.T) { - t.Run("Return string type value.", func(t *testing.T) { + t.Run("Return the arguemtn with a string type value.", func(t *testing.T) { args := GetArgs(struct { StringValue string `thanos:"arg=%s"` }{ @@ -20,7 +20,7 @@ func Test_GetArgs(t *testing.T) { }) - t.Run("Return int type value.", func(t *testing.T) { + t.Run("Return the argument with an int type value.", func(t *testing.T) { args := GetArgs(struct { IntValue int `thanos:"arg=%d"` }{ @@ -34,7 +34,7 @@ func Test_GetArgs(t *testing.T) { }) - t.Run("Return bool type value.", func(t *testing.T) { + t.Run("Return the argument only.", func(t *testing.T) { args := GetArgs(struct { BoolValue bool `thanos:"arg"` }{ @@ -47,7 +47,7 @@ func Test_GetArgs(t *testing.T) { } }) - t.Run("Return bool pointer type value.", func(t *testing.T) { + t.Run("Return the argument only.", func(t *testing.T) { b := true args := GetArgs(struct { BoolValue *bool `thanos:"arg"`