Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the bug of arguments generation #190

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions pkg/resources/tagparam.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package resources
import (
"fmt"
"reflect"
"strconv"
"strings"

v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -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)
}
}
}
Expand Down
63 changes: 63 additions & 0 deletions pkg/resources/tagparam_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package resources

import (
"reflect"
"testing"
)

func Test_GetArgs(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"`
}{
StringValue: "val",
})

wanted := []string{"arg=val"}
if !reflect.DeepEqual(args, wanted) {
t.Fatalf("GetArgs != %v, wanted %v", args, wanted)
}

})

t.Run("Return the argument with an 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 the argument only.", 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 the argument only.", 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)
}
})
}