-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* First attempt to incorporate json into bids.File * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * lint * Added util function to update structures * Style and tests for util/update_struct.m * Using metadata field instead of json_struct * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Removed JSONFile subclass * Added tests for metadata for File class * Fixed spelling in doc * File: Added functions that act on individual fields * File: fixed style * Splitted File.metadada tests into smaller ones --------- Co-authored-by: Beliy Nikita <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Remi Gau <[email protected]>
- Loading branch information
1 parent
10baa5d
commit 43eb70b
Showing
4 changed files
with
344 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
function js = update_struct(js, varargin) | ||
% | ||
% Updates structure with new values. | ||
% Can add new fields, replace field values, remove fields, | ||
% and append new values to a cellarray. | ||
% | ||
% Designed for manipulating json structures and will not work | ||
% on structarrays. | ||
% | ||
% USAGE:: | ||
% | ||
% js = update_struct(key1, value1, key2, value2); | ||
% js = update_struct(struct(key1, value1, ... | ||
% key2, value2)); | ||
% | ||
% Examples: | ||
% --------- | ||
% Adding and replacing existing fields: | ||
% update_struct(struct('a', 'val_a'),... | ||
% 'a', 'new_val', 'b', 'val_b') | ||
% struct with fields: | ||
% a: 'new_val' | ||
% b: 'val_b' | ||
% Removing field from structure: | ||
% update_struct(struct('a', 'val_a', 'b', 'val_b'), | ||
% 'b', []) | ||
% struct with fields: | ||
% a: 'val_a' | ||
% Appending values to existing field: | ||
% update_struct(struct('a', 'val_a', 'b', 'val_b'), | ||
% 'b-add', 'val_b2') | ||
% struct with fields: | ||
% a: 'val_a' | ||
% b: {'val_b'; 'val_b2'} | ||
% | ||
|
||
% (C) Copyright 2023 BIDS-MATLAB developers | ||
|
||
if numel(varargin) == 0 | ||
% Nothing to do | ||
return | ||
end | ||
|
||
if numel(varargin) > 1 | ||
key_list = varargin(1:2:end); | ||
val_list = varargin(2:2:end); | ||
elseif isstruct(varargin{1}) | ||
key_list = fieldnames(varargin{1}); | ||
val_list = cell(size(key_list)); | ||
for i = 1:numel(key_list) | ||
val_list{i} = varargin{1}.(key_list{i}); | ||
end | ||
else | ||
id = bids.internal.camel_case('invalidInput'); | ||
msg = 'Not list of parameters or structure'; | ||
bids.internal.error_handling(mfilename(), id, msg, false, true); | ||
end | ||
|
||
for ii = 1:numel(key_list) | ||
par_key = key_list{ii}; | ||
try | ||
par_value = val_list{ii}; | ||
|
||
% Removing field from json structure | ||
% Should use only empty double ([]) or any empth object? | ||
if isempty(par_value) && isnumeric(par_value) | ||
if isfield(js, par_key) | ||
js = rmfield(js, par_key); | ||
end | ||
continue | ||
end | ||
|
||
if bids.internal.ends_with(par_key, '-add') | ||
par_key = par_key(1:end - 4); | ||
if isfield(js, par_key) | ||
if ischar(js.(par_key)) | ||
par_value = {js.(par_key); par_value}; %#ok<AGROW> | ||
else | ||
par_value = [js.(par_key); par_value]; %#ok<AGROW> | ||
end | ||
end | ||
end | ||
js(1).(par_key) = par_value; | ||
|
||
catch ME | ||
id = bids.internal.camel_case('structError'); | ||
msg = sprintf('''%s'' (%d) -- %s', par_key, ii, ME.message); | ||
bids.internal.error_handling(mfilename(), id, msg, false, true); | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
function test_suite = test_update_struct %#ok<*STOUT> | ||
try % assignment of 'localfunctions' is necessary in Matlab >= 2016 | ||
test_functions = localfunctions(); %#ok<*NASGU> | ||
catch % no problem; early Matlab versions can use initTestSuite fine | ||
end | ||
initTestSuite; | ||
|
||
end | ||
|
||
function test_main_func() | ||
|
||
% testing with parameters | ||
js = struct([]); | ||
js = bids.util.update_struct(js, 'key_a', 'val_a', 'key_b', 'val_b'); | ||
assertTrue(isfield(js, 'key_a')); | ||
assertTrue(isfield(js, 'key_b')); | ||
assertEqual(js.key_a, 'val_a'); | ||
assertEqual(js.key_b, 'val_b'); | ||
|
||
% testing with struct | ||
test_struct.key_c = 'val_c'; | ||
|
||
js = bids.util.update_struct(js, test_struct); | ||
assertTrue(isfield(js, 'key_c')); | ||
assertEqual(js.key_c, 'val_c'); | ||
|
||
% testing update and removal of field | ||
js = bids.util.update_struct(js, 'key_c', [], 'key_a', 'val_a2'); | ||
assertFalse(isfield(js, 'key_c')); | ||
assertEqual(js.key_a, 'val_a2'); | ||
|
||
% testing concatenating as string cell | ||
js = bids.util.update_struct(js, 'key_b-add', 'val_b2'); | ||
assertEqual(js.key_b, {'val_b'; 'val_b2'}); | ||
|
||
% testing concatenating numericals | ||
js = bids.util.update_struct(js, 'key_b-add', 3); | ||
assertEqual(js.key_b, {'val_b'; 'val_b2'; 3}); | ||
end | ||
|
||
function test_exceptions() | ||
% Invalid input | ||
assertExceptionThrown(@() bids.util.update_struct(struct([]), 'key_b-add'), ... | ||
'update_struct:invalidInput'); | ||
assertExceptionThrown(@() bids.util.update_struct(struct([]), ... | ||
'key_b-add', [], ... | ||
'key_c'), ... | ||
'update_struct:structError'); | ||
end |