lookupMapPath traverses a hierarchical map structure, like the one produced by json.Unmarshal, to return the leaf value. Traversing arrays/slices is not supported, only objects/maps.
(path []string, m map[string]interface{})
| 72 | // produced by json.Unmarshal, to return the leaf value. Traversing |
| 73 | // arrays/slices is not supported, only objects/maps. |
| 74 | func lookupMapPath(path []string, m map[string]interface{}) (interface{}, error) { |
| 75 | if len(path) == 0 { |
| 76 | return nil, fmt.Errorf("empty path") |
| 77 | } |
| 78 | |
| 79 | var v interface{} = m |
| 80 | for i, key := range path { |
| 81 | m, ok := v.(map[string]interface{}) |
| 82 | if !ok { |
| 83 | return nil, fmt.Errorf("expected an object for path %q, but got %T", strings.Join(path[:i+1], "."), v) |
| 84 | } |
| 85 | |
| 86 | v, ok = m[key] |
| 87 | if !ok { |
| 88 | return nil, fmt.Errorf("path not found: %s", strings.Join(path[:i+1], ".")) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | return v, nil |
| 93 | } |
| 94 | |
| 95 | // parseRoomMemberCountCondition parses a string like "2", "==2", "<2" |
| 96 | // into a function that checks if the argument to it fulfils the |
no outgoing calls