MCPcopy Create free account
hub / github.com/daodst/chat / lookupMapPath

Function lookupMapPath

internal/pushrules/util.go:74–93  ·  view source on GitHub ↗

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{})

Source from the content-addressed store, hash-verified

72// produced by json.Unmarshal, to return the leaf value. Traversing
73// arrays/slices is not supported, only objects/maps.
74func 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

Callers 3

TestLookupMapPathFunction · 0.85
TestLookupMapPathInvalidFunction · 0.85
patternMatchesFunction · 0.85

Calls

no outgoing calls

Tested by 2

TestLookupMapPathFunction · 0.68
TestLookupMapPathInvalidFunction · 0.68