11function matlabType = mapType(dtype )
2- % MAPTYPE
3- % converts dtype name to type name. If struct, then returns a struct of mapped types
4- % all this does is narrow the possible range of names per type.
2+ % mapType - Map an HDMF/NWB dtype descriptor to a MATLAB type descriptor
3+ %
4+ % This function normalizes the value of a schema ``dtype`` field into the
5+ % MATLAB type representation used by MatNWB code generation.
6+ %
7+ % Input:
8+ % dtype - Schema dtype descriptor. Supported forms are:
9+ % * character vector describing basic dtype, e.g. 'int', 'float32', 'text'
10+ % * cell array describing a compound dtype
11+ % * containers.Map describing a reference dtype
12+ %
13+ % Output:
14+ % matlabType - MATLAB type descriptor corresponding to ``dtype``:
15+ % * character vector for basic dtypes, e.g. 'int32', 'single', 'char'
16+ % * struct for compound dtypes, with one field per compound member
17+ % * containers.Map for reference dtypes
18+ %
19+ % Special cases:
20+ % * empty, 'None', and 'any' map to 'any'
21+ %
22+ % Raises an error if ``dtype`` is not a supported schema dtype.
523
6- if isempty(dtype ) || (ischar(dtype ) && any(strcmpi({' None' , ' any' }, dtype )))
7- matlabType = ' any' ;
8- return ;
9- end
10-
11- if iscell(dtype )
12- % compound type
13- matlabType = struct();
14- numTypes = length(dtype );
15- for i= 1 : numTypes
16- typeMap = dtype{i };
17- typeName = typeMap(' name' );
18- type = file .mapType(typeMap(' dtype' ));
19- matlabType.(typeName ) = type ;
24+ persistent basicTypeMap
25+ if isempty(basicTypeMap )
26+ basicTypeMap = spec .getBasicDTypeMap();
2027 end
21- return ;
22- end
2328
24- if isa(dtype , ' containers.Map' )
25- matlabType = dtype ;
26- return ;
27- end
29+ if ischar(dtype ) % Basic dtype
30+ if isempty(dtype ) || any(strcmpi({' None' , ' any' }, dtype ))
31+ matlabType = ' any' ;
32+ else
33+ try
34+ matlabType = basicTypeMap(dtype );
35+ matlabType = char(matlabType );
36+ catch
37+ error(' NWB:MapType:UnsupportedDtype' , ...
38+ [' Schema attribute `dtype` returned an unsupported value "%s ". ' ...
39+ ' If this value is a supported dtype according to the HDMF/NWB ' ...
40+ ' specification language, please raise an issue on the MatNWB ' ...
41+ ' GitHub repository' ], dtype )
42+ end
43+ end
2844
29- assert(ischar(dtype ), ' NWB:MapType:InvalidDtype' , ...
30- ' schema attribute `dtype` returned in unsupported type `%s `' , class(dtype ));
31-
32- switch dtype
33- case {' text' , ' utf' , ' utf8' , ' utf-8' , ' ascii' , ' bytes' }
34- matlabType = ' char' ;
35- case ' bool'
36- matlabType = ' logical' ;
37- case ' isodatetime'
38- matlabType = ' datetime' ;
39- case {' float' , ' float32' }
40- matlabType = ' single' ;
41- case ' float64'
42- matlabType = ' double' ;
43- case ' long'
44- matlabType = ' int64' ;
45- case ' int'
46- matlabType = ' int8' ;
47- case ' short'
48- matlabType = ' int16' ;
49- otherwise
45+ elseif iscell(dtype ) % Compound dtype
46+ matlabType = struct();
47+ numTypes = numel(dtype );
48+ for i = 1 : numTypes
49+ typeMap = dtype{i };
50+ typeName = typeMap(' name' );
51+ type = file .mapType(typeMap(' dtype' ));
52+ matlabType.(typeName ) = type ;
53+ end
54+
55+ elseif isa(dtype , ' containers.Map' ) % Reference dtype
5056 matlabType = dtype ;
51- end
57+
58+ elseif isempty(dtype )
59+ matlabType = ' any' ;
60+
61+ else
62+ error(...
63+ ' NWB:MapType:InvalidDtype' , ...
64+ [' Schema `dtype` specification key must be a character vector, ' , ...
65+ ' cell array, or containers.Map; got %s .' ], class(dtype ))
66+ end
67+ end
0 commit comments