Fix bug around empty-stirng checks for enumvalueparsing

This commit is contained in:
Kris De Volder
2017-07-28 16:58:17 -07:00
parent 49f33fb8f7
commit 6bdc83217d
4 changed files with 45 additions and 14 deletions

View File

@@ -1591,4 +1591,21 @@ public class BoshEditorTest {
);
}
@Test public void bug_149769913() throws Exception {
Editor editor = harness.newEditor(
"releases:\n" +
"- name: learn-bosh\n" +
" url: file:///blah\n" +
" version:\n" +
"- name: blah-blah\n" +
" version:"
);
editor.ignoreProblem(YamlSchemaProblems.MISSING_PROPERTY);
editor.assertProblems(
"version:^^|cannot be blank",
"version:^^|cannot be blank"
);
}
}

View File

@@ -53,6 +53,10 @@ public class EnumValueParser implements ValueParser {
this.values = values;
}
public EnumValueParser(String name, PartialCollection<String> values) {
this(name, () -> values);
}
@Override
public Object parse(String str) throws Exception {
// IMPORTANT: check the text FIRST before fetching values

View File

@@ -68,6 +68,20 @@ public class PartialCollection<T> {
return new PartialCollection<>(ImmutableSet.of(), e);
}
}
/**
* Create a {@link PartialCollection} by executing some computation that returs a collectioon.
* If the computation throws the resulting collection will be completely unknown, otherwise
* it will be completely known.
*/
public static <T> PartialCollection<T> fromCallable(Callable<PartialCollection<T>> computer) {
try {
return computer.call();
} catch (Exception e) {
return new PartialCollection<>(ImmutableSet.of(), e);
}
}
/**
* @return All the known elements of this partial collection.

View File

@@ -859,21 +859,17 @@ public class YTypeFactory {
YAtomicType t = yatomic(name);
t.setHintProvider(values);
t.parseWith((DynamicSchemaContext dc) -> {
PartialCollection<YValueHint> hints = values.withContext(dc);
if (hints.isComplete()) {
Collection<String> strings = values(hints.getElements());
return new EnumValueParser(name, strings) {
@Override
protected String createErrorMessage(String parseString, Collection<String> values) {
try {
return errorMessageFormatter.withContext(dc).apply(parseString, values);
} catch (Exception e) {
return super.createErrorMessage(parseString, values);
}
PartialCollection<YValueHint> hints = PartialCollection.fromCallable(() -> values.withContext(dc));
return new EnumValueParser(name, hints.map(h -> h.getValue())) {
@Override
protected String createErrorMessage(String parseString, Collection<String> values) {
try {
return errorMessageFormatter.withContext(dc).apply(parseString, values);
} catch (Exception e) {
return super.createErrorMessage(parseString, values);
}
};
}
return null;
}
};
});
return t;
}