Enable checking for existing and non-null items

This commit is contained in:
Tobie Morgan Hitchcock 2016-10-24 14:16:33 +01:00
parent 99d9657d80
commit fd540a5237
2 changed files with 18 additions and 0 deletions

View file

@ -78,6 +78,14 @@ func (d *Doc) Reset(path ...string) (*Doc, error) {
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.
func (d *Doc) Array(path ...string) (*Doc, error) {
return d.Set([]interface{}{}, path...)

View file

@ -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() {
So(doc.Keys("the.item.object").Data(), ShouldResemble, []interface{}{"enabled"})
So(doc.Vals("the.item.object").Data(), ShouldResemble, []interface{}{false})