Array() from null becomes zero length Go array

fixes #53
This commit is contained in:
Josh Baker 2017-12-01 14:13:24 -07:00
parent 67e2a63ac7
commit 182ad76050
2 changed files with 22 additions and 3 deletions

View File

@ -177,8 +177,8 @@ func (t Result) Time() time.Time {
// If the result represents a non-existent value, then an empty array will be returned. // If the result represents a non-existent value, then an empty array will be returned.
// If the result is not a JSON array, the return value will be an array containing one result. // If the result is not a JSON array, the return value will be an array containing one result.
func (t Result) Array() []Result { func (t Result) Array() []Result {
if !t.Exists() { if t.Type == Null {
return nil return []Result{}
} }
if t.Type != JSON { if t.Type != JSON {
return []Result{t} return []Result{t}
@ -192,7 +192,7 @@ func (t Result) IsObject() bool {
return t.Type == JSON && len(t.Raw) > 0 && t.Raw[0] == '{' return t.Type == JSON && len(t.Raw) > 0 && t.Raw[0] == '{'
} }
// IsObject returns true if the result value is a JSON array. // IsArray returns true if the result value is a JSON array.
func (t Result) IsArray() bool { func (t Result) IsArray() bool {
return t.Type == JSON && len(t.Raw) > 0 && t.Raw[0] == '[' return t.Type == JSON && len(t.Raw) > 0 && t.Raw[0] == '['
} }

View File

@ -1110,3 +1110,22 @@ func TestResultRawForLiteral(t *testing.T) {
} }
} }
} }
func TestNullArray(t *testing.T) {
n := len(Get(`{"data":null}`, "data").Array())
if n != 0 {
t.Fatalf("expected '%v', got '%v'", 0, n)
}
n = len(Get(`{}`, "data").Array())
if n != 0 {
t.Fatalf("expected '%v', got '%v'", 0, n)
}
n = len(Get(`{"data":[]}`, "data").Array())
if n != 0 {
t.Fatalf("expected '%v', got '%v'", 0, n)
}
n = len(Get(`{"data":[null]}`, "data").Array())
if n != 1 {
t.Fatalf("expected '%v', got '%v'", 1, n)
}
}