From 3f684ef94cb87770ff5518ec97f3edb21c84596a Mon Sep 17 00:00:00 2001 From: Yousong Zhou Date: Tue, 14 Jan 2020 21:26:14 +0800 Subject: [PATCH] mcclient: options: test marshal string ptr --- pkg/mcclient/options/base.go | 11 +++++++++++ pkg/mcclient/options/base_test.go | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/pkg/mcclient/options/base.go b/pkg/mcclient/options/base.go index dd3114b7ef..6473527ac0 100644 --- a/pkg/mcclient/options/base.go +++ b/pkg/mcclient/options/base.go @@ -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) } diff --git a/pkg/mcclient/options/base_test.go b/pkg/mcclient/options/base_test.go index b5ad5ab2ef..90ffde2abd 100644 --- a/pkg/mcclient/options/base_test.go +++ b/pkg/mcclient/options/base_test.go @@ -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