mcclient: options: test marshal string ptr

This commit is contained in:
Yousong Zhou
2020-01-14 21:36:50 +08:00
parent dd01610931
commit 3f684ef94c
2 changed files with 31 additions and 0 deletions
+11
View File
@@ -37,6 +37,10 @@ func Bool(v bool) *bool {
return &v
}
func String(v string) *string {
return &v
}
// IntV returns the integer value as pointed to by the argument if it's
// non-nil, return 0 otherwise
func IntV(p *int) int {
@@ -55,6 +59,13 @@ func BoolV(p *bool) bool {
return false
}
func StringV(p *string) string {
if p != nil {
return *p
}
return ""
}
type IParamsOptions interface {
Params() (*jsonutils.JSONDict, error)
}
+20
View File
@@ -142,6 +142,26 @@ func TestOptionsStructToParams(t *testing.T) {
}
testSs(t, cases)
})
t.Run("string ptr", func(t *testing.T) {
type s struct {
String *string `json:",allowempty"`
}
cases := []*S{
{
In: &s{},
Want: `{}`,
},
{
In: &s{String("")},
Want: `{"string": ""}`,
},
{
In: &s{String("holy")},
Want: `{string: "holy"}`,
},
}
testSs(t, cases)
})
t.Run("string slice", func(t *testing.T) {
type s struct {
StringSlice []string