GetNodeFunc is a general-purpose method for finding object properties by their key. The provided function is called with each key in turn. The first time that function returns true the corresponding *Node for that key is returned.
(fn func(key string) bool)
| 44 | // with each key in turn. The first time that function returns |
| 45 | // true the corresponding *Node for that key is returned. |
| 46 | func (o Object) GetNodeFunc(fn func(key string) bool) *Node { |
| 47 | if !o.HasValidNode() { |
| 48 | return &Node{} |
| 49 | } |
| 50 | |
| 51 | count := int(o.node.NamedChildCount()) |
| 52 | |
| 53 | for i := 0; i < count; i++ { |
| 54 | pair := o.node.NamedChild(i) |
| 55 | |
| 56 | if pair.Type() != "pair" { |
| 57 | continue |
| 58 | } |
| 59 | |
| 60 | if !fn(pair.ChildByFieldName("key").RawString()) { |
| 61 | continue |
| 62 | } |
| 63 | |
| 64 | return pair.ChildByFieldName("value") |
| 65 | } |
| 66 | return nil |
| 67 | } |
| 68 | |
| 69 | // GetNode returns the matching *Node for a given key |
| 70 | func (o Object) GetNode(key string) *Node { |
no test coverage detected