forked from zabawaba99/firego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnapshot.go
51 lines (41 loc) · 1.07 KB
/
snapshot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package firego
import (
"strings"
"github.com/zabawaba99/firego/sync"
)
// DataSnapshot instances contains data from a Firebase reference.
type DataSnapshot struct {
// Key retrieves the key for the source location of this snapshot
Key string
// Value retrieves the data contained in this snapshot.
Value interface{}
}
func newSnapshot(node *sync.Node) DataSnapshot {
return DataSnapshot{
Key: node.Key,
Value: node.Objectify(),
}
}
// Child gets a DataSnapshot for the location at the specified relative path.
// The relative path can either be a simple child key (e.g. 'fred') or a deeper
// slash-separated path (e.g. 'fred/name/first').
func (d *DataSnapshot) Child(name string) (DataSnapshot, bool) {
name = strings.Trim(name, "/")
rabbitHole := strings.Split(name, "/")
current := *d
for _, tkn := range rabbitHole {
children, ok := current.Value.(map[string]interface{})
if !ok {
return current, false
}
v, ok := children[tkn]
if !ok {
return current, ok
}
current = DataSnapshot{
Key: tkn,
Value: v,
}
}
return current, true
}