Skip to content

Commit 8846515

Browse files
committed
[feat] filter attributes when extracting sub-keys
1 parent e06b156 commit 8846515

1 file changed

Lines changed: 32 additions & 5 deletions

File tree

jdict.m

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@
171171

172172
if (strcmp(idx.type, '.') && isnumeric(idx.subs))
173173
val = val(idx.subs);
174-
elseif ((strcmp(idx.type, '()') || strcmp(idx.type, '.')) && ischar(idx.subs) && ismember(idx.subs, {'tojson', 'fromjson', 'v', 'keys', 'len', 'setattr', 'getattr'}) && i < oplen)
174+
elseif ((strcmp(idx.type, '()') || strcmp(idx.type, '.')) && ischar(idx.subs) && ismember(idx.subs, {'tojson', 'fromjson', 'v', 'keys', 'len', 'size', 'setattr', 'getattr'}) && i < oplen)
175175
if (strcmp(idx.subs, 'v'))
176176
if (iscell(val) && strcmp(idxkey(i + 1).type, '()'))
177177
idxkey(i + 1).type = '{}';
@@ -278,8 +278,14 @@
278278
return
279279
elseif (~(isempty(idxkey(end).subs) && (strcmp(idxkey(end).type, '()') || strcmp(idxkey(end).type, '{}'))))
280280
newobj = jdict(val);
281-
newobj.attr = obj.attr;
282-
newobj.currentpath = trackpath;
281+
attrkeys = keys(obj.attr);
282+
newobj.attr = containers.Map();
283+
for i = 1:length(attrkeys)
284+
if (strncmp(attrkeys{i}, trackpath, length(trackpath)))
285+
newobj.attr(strrep(attrkeys{i}, trackpath, char(36))) = obj.attr(attrkeys{i});
286+
end
287+
end
288+
newobj.currentpath = char(36);
283289
val = newobj;
284290
end
285291
varargout{1} = val;
@@ -418,7 +424,12 @@
418424
idx.type = '()';
419425
opcell{i}(idx.subs) = obj.newkey_();
420426
elseif (isstruct(opcell{i}) && ~isfield(opcell{i}, idx.subs))
421-
opcell{i}.(idx.subs) = obj.newkey_();
427+
try
428+
opcell{i}.(idx.subs) = obj.newkey_();
429+
catch
430+
opcell{i} = containers.Map(fieldnames(opcell{i}), struct2cell(opcell{i}));
431+
opcell{i}(idx.subs) = obj.newkey_();
432+
end
422433
end
423434
end
424435
end
@@ -433,6 +444,9 @@
433444
opcell{i}(idx.subs) = otherobj;
434445
opcell{end - 1} = opcell{i};
435446
else
447+
if (isa(opcell{i}, 'containers.Map') || isa(opcell{i}, 'dictionary'))
448+
idx = struct('type', '()', 'subs', idx.subs);
449+
end
436450
opcell{end - 1} = subsasgn(opcell{i}, idx, otherobj);
437451
end
438452

@@ -485,7 +499,16 @@
485499

486500
function val = len(obj)
487501
% return the number of subfields at the current level
488-
val = length(obj.data);
502+
if (isstruct(obj.data))
503+
val = length(fieldnames(obj.data));
504+
else
505+
val = length(obj.data);
506+
end
507+
end
508+
509+
function val = size(obj)
510+
% return the dimension vector of the data
511+
val = size(obj.data);
489512
end
490513

491514
function val = v(obj, varargin)
@@ -544,6 +567,10 @@
544567
end
545568

546569
function val = getattr(obj, jsonpath, attrname)
570+
if (nargin == 1)
571+
val = keys(obj.attr);
572+
return
573+
end
547574
if (nargin == 2)
548575
attrname = jsonpath;
549576
jsonpath = obj.currentpath;

0 commit comments

Comments
 (0)