@Test public void testPathExpression() { String inputExpression = "[ 10, 15 ].size"; BaseNode pathBase = parse( inputExpression ); assertThat( pathBase, is( instanceOf( PathExpressionNode.class ) ) ); assertThat( pathBase.getText(), is( inputExpression ) ); PathExpressionNode pathExpr = (PathExpressionNode) pathBase; assertThat( pathExpr.getExpression(), is( instanceOf( ListNode.class ) ) ); assertThat( pathExpr.getExpression().getText(), is( "10, 15" ) ); assertThat( pathExpr.getName(), is( instanceOf( NameRefNode.class ) ) ); assertThat( pathExpr.getName().getText(), is( "size" ) ); }
@Test public void testContextPathExpression3() { String inputExpression = "{ first name : \"bob\" }.first name"; BaseNode pathBase = parse( inputExpression ); assertThat( pathBase, is( instanceOf( PathExpressionNode.class ) ) ); assertThat( pathBase.getText(), is( inputExpression ) ); assertThat( pathBase.getResultType(), is( BuiltInType.STRING ) ); PathExpressionNode pathExpr = (PathExpressionNode) pathBase; assertThat( pathExpr.getExpression(), is( instanceOf( ContextNode.class ) ) ); assertThat( pathExpr.getExpression().getText(), is( "{ first name : \"bob\" }" ) ); assertThat( pathExpr.getName(), is( instanceOf( NameRefNode.class ) ) ); assertThat( pathExpr.getName().getText(), is( "first name" ) ); }
@Test public void testContextPathExpression() { String inputExpression = "{ x : \"foo\" }.x"; BaseNode pathBase = parse( inputExpression ); assertThat( pathBase, is( instanceOf( PathExpressionNode.class ) ) ); assertThat( pathBase.getText(), is( inputExpression ) ); assertThat( pathBase.getResultType(), is( BuiltInType.STRING ) ); PathExpressionNode pathExpr = (PathExpressionNode) pathBase; assertThat( pathExpr.getExpression(), is( instanceOf( ContextNode.class ) ) ); assertThat( pathExpr.getExpression().getText(), is( "{ x : \"foo\" }" ) ); assertThat( pathExpr.getName(), is( instanceOf( NameRefNode.class ) ) ); assertThat( pathExpr.getName().getText(), is( "x" ) ); }
@Test public void testContextPathExpression2() { String inputExpression = "{ x : { y : \"foo\" } }.x.y"; BaseNode pathBase = parse( inputExpression ); assertThat( pathBase, is( instanceOf( PathExpressionNode.class ) ) ); assertThat( pathBase.getText(), is( inputExpression ) ); assertThat( pathBase.getResultType(), is( BuiltInType.STRING ) ); PathExpressionNode pathExpr = (PathExpressionNode) pathBase; assertThat( pathExpr.getExpression(), is( instanceOf( ContextNode.class ) ) ); assertThat( pathExpr.getExpression().getText(), is( "{ x : { y : \"foo\" } }" ) ); assertThat( pathExpr.getName(), is( instanceOf( QualifiedNameNode.class ) ) ); assertThat( pathExpr.getName().getText(), is( "x.y" ) ); }
@Override public DirectCompilerResult visit(PathExpressionNode n) { DirectCompilerResult expr = n.getExpression().accept(this); BaseNode nameNode = n.getName(); if (nameNode instanceof QualifiedNameNode) { QualifiedNameNode qualifiedNameNode = (QualifiedNameNode) n.getName(); List<Expression> exprs = qualifiedNameNode.getParts().stream() .map(name -> new StringLiteralExpr(name.getText())) .collect(Collectors.toList()); return DirectCompilerResult.of( Expressions.path(expr.getExpression(), exprs), // here we could still try to infer the result type, but presently use ANY BuiltInType.UNKNOWN).withFD(expr); } else { return DirectCompilerResult.of( Expressions.path(expr.getExpression(), new StringLiteralExpr(nameNode.getText())), // here we could still try to infer the result type, but presently use ANY BuiltInType.UNKNOWN).withFD(expr); } }