Add ability to get object keys and object values
This commit is contained in:
parent
4836c498a4
commit
0f420d2cae
2 changed files with 39 additions and 0 deletions
|
@ -110,6 +110,40 @@ func (d *Doc) Iff(value interface{}, path ...string) (*Doc, error) {
|
|||
return &Doc{nil}, d.Del(path...)
|
||||
}
|
||||
|
||||
// Keys retrieves the object keys at the specified path.
|
||||
func (d *Doc) Keys(path ...string) *Doc {
|
||||
|
||||
path = d.path(path...)
|
||||
|
||||
out := []interface{}{}
|
||||
|
||||
if m, ok := d.Get(path...).Data().(map[string]interface{}); ok {
|
||||
for k, _ := range m {
|
||||
out = append(out, k)
|
||||
}
|
||||
}
|
||||
|
||||
return &Doc{out}
|
||||
|
||||
}
|
||||
|
||||
// Vals retrieves the object values at the specified path.
|
||||
func (d *Doc) Vals(path ...string) *Doc {
|
||||
|
||||
path = d.path(path...)
|
||||
|
||||
out := []interface{}{}
|
||||
|
||||
if m, ok := d.Get(path...).Data().(map[string]interface{}); ok {
|
||||
for _, v := range m {
|
||||
out = append(out, v)
|
||||
}
|
||||
}
|
||||
|
||||
return &Doc{out}
|
||||
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------
|
||||
|
||||
// Exists checks whether the specified path exists.
|
||||
|
|
|
@ -335,6 +335,11 @@ func TestOperations(t *testing.T) {
|
|||
|
||||
// ----------------------------------------------------------------------------------------------------
|
||||
|
||||
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})
|
||||
})
|
||||
|
||||
// ----------------------------------------------------------------------------------------------------
|
||||
|
||||
Convey("Can get unset item", t, func() {
|
||||
|
|
Loading…
Reference in a new issue