omitempty
Kubernetes-commit: 1347c094ce51f1fbababe0a2866c9046ea9ba1cc
This commit is contained in:
parent
8d690cc5cb
commit
0b031bbb8b
|
@ -48,12 +48,6 @@ func (m *ColonSeparatedMultimapStringString) Set(value string) error {
|
|||
if m.Multimap == nil {
|
||||
return fmt.Errorf("no target (nil pointer to map[string][]string)")
|
||||
}
|
||||
// allow explicit nil multimap
|
||||
if value == "nil" {
|
||||
*m.Multimap = nil
|
||||
m.initialized = true
|
||||
return nil
|
||||
}
|
||||
if !m.initialized || *m.Multimap == nil {
|
||||
// clear default values, or allocate if no existing map
|
||||
*m.Multimap = make(map[string][]string)
|
||||
|
@ -76,10 +70,6 @@ func (m *ColonSeparatedMultimapStringString) Set(value string) error {
|
|||
|
||||
// String implements github.com/spf13/pflag.Value
|
||||
func (m *ColonSeparatedMultimapStringString) String() string {
|
||||
if *m.Multimap == nil {
|
||||
return "nil"
|
||||
}
|
||||
|
||||
type kv struct {
|
||||
k string
|
||||
v string
|
||||
|
@ -105,3 +95,8 @@ func (m *ColonSeparatedMultimapStringString) String() string {
|
|||
func (m *ColonSeparatedMultimapStringString) Type() string {
|
||||
return "colonSeparatedMultimapStringString"
|
||||
}
|
||||
|
||||
// Empty implements OmitEmpty
|
||||
func (m *ColonSeparatedMultimapStringString) Empty() bool {
|
||||
return len(*m.Multimap) == 0
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ func TestStringColonSeparatedMultimapStringString(t *testing.T) {
|
|||
m *ColonSeparatedMultimapStringString
|
||||
expect string
|
||||
}{
|
||||
{"nil", NewColonSeparatedMultimapStringString(&nilMap), "nil"},
|
||||
{"nil", NewColonSeparatedMultimapStringString(&nilMap), ""},
|
||||
{"empty", NewColonSeparatedMultimapStringString(&map[string][]string{}), ""},
|
||||
{"empty key", NewColonSeparatedMultimapStringString(
|
||||
&map[string][]string{
|
||||
|
@ -85,12 +85,6 @@ func TestSetColonSeparatedMultimapStringString(t *testing.T) {
|
|||
&ColonSeparatedMultimapStringString{
|
||||
initialized: true,
|
||||
Multimap: &map[string][]string{}}, ""},
|
||||
{"explicitly nil", []string{"nil"},
|
||||
NewColonSeparatedMultimapStringString(&map[string][]string{"default": {}}),
|
||||
&ColonSeparatedMultimapStringString{
|
||||
initialized: true,
|
||||
Multimap: &nilMap,
|
||||
}, ""},
|
||||
// make sure we still allocate for "initialized" multimaps where Multimap was initially set to a nil map
|
||||
{"allocates map if currently nil", []string{""},
|
||||
&ColonSeparatedMultimapStringString{initialized: true, Multimap: &nilMap},
|
||||
|
@ -225,3 +219,24 @@ func TestRoundTripColonSeparatedMultimapStringString(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmptyColonSeparatedMultimapStringString(t *testing.T) {
|
||||
var nilMap map[string][]string
|
||||
cases := []struct {
|
||||
desc string
|
||||
val *ColonSeparatedMultimapStringString
|
||||
expect bool
|
||||
}{
|
||||
{"nil", NewColonSeparatedMultimapStringString(&nilMap), true},
|
||||
{"empty", NewColonSeparatedMultimapStringString(&map[string][]string{}), true},
|
||||
{"populated", NewColonSeparatedMultimapStringString(&map[string][]string{"foo": {}}), false},
|
||||
}
|
||||
for _, c := range cases {
|
||||
t.Run(c.desc, func(t *testing.T) {
|
||||
result := c.val.Empty()
|
||||
if result != c.expect {
|
||||
t.Fatalf("expect %t but got %t", c.expect, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue