Skip to content

Commit 56013db

Browse files
committed
[doc] add back removed comments
1 parent c851ee2 commit 56013db

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

jdict.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@
404404
% overloaded assignment operator: handling assignments at arbitrary depths
405405
function obj = subsasgn(obj, idxkey, otherobj)
406406
% overloading the setter function, obj.('key').('subkey')=otherobj
407+
% expanded from rahnema1's sample at https://stackoverflow.com/a/79030223/4271392
407408

408409
% handle curly bracket indexing for setting attributes
409410
oplen = length(idxkey);
@@ -422,6 +423,7 @@
422423
if (iscell(idxkey(oplen).subs) && ~isempty(idxkey(oplen).subs))
423424
attrn = idxkey(oplen).subs{1};
424425
if (ischar(attrn))
426+
% Build the path by navigating through keys
425427
temppath = obj.currentpath__;
426428
for i = 1:oplen - 1
427429
idx = idxkey(i);
@@ -441,6 +443,7 @@
441443
end
442444
end
443445
end
446+
% set attribute on original object with computed path
444447
obj.setattr(temppath, attrn, otherobj);
445448
return
446449
end
@@ -451,6 +454,7 @@
451454
if (oplen >= 2 && strcmp(idxkey(oplen).type, '()'))
452455
if (strcmp(idxkey(oplen - 1).type, '.') && ischar(idxkey(oplen - 1).subs))
453456
dimname = idxkey(oplen - 1).subs;
457+
% build path to the data
454458
temppath = obj.currentpath__;
455459
for i = 1:oplen - 2
456460
idx = idxkey(i);
@@ -470,15 +474,19 @@
470474
end
471475
end
472476
end
477+
% check if dimname is in dims
473478
dims = obj.getattr(temppath, 'dims');
474479
if (~isempty(dims) && iscell(dims))
475480
dimpos = find(strcmp(dims, dimname));
476481
if (~isempty(dimpos) && ~isempty(idxkey(oplen).subs))
482+
% build full indices
477483
nddata = length(dims);
478484
indices = repmat({':'}, 1, nddata);
479485
indices{dimpos(1)} = idxkey(oplen).subs{1};
486+
% perform assignment
480487
subsargs = struct('type', '()', 'subs', {indices});
481488
if (oplen > 2)
489+
% need to assign back through the chain
482490
subidx = idxkey(1:oplen - 2);
483491
tempdata = subsref(obj.data, subidx);
484492
tempdata = subsasgn(tempdata, subsargs, otherobj);
@@ -495,6 +503,7 @@
495503
% Fast path: single-level assignment like jd.key = value
496504
if (oplen == 1 && strcmp(idxkey(1).type, '.') && ischar(idxkey(1).subs))
497505
fieldname = idxkey(1).subs;
506+
% Skip if JSONPath
498507
if (isempty(fieldname) || fieldname(1) ~= char(36))
499508
if (isempty(obj.data))
500509
obj.data = struct();
@@ -504,6 +513,7 @@
504513
obj.data.(fieldname) = otherobj;
505514
return
506515
catch
516+
% Field name invalid for struct, convert to Map
507517
fnames = fieldnames(obj.data);
508518
if (~isempty(fnames))
509519
obj.data = containers.Map(fnames, struct2cell(obj.data), 'UniformValues', 0);
@@ -594,6 +604,7 @@
594604
continue
595605
end
596606
if (ischar(idx.subs) && ~(~isempty(idx.subs) && idx.subs(1) == char(36)))
607+
% Handle empty or non-struct/map data
597608
if isempty(opcell{i}) || (~isstruct(opcell{i}) && ~ismap_(obj.flags__, opcell{i}))
598609
opcell{i} = obj.newkey_();
599610
end

0 commit comments

Comments
 (0)