Skip to content

Commit 9f2bc71

Browse files
committed
OpParser: make authors tag more flexible
1 parent 2d11d92 commit 9f2bc71

3 files changed

Lines changed: 28 additions & 9 deletions

File tree

scijava-ops-ext-parser/src/main/java/org/scijava/ops/parser/OpParser.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ public static String parseOpDocument(String inputYamlPath)
127127
version = (String) opsYaml.remove(VERSION_KEY);
128128
}
129129
if (opsYaml.containsKey(AUTHOR_KEY)) {
130-
authors = (List<String>) opsYaml.remove(AUTHOR_KEY);
130+
authors = getListHelper(AUTHOR_KEY, opsYaml);
131+
opsYaml.remove(AUTHOR_KEY);
131132
}
132133

133134
// We assume the remaining entries are nested maps of classes to (maps of
@@ -155,17 +156,19 @@ public static String parseOpDocument(String inputYamlPath)
155156
if (opMetadata.containsKey(ALIAS_KEY)) {
156157
Object alias = opMetadata.get(ALIAS_KEY);
157158
if (alias instanceof String) {
158-
opNames.add((String)alias);
159-
} else if (alias instanceof List) {
160-
opNames.addAll((List<String>)alias);
159+
opNames.add((String) alias);
161160
}
162-
} else {
161+
else if (alias instanceof List) {
162+
opNames.addAll((List<String>) alias);
163+
}
164+
}
165+
else {
163166
opNames.add("ext." + methodName);
164167
}
165168

166169
List<String> opAuthors = authors;
167170
if (opMetadata.containsKey(AUTHOR_KEY)) {
168-
opAuthors = (List<String>) opMetadata.get(AUTHOR_KEY);
171+
opAuthors = getListHelper(AUTHOR_KEY, opMetadata);
169172
}
170173

171174
// If a global namespace is specified, we also alias the Op by its
@@ -197,6 +200,22 @@ public static String parseOpDocument(String inputYamlPath)
197200
return new Yaml().dump(data);
198201
}
199202

203+
/**
204+
* Helper method to extract a key from a map that may point to a single
205+
* {@link String}, or a {@link List} thereof.
206+
*/
207+
private static List<String> getListHelper(String key,
208+
Map<String, Object> map)
209+
{
210+
Object value = map.get(key);
211+
if (value instanceof List) {
212+
return (List<String>) value;
213+
}
214+
List<String> result = new ArrayList<>();
215+
result.add((String) value);
216+
return result;
217+
}
218+
200219
/**
201220
* Helper method to generate a properly formatted "source" string
202221
*

scijava-ops-ext-parser/src/test/java/org/scijava/ops/parser/TestOpParser.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ public void validateParsing() throws ClassNotFoundException {
4949

5050
String expected = //
5151
"- op:" //
52-
+ "\n names: [arrays.toStringDeep, fun.stringMaker, test.deepToString]" //
52+
+
53+
"\n names: [arrays.toStringDeep, fun.stringMaker, test.deepToString]" //
5354
+ "\n description: ''" //
5455
+
5556
"\n source: javaMethod:/java.util.Arrays.deepToString%28%5BLjava.lang.Object%3B%29" //

scijava-ops-ext-parser/src/test/resources/test-ops.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ java.lang.System:
1414
arraycopy:
1515
priority: "50"
1616
description: "a useful op"
17-
authors:
18-
- "You-know-who"
17+
authors: "You-know-who"
1918
type: "Computer2"
2019

0 commit comments

Comments
 (0)