Skip to content

Commit 28479f3

Browse files
committed
Merge branch 'enums-in-scripts'
This restores the ability to refer to enum values directly, without quotes, from script parameter attributes. E.g.: // @string(visibility=MESSAGE) text See also: * #199 * scijava/parsington#4
2 parents 01c68db + 632d975 commit 28479f3

5 files changed

Lines changed: 46 additions & 19 deletions

File tree

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
<dependency>
107107
<groupId>org.scijava</groupId>
108108
<artifactId>scijava-expression-parser</artifactId>
109+
<version>3.1.0</version>
109110
</dependency>
110111

111112
<!-- Third-party dependencies -->

src/main/java/org/scijava/parse/DefaultParseService.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ public class DefaultParseService extends AbstractService implements
5555

5656
@Override
5757
public Items parse(final String arg) {
58-
return new ItemsList(arg);
58+
return parse(arg, true);
59+
}
60+
61+
@Override
62+
public Items parse(final String arg, final boolean strict) {
63+
return new ItemsList(arg, strict);
5964
}
6065

6166
// -- Helper classes --
@@ -67,9 +72,9 @@ public Items parse(final String arg) {
6772
*/
6873
private static class ItemsList extends ObjectArray<Item> implements Items {
6974

70-
public ItemsList(final String arg) {
75+
public ItemsList(final String arg, final boolean strict) {
7176
super(Item.class);
72-
parseItems(arg);
77+
parseItems(arg, strict);
7378
}
7479

7580
@Override
@@ -98,8 +103,9 @@ public boolean isList() {
98103
return true;
99104
}
100105

101-
private void parseItems(final String arg) {
106+
private void parseItems(final String arg, final boolean strict) {
102107
final DefaultEvaluator e = new DefaultEvaluator();
108+
e.setStrict(strict);
103109
final Object result = e.evaluate("(" + arg + ")");
104110
if (result == null) {
105111
throw new IllegalStateException("Error parsing string: '" + arg + "'");

src/main/java/org/scijava/parse/ParseService.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,19 @@ public interface ParseService extends SciJavaService {
5454
*/
5555
Items parse(String arg);
5656

57+
/**
58+
* Parses a comma-delimited list of data elements.
59+
* <p>
60+
* Some data elements might be {@code key=value} pairs, while others might be
61+
* raw values (i.e., no equals sign).
62+
* </p>
63+
*
64+
* @param arg The string to parse.
65+
* @param strict Whether to fail fast when encountering an unassigned variable
66+
* token.
67+
* @return A parsed list of {@link Item}s.
68+
* @throws IllegalArgumentException If the string does not conform to expected
69+
* syntax.
70+
*/
71+
Items parse(String arg, boolean strict);
5772
}

src/main/java/org/scijava/script/ScriptInfo.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,16 @@ public ScriptInfo(final Context context, final String path,
136136
setContext(context);
137137
this.path = path;
138138

139-
String script = null;
139+
String contents = null;
140140
if (reader != null) {
141141
try {
142-
script = getReaderContentsAsString(reader);
142+
contents = getReaderContentsAsString(reader);
143143
}
144144
catch (final IOException exc) {
145145
log.error("Error reading script: " + path, exc);
146146
}
147147
}
148-
this.script = script;
148+
script = contents;
149149
}
150150

151151
// -- ScriptInfo methods --
@@ -231,14 +231,9 @@ public void parseParameters() {
231231
clearParameters();
232232
appendReturnValue = true;
233233

234-
try {
235-
final BufferedReader in;
236-
if (script == null) {
237-
in = new BufferedReader(new FileReader(getPath()));
238-
}
239-
else {
240-
in = getReader();
241-
}
234+
try (final BufferedReader in = script == null ? //
235+
new BufferedReader(new FileReader(getPath())) : getReader()) //
236+
{
242237
while (true) {
243238
final String line = in.readLine();
244239
if (line == null) break;
@@ -252,7 +247,6 @@ public void parseParameters() {
252247
}
253248
else if (line.matches(".*\\w.*")) break;
254249
}
255-
in.close();
256250

257251
if (appendReturnValue) addReturnValue();
258252
}
@@ -384,7 +378,7 @@ private void parseParam(final String param,
384378

385379
/** Parses a comma-delimited list of {@code key=value} pairs into a map. */
386380
private Map<String, Object> parseAttrs(final String attrs) {
387-
return parser.parse(attrs).asMap();
381+
return parser.parse(attrs, false).asMap();
388382
}
389383

390384
private boolean isIOType(final String token) {
@@ -456,7 +450,13 @@ private boolean is(final String key, final String desired) {
456450

457451
/** Super terse conversion helper method. */
458452
private <T> T as(final Object v, final Class<T> type) {
459-
return convertService.convert(v, type);
453+
final T converted = convertService.convert(v, type);
454+
if (converted != null) return converted;
455+
// NB: Attempt to convert via string.
456+
// This is useful in cases where a weird type of object came back
457+
// (e.g., org.scijava.sjep.eval.Unresolved), but which happens to have a
458+
// nice string representation which ultimately is expressible as the type.
459+
return convertService.convert(v.toString(), type);
460460
}
461461

462462
private <T> List<T> asList(final Object v, final Class<T> type) {

src/test/java/org/scijava/script/ScriptInfoTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import org.junit.Test;
5959
import org.scijava.Context;
6060
import org.scijava.ItemIO;
61+
import org.scijava.ItemVisibility;
6162
import org.scijava.log.LogService;
6263
import org.scijava.module.ModuleItem;
6364
import org.scijava.plugin.Plugin;
@@ -181,6 +182,7 @@ public void testParameters() {
181182
"stepSize=3, value=11, style=\"slider\") sliderValue\n" + //
182183
"% @String(persist = false, family='Carnivora', " + //
183184
"choices={'quick brown fox', 'lazy dog'}) animal\n" + //
185+
"% @String(visibility=MESSAGE) msg\n" + //
184186
"% @BOTH java.lang.StringBuilder buffer";
185187

186188
final ScriptInfo info =
@@ -203,12 +205,15 @@ public void testParameters() {
203205
null, null, null, null, null, null, null, null, animalChoices, animal);
204206
assertEquals(animal.get("family"), "Carnivora"); // test custom attribute
205207

208+
final ModuleItem<?> msg = info.getInput("msg");
209+
assertSame(ItemVisibility.MESSAGE, msg.getVisibility());
210+
206211
final ModuleItem<?> buffer = info.getOutput("buffer");
207212
assertItem("buffer", StringBuilder.class, null, ItemIO.BOTH, true, true,
208213
null, null, null, null, null, null, null, null, noChoices, buffer);
209214

210215
int inputCount = 0;
211-
final ModuleItem<?>[] inputs = { log, sliderValue, animal, buffer };
216+
final ModuleItem<?>[] inputs = { log, sliderValue, animal, msg, buffer };
212217
for (final ModuleItem<?> inItem : info.inputs()) {
213218
assertSame(inputs[inputCount++], inItem);
214219
}

0 commit comments

Comments
 (0)