Skip to content

Commit

Permalink
[bug] fix octave adding new element bug
Browse files Browse the repository at this point in the history
two bugs in octave preventing subsref and subsasgn for handling indices have been submitted in
https://savannah.gnu.org/bugs/?66271
https://savannah.gnu.org/bugs/?66270
here I use a workaround that works on both matlab and octave
  • Loading branch information
fangq committed Sep 28, 2024
1 parent afce1de commit ade1919
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
23 changes: 19 additions & 4 deletions jdict.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
% jd = jdict(obj);
%
% % getting values
% jd() % return obj
% jd.('key1').('subkey1') % return jdict(1)
% jd.keys.subkey1 % return jdict(1)
% jd.('key1').('subkey3') % return jdict(obj.key1.subkey3)
Expand Down Expand Up @@ -71,6 +72,11 @@
% jd.Atlas_Age_19_0.('Landmark_10_10').('$.._DataLink_')
% jd.Atlas_Age_19_0.Landmark_10_10.('$.._DataLink_')()
%
% % creating and managing hierachical data with any key value
% jd = jdict;
% jd.('_DataInfo_') = struct('toolbox', 'jsonlab', 'version', '3.0.0')
% jd.('_DataInfo_').tojson()
%
% license:
% BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details
%
Expand Down Expand Up @@ -187,16 +193,25 @@
if (ischar(idx.subs))
if (((isa(opcell{i}, 'containers.Map') || isa(opcell{i}, 'dictionary')) && ~isKey(opcell{i}, idx.subs)))
idx.type = '()';
opcell{i} = subsasgn(opcell{i}, idx, containers.Map());
opcell{i}(idx.subs) = containers.Map();
elseif (isstruct(opcell{i}) && ~isfield(opcell{i}, idx.subs))
opcell{i} = subsasgn(opcell{i}, idx, containers.Map());
opcell{i}.(idx.subs) = containers.Map();
end
end
end
opcell{i + 1} = subsref(opcell{i}, idx);
if (exist('OCTAVE_VERSION', 'builtin') ~= 0) && (isa(opcell{i}, 'containers.Map') || isa(opcell{i}, 'dictionary'))
opcell{i + 1} = opcell{i}(idx.subs);
else
opcell{i + 1} = subsref(opcell{i}, idx);
end
end

opcell{end - 1} = subsasgn(opcell{i}, idx, otherobj);
if (exist('OCTAVE_VERSION', 'builtin') ~= 0) && (isa(opcell{i}, 'containers.Map') || isa(opcell{i}, 'dictionary'))
opcell{i}(idx.subs) = otherobj;
opcell{end - 1} = opcell{i};
else
opcell{end - 1} = subsasgn(opcell{i}, idx, otherobj);
end

for i = oplen - 1:-1:1
idx = idxkey(i);
Expand Down
4 changes: 2 additions & 2 deletions test/run_jsonlab_test.m
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,8 @@ function run_jsonlab_test(tests)
jd.('key1').('subkey2').v([2, 3]) = [10, 11];
jd.('key1').('subkey3').v(2) = 'mod';
jd.('key1').('subkey3').v(3).('subsubkey1') = 1;
% jd.('key1').('subkey3').v(3).('subsubkey2') = 'new';
test_jsonlab('jd.(''key1'').(''subkey3'')', @savejson, jd.('key1').('subkey3'), '[8,"mod",{"subsubkey1":1}]', 'compact', 1);
jd.('key1').('subkey3').v(3).('subsubkey2') = 'new';
test_jsonlab('jd.(''key1'').(''subkey3'')', @savejson, jd.('key1').('subkey3'), '[8,"mod",{"subsubkey1":1,"subsubkey2":"new"}]', 'compact', 1);
test_jsonlab('jd.(''key1'').(''subkey2'')', @savejson, jd.('key1').('subkey2'), '[2,10,11]', 'compact', 1);

clear testdata jd;
Expand Down

0 comments on commit ade1919

Please sign in to comment.