Skip to content

Commit

Permalink
Fix +matnwb/+utility/isNeurodataTypeClassName.m
Browse files Browse the repository at this point in the history
Make check more robust
  • Loading branch information
ehennestad committed Dec 12, 2024
1 parent 839720d commit 153fa2d
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions +matnwb/+utility/isNeurodataTypeClassName.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,51 @@
arguments
typeName (1,1) string
end

tf = false;
if startsWith(typeName, 'types.') && ~startsWith(typeName, 'types.untyped')
tf = true;
mc = meta.class.fromName(typeName);
if ~isempty(mc)
tf = hasSuperClass(mc, 'types.untyped.MetaClass');
end
end
end

function tf = hasSuperClass(mc, superClassName)
% hasSuperClass - Recursively check if a meta.class object has a specific superclass.
%
% tf = hasSuperClass(mc, superClassName) returns true if the meta.class object
% mc has a superclass with the name superClassName, either directly or
% indirectly (through its own superclasses).
%
% Arguments:
% mc - A meta.class object.
% superClassName - The name of the superclass to check for (string).
%
% Returns:
% tf - Logical value indicating if the class has the specified superclass.

arguments
mc meta.class
superClassName (1,1) string
end

% Check if the current class has the desired superclass directly.
for i = 1:numel(mc.SuperclassList)
if mc.SuperclassList(i).Name == superClassName
tf = true;
return;
end
end

% If not, check recursively through each superclass.
for i = 1:numel(mc.SuperclassList)
if hasSuperClass(mc.SuperclassList(i), superClassName)
tf = true;
return;
end
end

% If no match found, return false.
tf = false;

Check warning on line 57 in +matnwb/+utility/isNeurodataTypeClassName.m

View check run for this annotation

Codecov / codecov/patch

+matnwb/+utility/isNeurodataTypeClassName.m#L57

Added line #L57 was not covered by tests
end

0 comments on commit 153fa2d

Please sign in to comment.