From 834bca744cd7bb904cc47bb096706dd56bde79b2 Mon Sep 17 00:00:00 2001 From: vvakame Date: Wed, 23 Sep 2020 03:31:22 +0900 Subject: [PATCH] port support *bool like pointer filling in SetValueFromString to v3 --- utils_test.go | 20 ++++++++++++++++++++ v3/utils.go | 6 +++++- v3/utils_test.go | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/utils_test.go b/utils_test.go index b852091..752f038 100644 --- a/utils_test.go +++ b/utils_test.go @@ -264,6 +264,26 @@ func TestValueStringSliceMapper(t *testing.T) { t.Errorf("unexpected KTime[1].d: %v", d) } + if len(obj.LPtrBools) != 2 { + t.Errorf("unexpected J len: %v", len(obj.JBools)) + } + if obj.LPtrBools[0] != nil && *obj.LPtrBools[0] { + t.Errorf("unexpected J[0]: %v", obj.JBools[0]) + } + if obj.LPtrBools[1] != nil && !*obj.LPtrBools[1] { + t.Errorf("unexpected J[1]: %v", obj.JBools[1]) + } + + if len(obj.MPtrPtrBools) != 2 { + t.Errorf("unexpected J len: %v", len(obj.JBools)) + } + if obj.MPtrPtrBools[0] != nil && **obj.MPtrPtrBools[0] { + t.Errorf("unexpected J[0]: %v", obj.JBools[0]) + } + if obj.MPtrPtrBools[1] != nil && !**obj.MPtrPtrBools[1] { + t.Errorf("unexpected J[1]: %v", obj.JBools[1]) + } + if obj.YString != "This is Y" { t.Errorf("unexpected Y: %v", obj.YString) } diff --git a/v3/utils.go b/v3/utils.go index 7b7bf64..c0c76d1 100644 --- a/v3/utils.go +++ b/v3/utils.go @@ -198,8 +198,12 @@ func IsEmpty(fV reflect.Value) bool { // SetValueFromString parses string and sets value. func SetValueFromString(f reflect.Value, value string) error { ft := f.Type() - if ft.Kind() == reflect.Ptr { + + for ft.Kind() == reflect.Ptr { ft = ft.Elem() + if f.IsZero() { + f.Set(reflect.New(ft)) + } f = f.Elem() } diff --git a/v3/utils_test.go b/v3/utils_test.go index 6077694..752f038 100644 --- a/v3/utils_test.go +++ b/v3/utils_test.go @@ -29,6 +29,9 @@ type ValueStringMapperSample struct { JBool bool KTime valueStringTime + LPtrBool *bool + MPtrPtrBool **bool + ValueStringMapperSampleInner } @@ -49,6 +52,9 @@ type ValueStringSliceMapperSample struct { JBools []bool KTimes []valueStringTime + LPtrBools []*bool + MPtrPtrBools []**bool + ValueStringSliceMapperSampleInner YString string @@ -73,6 +79,8 @@ func TestValueStringMapper(t *testing.T) { valueStringMapper(target, "IFloat64", "2.75") valueStringMapper(target, "JBool", "true") valueStringMapper(target, "KTime", "2016-01-07") + valueStringMapper(target, "LPtrBool", "true") + valueStringMapper(target, "MPtrPtrBool", "true") if obj.AString != "This is A" { t.Errorf("unexpected A: %v", obj.AString) @@ -111,6 +119,12 @@ func TestValueStringMapper(t *testing.T) { } else if d != 7 { t.Errorf("unexpected KTime.d: %v", d) } + if obj.LPtrBool == nil || *obj.LPtrBool != true { + t.Errorf("unexpected L: %v", obj.LPtrBool) + } + if obj.MPtrPtrBool == nil || *obj.MPtrPtrBool == nil || **obj.MPtrPtrBool != true { + t.Errorf("unexpected M: %v", obj.LPtrBool) + } } func TestValueStringSliceMapper(t *testing.T) { @@ -127,6 +141,8 @@ func TestValueStringSliceMapper(t *testing.T) { valueStringSliceMapper(target, "IFloat64s", []string{"2.75", "22.75"}) valueStringSliceMapper(target, "JBools", []string{"true", "false"}) valueStringSliceMapper(target, "KTimes", []string{"2016-01-07", "2016-04-05"}) + valueStringSliceMapper(target, "LPtrBool", []string{"true", "false"}) + valueStringSliceMapper(target, "MPtrPtrBool", []string{"true", "false"}) valueStringSliceMapper(target, "YString", []string{"This is Y"}) valueStringSliceMapper(target, "ZString", []string{}) @@ -248,6 +264,26 @@ func TestValueStringSliceMapper(t *testing.T) { t.Errorf("unexpected KTime[1].d: %v", d) } + if len(obj.LPtrBools) != 2 { + t.Errorf("unexpected J len: %v", len(obj.JBools)) + } + if obj.LPtrBools[0] != nil && *obj.LPtrBools[0] { + t.Errorf("unexpected J[0]: %v", obj.JBools[0]) + } + if obj.LPtrBools[1] != nil && !*obj.LPtrBools[1] { + t.Errorf("unexpected J[1]: %v", obj.JBools[1]) + } + + if len(obj.MPtrPtrBools) != 2 { + t.Errorf("unexpected J len: %v", len(obj.JBools)) + } + if obj.MPtrPtrBools[0] != nil && **obj.MPtrPtrBools[0] { + t.Errorf("unexpected J[0]: %v", obj.JBools[0]) + } + if obj.MPtrPtrBools[1] != nil && !**obj.MPtrPtrBools[1] { + t.Errorf("unexpected J[1]: %v", obj.JBools[1]) + } + if obj.YString != "This is Y" { t.Errorf("unexpected Y: %v", obj.YString) }