Enable checking for existing and non-null items
This commit is contained in:
parent
99d9657d80
commit
fd540a5237
2 changed files with 18 additions and 0 deletions
|
@ -78,6 +78,14 @@ func (d *Doc) Reset(path ...string) (*Doc, error) {
|
||||||
return d.Set(nil, path...)
|
return d.Set(nil, path...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Valid checks whether the value at the specified path is nil.
|
||||||
|
func (d *Doc) Valid(path ...string) bool {
|
||||||
|
if !d.Exists(path...) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return d.Get(path...).Data() != nil
|
||||||
|
}
|
||||||
|
|
||||||
// Array sets the specified path to an array.
|
// Array sets the specified path to an array.
|
||||||
func (d *Doc) Array(path ...string) (*Doc, error) {
|
func (d *Doc) Array(path ...string) (*Doc, error) {
|
||||||
return d.Set([]interface{}{}, path...)
|
return d.Set([]interface{}{}, path...)
|
||||||
|
|
|
@ -334,6 +334,16 @@ func TestOperations(t *testing.T) {
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
Convey("Is unset item valid", t, func() {
|
||||||
|
So(doc.Valid("the.none"), ShouldBeFalse)
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("Is item valid", t, func() {
|
||||||
|
So(doc.Valid("the.item"), ShouldBeTrue)
|
||||||
|
})
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
Convey("Can get object keys and vals", t, func() {
|
Convey("Can get object keys and vals", t, func() {
|
||||||
So(doc.Keys("the.item.object").Data(), ShouldResemble, []interface{}{"enabled"})
|
So(doc.Keys("the.item.object").Data(), ShouldResemble, []interface{}{"enabled"})
|
||||||
So(doc.Vals("the.item.object").Data(), ShouldResemble, []interface{}{false})
|
So(doc.Vals("the.item.object").Data(), ShouldResemble, []interface{}{false})
|
||||||
|
|
Loading…
Reference in a new issue