Skip to content

Commit

Permalink
Allow createdOutputType on unverified expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
bratseth committed Nov 6, 2024
1 parent 24c9d37 commit ebaf7e4
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,17 @@ public ExpressionConverter branch() {
}

@Override
protected boolean shouldConvert(Expression exp) {
if (exp instanceof OutputExpression) {
String fieldName = ((OutputExpression)exp).getFieldName();
protected boolean shouldConvert(Expression expression) {
if (expression instanceof OutputExpression) {
String fieldName = ((OutputExpression)expression).getFieldName();
if (outputs.contains(fieldName) && !prevNames.contains(fieldName)) {
throw new VerificationException(exp, "Attempting to assign conflicting values to field '" +
fieldName + "'");
throw new VerificationException(expression, "Attempting to assign conflicting values to field '" +
fieldName + "'");
}
outputs.add(fieldName);
prevNames.add(fieldName);
}
if (exp.createdOutputType() != null) {
if (expression.createdOutputType() != null) {
prevNames.clear();
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ private DataType assignInputType(DataType inputType) {
*/
public DataType getOutputType(VerificationContext context) { return outputType; }

/** Returns the already assigned (during verification) output type, or null if no type is assigned. */
public DataType getOutputType() { return outputType; }

/** Returns the already assigned (during verification) output type, or throws an exception if no type is assigned. */
public DataType requireOutputType() {
if (outputType == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ private boolean isHashCompatible(DataType type) {
}

@Override
public DataType createdOutputType() { return requireOutputType(); }
public DataType createdOutputType() {
return getOutputType();
}

@Override
public String toString() { return "hash"; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,11 @@ public void testIntHash() throws ParseException {
var intField = new Field("myInt", DataType.INT);
adapter.createField(intField);
adapter.setValue("myText", new StringFieldValue("input text"));
expression.setStatementOutput(new DocumentType("myDocument"), intField);

// Necessary to resolve output type
VerificationContext verificationContext = new VerificationContext(adapter);
assertEquals(DataType.INT, expression.verify(verificationContext));

ExecutionContext context = new ExecutionContext(adapter);
context.setCurrentValue(new StringFieldValue("input text"));
expression.execute(context);
assertTrue(adapter.values.containsKey("myInt"));
assertEquals(-1425622096, adapter.values.get("myInt").getWrappedValue());
Expand All @@ -129,14 +126,11 @@ public void testIntArrayHash() throws ParseException {
array.add(new StringFieldValue("first"));
array.add(new StringFieldValue("second"));
adapter.setValue("myTextArray", array);
expression.setStatementOutput(new DocumentType("myDocument"), intField);

// Necessary to resolve output type
VerificationContext verificationContext = new VerificationContext(adapter);
assertEquals(new ArrayDataType(DataType.INT), expression.verify(verificationContext));

ExecutionContext context = new ExecutionContext(adapter);
context.setCurrentValue(array);
expression.execute(context);
assertTrue(adapter.values.containsKey("myIntArray"));
var intArray = (Array<IntegerFieldValue>)adapter.values.get("myIntArray");
Expand All @@ -153,14 +147,11 @@ public void testLongHash() throws ParseException {
var intField = new Field("myLong", DataType.LONG);
adapter.createField(intField);
adapter.setValue("myText", new StringFieldValue("input text"));
expression.setStatementOutput(new DocumentType("myDocument"), intField);

// Necessary to resolve output type
VerificationContext verificationContext = new VerificationContext(adapter);
assertEquals(DataType.LONG, expression.verify(verificationContext));

ExecutionContext context = new ExecutionContext(adapter);
context.setCurrentValue(new StringFieldValue("input text"));
expression.execute(context);
assertTrue(adapter.values.containsKey("myLong"));
assertEquals(7678158186624760752L, adapter.values.get("myLong").getWrappedValue());
Expand All @@ -179,14 +170,11 @@ public void testZCurveArray() throws ParseException {
array.add(new StringFieldValue("30;40"));
array.add(new StringFieldValue("50;60"));
adapter.setValue("location_str", array);
expression.setStatementOutput(new DocumentType("myDocument"), zcurveField);

// Necessary to resolve output type
VerificationContext verificationContext = new VerificationContext(adapter);
assertEquals(DataType.getArray(DataType.LONG), expression.verify(verificationContext));

ExecutionContext context = new ExecutionContext(adapter);
context.setCurrentValue(array);
expression.execute(context);
assertTrue(adapter.values.containsKey("location_zcurve"));
var longArray = (Array<LongFieldValue>)adapter.values.get("location_zcurve");
Expand Down

0 comments on commit ebaf7e4

Please sign in to comment.