2
0

Fix: do not silently ignore assign NULL to *string

AssignTo can only assign NULL to a **string. Previous code tried to
assign nil to a *string, which did nothing. Correct behavior is to
detect this as an error.
This commit is contained in:
Jack Christensen
2018-09-01 18:40:42 -05:00
parent 79ba0275de
commit 8f7c03a47f
3 changed files with 13 additions and 7 deletions
+11 -5
View File
@@ -72,14 +72,20 @@ func (dst *JSON) Get() interface{} {
func (src *JSON) AssignTo(dst interface{}) error {
switch v := dst.(type) {
case *string:
if src.Status != Present {
v = nil
} else {
if src.Status == Present {
*v = string(src.Bytes)
} else {
return errors.Errorf("cannot assign non-present status to %T", dst)
}
case **string:
*v = new(string)
return src.AssignTo(*v)
if src.Status == Present {
s := string(src.Bytes)
*v = &s
return nil
} else {
*v = nil
return nil
}
case *[]byte:
if src.Status != Present {
*v = nil
+1 -1
View File
@@ -129,7 +129,7 @@ func TestJSONAssignTo(t *testing.T) {
t.Errorf("%d: %v", i, err)
}
if *tt.dst == tt.expected {
if *tt.dst != tt.expected {
t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, *tt.dst)
}
}
+1 -1
View File
@@ -135,7 +135,7 @@ func TestJSONBAssignTo(t *testing.T) {
t.Errorf("%d: %v", i, err)
}
if *tt.dst == tt.expected {
if *tt.dst != tt.expected {
t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, *tt.dst)
}
}