Skip to content

Commit

Permalink
port support *bool like pointer filling in SetValueFromString to v3
Browse files Browse the repository at this point in the history
  • Loading branch information
vvakame committed Sep 22, 2020
1 parent 30b5f9c commit 834bca7
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
20 changes: 20 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 5 additions & 1 deletion v3/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
36 changes: 36 additions & 0 deletions v3/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ type ValueStringMapperSample struct {
JBool bool
KTime valueStringTime

LPtrBool *bool
MPtrPtrBool **bool

ValueStringMapperSampleInner
}

Expand All @@ -49,6 +52,9 @@ type ValueStringSliceMapperSample struct {
JBools []bool
KTimes []valueStringTime

LPtrBools []*bool
MPtrPtrBools []**bool

ValueStringSliceMapperSampleInner

YString string
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand All @@ -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{})

Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 834bca7

Please sign in to comment.