Skip to content

Commit

Permalink
Expression NO_$$ mode
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Sep 27, 2024
1 parent f3eefdd commit 6b0bc75
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ private static Node parseString(Itr itr, final boolean allowExpr, final boolean
}
// check to see if it's a dangling $
if (!itr.hasNext()) {
if (flags.contains(Flag.NO_$$)) {
continue;
}
if (!flags.contains(Flag.LENIENT_SYNTAX)) {
// TP 2
throw invalidExpressionSyntax(itr.getStr(), idx);
Expand Down Expand Up @@ -410,6 +413,11 @@ private static Node parseString(Itr itr, final boolean allowExpr, final boolean
if (flags.contains(Flag.MINI_EXPRS)) {
// TP 13
list.add(new ExpressionNode(false, LiteralNode.DOLLAR, Node.NULL));
} else if (flags.contains(Flag.NO_$$)) {
list.add(LiteralNode.DOLLAR);
if (!itr.hasNext() || itr.peekNext() != '{') {
itr.prev();
}
} else {
// just resolve $$ to $
// TP 14
Expand Down Expand Up @@ -485,6 +493,16 @@ private static Node parseString(Itr itr, final boolean allowExpr, final boolean
}
//throw Assert.unreachableCode();
}
case '\\': {
if (flags.contains(Flag.NO_$$)) {
if (itr.hasNext() && itr.peekNext() == '$') {
list.add(LiteralNode.DOLLAR);
start = itr.getNextIdx();
itr.next();
continue;
}
}
}
default: {
// $ followed by anything else
if (flags.contains(Flag.MINI_EXPRS)) {
Expand All @@ -493,7 +511,7 @@ private static Node parseString(Itr itr, final boolean allowExpr, final boolean
Node.NULL));
start = itr.getNextIdx();
continue;
} else if (flags.contains(Flag.LENIENT_SYNTAX)) {
} else if (flags.contains(Flag.LENIENT_SYNTAX) || flags.contains(Flag.NO_$$)) {
// TP 26
// just treat it as literal
start = itr.getPrevIdx() - 1; // we can use 1 here because unicode '$' is one char in size
Expand Down Expand Up @@ -551,7 +569,22 @@ private static Node parseString(Itr itr, final boolean allowExpr, final boolean
//throw Assert.unreachableCode();
}
case '\\': {
if (flags.contains(Flag.ESCAPES)) {
if (flags.contains(Flag.NO_$$)) {
int escape = itr.getNextIdx();
if (itr.hasNext() && itr.peekNext() == '$') {
itr.next();
if (itr.hasNext() && itr.peekNext() == '{') {
list.add(new LiteralNode(itr.getStr(), start, escape - 1));
list.add(new LiteralNode(itr.getStr(), escape, itr.getNextIdx()));
start = itr.getNextIdx();
} else if (itr.hasNext() && itr.peekNext() == '$') {
list.add(new LiteralNode(itr.getStr(), start, escape));
start = itr.getPrevIdx();
itr.prev();
}
}
continue;
} else if (flags.contains(Flag.ESCAPES)) {
if (idx > start) {
list.add(new LiteralNode(itr.getStr(), start, idx));
start = idx;
Expand Down Expand Up @@ -694,6 +727,11 @@ public enum Flag {
* character.
*/
ESCAPES,
/**
* Escaping <code>$</code> with <code>$$</code> or <code>/$</code> only applies when <code>{</code> follows
* the initial escaped <code>$</code>.
*/
NO_$$,
/**
* Treat expressions containing a double-colon delimiter as special, encoding the entire content into the key.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import static io.smallrye.common.expression.Expression.Flag.NO_SMART_BRACES;
import static org.junit.jupiter.api.Assertions.*;

import java.util.EnumSet;

import org.junit.jupiter.api.Test;

import io.smallrye.common.constraint.Assert;
Expand Down Expand Up @@ -693,4 +695,110 @@ void expressions() {
b.append(c.getExpandedDefault());
}));
}

@Test
void no$$() {
doubleDollarExpressions(EnumSet.of(NO_$$));
doubleDollarExpressions(EnumSet.of(NO_$$, ESCAPES));

assertEquals("", Expression.compile("$a", NO_$$, MINI_EXPRS).evaluate((c, b) -> {
assertEquals("a", c.getKey());
}));
assertEquals("a", Expression.compile("$$a", NO_$$, MINI_EXPRS).evaluate((c, b) -> {
assertEquals("$", c.getKey());
}));
}

private void doubleDollarExpressions(EnumSet<Expression.Flag> flags) {
assertEquals("$", Expression.compile("$", flags).evaluate((c, b) -> {
}));
assertEquals("$$", Expression.compile("$$", flags).evaluate((c, b) -> {
}));
assertEquals("\\$", Expression.compile("\\$", flags).evaluate((c, b) -> {
}));
assertEquals("\\$$", Expression.compile("\\$$", flags).evaluate((c, b) -> {
}));
assertEquals("$$foo", Expression.compile("$$foo", flags).evaluate((c, b) -> {
}));
assertEquals("foo$$", Expression.compile("foo$$", flags).evaluate((c, b) -> {
}));
assertEquals("$$foo", Expression.compile("$$foo", flags).evaluate((c, b) -> {
}));
assertEquals("foo$$bar", Expression.compile("foo$$bar", flags).evaluate((c, b) -> {
}));
assertEquals("${foo}", Expression.compile("$${foo}", flags).evaluate((c, b) -> {
}));
assertEquals("$${foo}", Expression.compile("$$${foo}", flags).evaluate((c, b) -> {
}));
assertEquals("$${foo}$", Expression.compile("$$${foo}$", flags).evaluate((c, b) -> {
}));
assertEquals("$${foo}$$", Expression.compile("$$${foo}$$", flags).evaluate((c, b) -> {
}));
assertEquals("foo${bar}", Expression.compile("foo$${bar}", flags).evaluate((c, b) -> {
}));
assertEquals("foo$${bar}", Expression.compile("foo$$${bar}", flags).evaluate((c, b) -> {
}));
assertEquals("foo$$$${bar}", Expression.compile("foo$$$$${bar}", flags).evaluate((c, b) -> {
}));
assertEquals("foo$$$${bar}$$$baz", Expression.compile("foo$$$$${bar}$$$baz", flags).evaluate((c, b) -> {
}));
assertEquals("foo$$$$", Expression.compile("foo$$$$", flags).evaluate((c, b) -> {
}));
assertEquals("${foo:bar}", Expression.compile("$${foo:bar}", flags).evaluate((c, b) -> {
}));
assertEquals("$${foo:bar}", Expression.compile("$$${foo:bar}", flags).evaluate((c, b) -> {
}));
assertEquals("${foo:}", Expression.compile("$${foo:${bar}}", flags).evaluate((c, b) -> {
}));
assertEquals("${foo:${bar}}", Expression.compile("$${foo:$${bar}}", flags).evaluate((c, b) -> {
}));

assertEquals("", Expression.compile("${foo}", flags).evaluate((c, b) -> {
assertEquals("foo", c.getKey());
}));
assertEquals("", Expression.compile("${foo}${bar}", flags).evaluate((c, b) -> {
if ("foo".equals(c.getKey()))
assertEquals("foo", c.getKey());
if ("bar".equals(c.getKey()))
assertEquals("bar", c.getKey());
}));
assertEquals("foobar", Expression.compile("foo${foo}${bar}bar", flags).evaluate((c, b) -> {
if ("foo".equals(c.getKey()))
assertEquals("foo", c.getKey());
if ("bar".equals(c.getKey()))
assertEquals("bar", c.getKey());
}));
assertEquals("foo${foo}bar", Expression.compile("foo$${foo}${bar}bar", flags).evaluate((c, b) -> {
if ("bar".equals(c.getKey()))
assertEquals("bar", c.getKey());
}));
assertEquals("foo${foo}bar", Expression.compile("foo$${foo${bar}}bar", flags).evaluate((c, b) -> {
if ("bar".equals(c.getKey()))
assertEquals("bar", c.getKey());
}));
assertEquals("", Expression.compile("${}", flags).evaluate((c, b) -> {
assertEquals("", c.getKey());
}));
assertEquals("", Expression.compile("${:}", flags).evaluate((c, b) -> {
assertEquals("", c.getKey());
}));

assertEquals("${foo}", Expression.compile("\\${foo}", flags).evaluate((c, b) -> {
}));
assertEquals("${foo}bar", Expression.compile("\\${foo}bar", flags).evaluate((c, b) -> {
}));
assertEquals("\\$\\{%s}", Expression.compile("\\$\\{%s}", flags).evaluate((c, b) -> {
}));
assertEquals("foo${bar}", Expression.compile("foo\\${bar}", flags).evaluate((c, b) -> {
}));
assertEquals("foo\\${bar}", Expression.compile("foo\\\\${bar}", flags).evaluate((c, b) -> {
}));

assertEquals("foo\\${bar}", Expression.compile("foo\\$${bar}", flags).evaluate((c, b) -> {
}));
assertEquals("foo$${bar}", Expression.compile("foo$\\${bar}", flags).evaluate((c, b) -> {
}));
assertEquals("foo$$\\{bar}", Expression.compile("foo$$\\{bar}", flags).evaluate((c, b) -> {
}));
}
}

0 comments on commit 6b0bc75

Please sign in to comment.