-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathresolvePath.m
81 lines (75 loc) · 1.84 KB
/
resolvePath.m
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
function o = resolvePath(nwb, path)
dotTok = split(path, '.');
tokens = split(dotTok{1}, '/');
%skip first `/` if it exists
if isempty(tokens{1})
tokens(1) = [];
end
%process slash tokens
o = nwb;
while ~isempty(tokens)
if isa(o, 'types.untyped.Set')
[o, tokens] = resolveSet(o, tokens);
elseif isa(o, 'types.untyped.Anon')
[o, tokens] = resolveAnon(o, tokens);
else
[o, tokens] = resolveObj(o, tokens);
end
if isempty(o)
error('NWB:IO:UnresolvedPath', 'Could not resolve path `%s`.', path);
end
end
end
function [o, remainder] = resolveSet(obj, tokens)
tok = tokens{1};
if any(strcmp(keys(obj), tok))
o = obj.get(tok);
remainder = tokens(2:end);
else
o = [];
remainder = tokens;
end
end
function [o, remainder] = resolveAnon(obj, tokens)
tok = tokens{1};
if strcmp(obj.name, tok)
o = obj.value;
remainder = tokens(2:end);
else
o = [];
remainder = tokens;
end
end
function [o, remainder] = resolveObj(obj, tokens)
props = properties(obj);
toklen = length(tokens);
eagerlist = cell(toklen,1);
for i=1:toklen
eagerlist{i} = strjoin(tokens(1:i), '_');
end
% stable in this case preserves ordering with eagerlist bias
[eagers, ei, ~] = intersect(eagerlist, props, 'stable');
if ~isempty(eagers)
o = obj.(eagers{end});
remainder = tokens(ei(end)+1:end);
return;
end
% go one level down and check for sets
proplen = length(props);
issetprops = false(proplen, 1);
for i=1:proplen
issetprops(i) = isa(obj.(props{i}), 'types.untyped.Set');
end
setprops = props(issetprops);
setpropslen = length(setprops);
minlen = length(tokens) + 1;
for i=1:setpropslen
[new_o, new_tokens] = resolveSet(obj.(setprops{i}), tokens);
new_toklen = length(new_tokens);
if new_toklen < minlen
o = new_o;
remainder = new_tokens;
minlen = new_toklen;
end
end
end