Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Synapse Expression support #2227

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
target/

## various other file types we don't want to have in the repository:
.DS_Store
.DS_Store
/modules/core/src/main/antlr4/org/apache/synapse/util/synapse_path/gen/
42 changes: 42 additions & 0 deletions modules/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,44 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<executions>
<execution>
<id>antlr</id>
<phase>generate-sources</phase>
<goals>
<goal>antlr4</goal>
</goals>
<configuration>
<sourceDirectory>${project.basedir}/src/main/antlr4</sourceDirectory>
<arguments>
<argument>-visitor</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
<!-- Build Helper Maven Plugin -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/antlr4</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>

<!-- Attach a JAR with the test classes so that we can reuse them in other
modules (see http://maven.apache.org/guides/mini/guide-attached-tests.html). -->
Expand Down Expand Up @@ -506,5 +544,9 @@
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</dependency>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
lexer grammar ExpressionLexer;

JSONPATH_PARAMS: 'in' | 'nin' | 'subsetof' | 'anyof' | 'noneof' | 'size' | 'empty' | '=~';

JSONPATH_FUNCTIONS: 'length()' | 'size()' | 'min()' | 'max()' | 'avg()' | 'sum()' | 'stddev()' | 'keys()' | 'first()' | 'last()';

// Tokens for identifiers, operators, and keywords
VAR: 'var';
PAYLOAD: 'payload' | '$';
HEADERS: 'headers';
CONFIG: 'config';
ATTRIBUTES: 'attributes' | 'attr';
AND: 'and' | '&&';
OR: 'or' | '||';
NOT: '!';

DOUBLE_DOT : '..';
ASTERISK : '*';

// Operators
PLUS: '+';
MINUS: '-';
DIV: '/';
MODULO: '%';
EQ: '==';
NEQ: '!=';
GT: '>';
LT: '<';
GTE: '>=';
LTE: '<=';

// Delimiters
LPAREN: '(';
RPAREN: ')';
LBRACKET: '[';
RBRACKET: ']';
DOT: '.';
COMMA: ',';
COLON: ':';
QUOTE: '"' | '\'';

// Literals
BOOLEAN_LITERAL: 'true' | 'false';
NUMBER: '-'? [0-9]+ ('.' [0-9]+)?;


STRING_LITERAL : ('"' (ESC | ~["\\])* '"' | '\'' (ESC | ~['\\])* '\'');


fragment ESC
: '\\' [btnfr"'\\/] // Basic escape sequences
| UNICODE_ESC
| OCTAL_ESC
;

fragment UNICODE_ESC
: '\\' 'u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT
;

fragment OCTAL_ESC
: '\\' [0-3]? [0-7] [0-7]
;

fragment HEX_DIGIT
: [0-9a-fA-F]
;

NULL_LITERAL
: 'null' // Define null as a recognized keyword
;

// Identifiers
ID: [a-zA-Z_][a-zA-Z_0-9]*;

// Special symbols for JSONPath filter expressions
QUESTION: '?';
AT: '@';

// Whitespace
WS: [ \t\n\r]+ -> skip;
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
parser grammar ExpressionParser;

options {
tokenVocab = ExpressionLexer;
}

expression
: comparisonExpression
| conditionalExpression
| EOF
;

conditionalExpression
: comparisonExpression (QUESTION expression COLON expression)?
;

comparisonExpression
: logicalExpression ( (GT | LT | GTE | LTE | EQ | NEQ) logicalExpression )*
| logicalExpression (EQ | NEQ) NULL_LITERAL
;

logicalExpression
: arithmeticExpression (AND logicalExpression | OR logicalExpression)?
;

arithmeticExpression
: term ( (PLUS | MINUS) term )*
;

term
: factor ( (ASTERISK | DIV | MODULO) factor )*
;

factor
: literal
| functionCall
| variableAccess
| payloadAccess
| headerAccess
| configAccess
| attributeAccess
| LPAREN expression RPAREN
;

configAccess
: CONFIG propertyName
;

headerAccess
: HEADERS propertyName
;

attributeAccess
: ATTRIBUTES (DOT ID propertyName)
;

propertyName
: DOT ID
| (DOT)? LBRACKET STRING_LITERAL RBRACKET
;

literal
: arrayLiteral
| BOOLEAN_LITERAL
| NUMBER
| STRING_LITERAL
| NULL_LITERAL
;

jsonPathExpression
:( DOT JSONPATH_FUNCTIONS
|DOUBLE_DOT ASTERISK
| DOUBLE_DOT ID
| DOT ID
| (DOT)? LBRACKET arrayIndex RBRACKET
| DOT ASTERISK
| DOUBLE_DOT ID (LBRACKET arrayIndex RBRACKET)?)+
;


variableAccess
: VAR ( DOT ID
| DOT STRING_LITERAL
| (DOT)? LBRACKET STRING_LITERAL RBRACKET // Bracket notation: var["variableName"]
)
( jsonPathExpression )?
;

arrayLiteral
: LBRACKET (expression (COMMA expression)*)? RBRACKET // Array with zero or more literals, separated by commas
;

payloadAccess
: PAYLOAD ( jsonPathExpression)?
;

arrayIndex
: NUMBER
| STRING_LITERAL
| expression
| multipleArrayIndices
| sliceArrayIndex
| expression ( (PLUS | MINUS | ASTERISK | DIV ) expression)*
| ASTERISK
| QUESTION? filterExpression
;

multipleArrayIndices
: expression (COMMA expression)+
;

sliceArrayIndex
: signedExpressions? COLON signedExpressions? (COLON signedExpressions?)?
;

signedExpressions
: MINUS? expression
;

filterExpression
: (filterComponent)+
;

filterComponent
: variableAccess
| payloadAccess
| headerAccess
| configAccess
| attributeAccess
| functionCall
| stringOrOperator
;

stringOrOperator
: QUESTION | AT | JSONPATH_PARAMS | STRING_LITERAL |NUMBER | BOOLEAN_LITERAL | ID | GT | LT | GTE | LTE | EQ | NEQ
| PLUS | MINUS | DIV | LPAREN | RPAREN | DOT | COMMA | COLON | WS | AND | OR | NOT | ASTERISK
;


functionCall
: ID LPAREN (expression (COMMA expression)*)? RPAREN functionCallSuffix?
;

functionCallSuffix
: DOT ID LPAREN (expression (COMMA expression)*)? RPAREN // Method chaining
| jsonPathExpression
;
Original file line number Diff line number Diff line change
Expand Up @@ -625,4 +625,57 @@ public enum ENDPOINT_TIMEOUT_TYPE { ENDPOINT_TIMEOUT, GLOBAL_TIMEOUT, HTTP_CONNE
public static final String JAEGER_SPAN_ID = "jaeger_span_id";

public static final String ANALYTICS_METADATA = "ANALYTICS_METADATA";

// Constants related to Synapse Expressions
public static final String AND = "and";
public static final String OR = "or";
public static final String NOT = "not";
public static final String TO_LOWER = "toLower";
public static final String TO_UPPER = "toUpper";
public static final String LENGTH = "length";
public static final String SUBSTRING = "subString";
public static final String STARTS_WITH = "startsWith";
public static final String ENDS_WITH = "endsWith";
public static final String CONTAINS = "contains";
public static final String TRIM = "trim";
public static final String REPLACE = "replace";
public static final String SPLIT = "split";
public static final String NOW = "now";
public static final String ABS = "abs";
public static final String CEIL = "ceil";
public static final String FLOOR = "floor";
public static final String SQRT = "sqrt";
public static final String LOG = "log";
public static final String POW = "pow";
public static final String B64ENCODE = "base64encode";
public static final String B64DECODE = "base64decode";
public static final String URL_ENCODE = "urlEncode";
public static final String URL_DECODE = "urlDecode";
public static final String IS_NUMBER = "isNumber";
public static final String IS_STRING = "isString";
public static final String IS_ARRAY = "isArray";
public static final String IS_OBJECT = "isObject";
public static final String OBJECT = "object";
public static final String ARRAY = "array";
public static final String REGISTRY = "registry";
public static final String EXISTS = "exists";
public static final String XPATH = "xpath";
public static final String SECRET = "secret";

public static final String ROUND = "round";
public static final String INTEGER = "integer";
public static final String FLOAT = "float";
public static final String STRING = "string";
public static final String BOOLEAN = "boolean";

public static final String PAYLOAD = "payload";
public static final String PAYLOAD_$ = "$";
public static final String SYNAPSE_EXPRESSION_IDENTIFIER_START = "${";
public static final String SYNAPSE_EXPRESSION_IDENTIFIER_END = "}";
public static final String AXIS2 = "axis2";
public static final String QUERY_PARAM = "queryParams";
public static final String URI_PARAM = "uriParams";

public static final String UNKNOWN = "unknown";
public static final String VAULT_LOOKUP = "wso2:vault-lookup('";
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
import org.apache.axiom.om.OMAttribute;
import org.apache.axiom.om.OMElement;
import org.apache.synapse.Mediator;
import org.apache.synapse.SynapseConstants;
import org.apache.synapse.SynapseException;
import org.apache.synapse.mediators.Value;
import org.apache.synapse.mediators.builtin.PropertyMediator;
import org.apache.synapse.util.MediatorPropertyUtils;
import org.apache.synapse.util.xpath.SynapseJsonPath;
import org.apache.synapse.util.xpath.SynapseExpression;
import org.apache.synapse.util.xpath.SynapseXPath;
import org.jaxen.JaxenException;

Expand Down Expand Up @@ -83,6 +85,9 @@ public Mediator createSpecificMediator(OMElement elem, Properties properties) {
String nameExpression = nameAttributeValue.substring(1, nameAttributeValue.length() - 1);
if(nameExpression.startsWith("json-eval(")) {
new SynapseJsonPath(nameExpression.substring(10, nameExpression.length() - 1));
} else if (nameExpression.startsWith(SynapseConstants.SYNAPSE_EXPRESSION_IDENTIFIER_START) &&
nameExpression.endsWith(SynapseConstants.SYNAPSE_EXPRESSION_IDENTIFIER_END)) {
new SynapseExpression(nameExpression.substring(2, nameExpression.length() - 1));
} else {
new SynapseXPath(nameExpression);
}
Expand Down
Loading
Loading