Skip to content

Commit

Permalink
Retain null-safe syntax in AST representation of selection & projection
Browse files Browse the repository at this point in the history
Prior to this commit, SpEL's CompoundExpression omitted the null-safe
syntax in AST string representations of the selection and projection
operators.

To address this, this commit implements isNullSafe() in Projection and
Selection.

Closes spring-projectsgh-32515
  • Loading branch information
sbrannen committed Mar 22, 2024
1 parent f941754 commit 52b8c71
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ public Projection(boolean nullSafe, int startPos, int endPos, SpelNodeImpl expre
}


/**
* Does this node represent a null-safe projection operation?
* @since 6.1.6
*/
@Override
public final boolean isNullSafe() {
return this.nullSafe;
}

@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
return getValueRef(state).getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ public Selection(boolean nullSafe, int variant, int startPos, int endPos, SpelNo
}


/**
* Does this node represent a null-safe selection operation?
* @since 6.1.6
*/
@Override
public final boolean isNullSafe() {
return this.nullSafe;
}

@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
return getValueRef(state).getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,24 @@ class Miscellaneous {

@Test
void compoundExpressions() {
parseCheck("#var1.methodOne().methodTwo(42)");
parseCheck("#func1().methodOne().methodTwo(42)");
parseCheck("#func2('enigma').methodOne().methodTwo(42)");
parseCheck("property1.property2.methodOne()");
parseCheck("property1[0].property2['key'].methodOne()");
parseCheck("property1.methodOne('enigma').methodTwo(42)");
parseCheck("property1.methodOne().property2.methodTwo()");
parseCheck("property1[0].property2['key'].methodTwo()");
parseCheck("property1[0][1].property2['key'][42].methodTwo()");

// null-safe variants
parseCheck("#var1?.methodOne()?.methodTwo(42)");
parseCheck("#func1()?.methodOne()?.methodTwo(42)");
parseCheck("#func2('enigma')?.methodOne()?.methodTwo(42)");
parseCheck("property1?.property2?.methodOne()");
parseCheck("property1?.methodOne('enigma')?.methodTwo(42)");
parseCheck("property1?.methodOne()?.property2?.methodTwo()");
parseCheck("property1[0]?.property2['key']?.methodTwo()");
parseCheck("property1[0][1]?.property2['key'][42]?.methodTwo()");
}

@Test
Expand Down Expand Up @@ -132,25 +147,34 @@ void indexing() {

@Test
void projection() {
parseCheck("{1,2,3,4,5,6,7,8,9,10}.![#isEven()]");
parseCheck("{1,2,3}.![#isEven()]");

// null-safe variant
parseCheck("{1,2,3}?.![#isEven()]");
}

@Test
void selection() {
parseCheck("{1,2,3,4,5,6,7,8,9,10}.?[#isEven(#this) == 'y']",
"{1,2,3,4,5,6,7,8,9,10}.?[(#isEven(#this) == 'y')]");
parseCheck("{1,2,3}.?[#isEven(#this)]");

// null-safe variant
parseCheck("{1,2,3}?.?[#isEven(#this)]");
}

@Test
void selectionFirst() {
parseCheck("{1,2,3,4,5,6,7,8,9,10}.^[#isEven(#this) == 'y']",
"{1,2,3,4,5,6,7,8,9,10}.^[(#isEven(#this) == 'y')]");
void selectFirst() {
parseCheck("{1,2,3}.^[#isEven(#this)]");

// null-safe variant
parseCheck("{1,2,3}?.^[#isEven(#this)]");
}

@Test
void selectionLast() {
parseCheck("{1,2,3,4,5,6,7,8,9,10}.$[#isEven(#this) == 'y']",
"{1,2,3,4,5,6,7,8,9,10}.$[(#isEven(#this) == 'y')]");
void selectLast() {
parseCheck("{1,2,3}.$[#isEven(#this)]");

// null-safe variant
parseCheck("{1,2,3}?.$[#isEven(#this)]");
}
}

Expand Down

0 comments on commit 52b8c71

Please sign in to comment.