-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #629 from HubSpot/chunk-resolver-parser
AST deferred value handling
- Loading branch information
Showing
63 changed files
with
2,222 additions
and
585 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 43 additions & 37 deletions
80
src/main/java/com/hubspot/jinjava/el/ext/AbsOperator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,57 @@ | ||
package com.hubspot.jinjava.el.ext; | ||
|
||
import com.hubspot.jinjava.el.ext.eager.EagerAstUnary; | ||
import de.odysseus.el.misc.TypeConverter; | ||
import de.odysseus.el.tree.impl.Parser.ExtensionHandler; | ||
import de.odysseus.el.tree.impl.Parser.ExtensionPoint; | ||
import de.odysseus.el.tree.impl.Scanner; | ||
import de.odysseus.el.tree.impl.Scanner.ExtensionToken; | ||
import de.odysseus.el.tree.impl.ast.AstNode; | ||
import de.odysseus.el.tree.impl.ast.AstUnary; | ||
import de.odysseus.el.tree.impl.ast.AstUnary.SimpleOperator; | ||
|
||
public class AbsOperator { | ||
public class AbsOperator extends SimpleOperator { | ||
public static final ExtensionToken TOKEN = new Scanner.ExtensionToken("+"); | ||
public static final AbsOperator OP = new AbsOperator(); | ||
|
||
public static final ExtensionHandler HANDLER = new ExtensionHandler( | ||
ExtensionPoint.UNARY | ||
) { | ||
|
||
@Override | ||
public AstNode createAstNode(AstNode... children) { | ||
return new AstUnary( | ||
children[0], | ||
new AstUnary.SimpleOperator() { | ||
|
||
@Override | ||
protected Object apply(TypeConverter converter, Object o) { | ||
if (o == null) { | ||
return null; | ||
} | ||
|
||
if (o instanceof Float) { | ||
return Math.abs((Float) o); | ||
} | ||
if (o instanceof Double) { | ||
return Math.abs((Double) o); | ||
} | ||
if (o instanceof Integer) { | ||
return Math.abs((Integer) o); | ||
} | ||
if (o instanceof Long) { | ||
return Math.abs((Long) o); | ||
} | ||
|
||
throw new IllegalArgumentException( | ||
"Unable to apply abs operator on object of type: " + o.getClass() | ||
); | ||
} | ||
} | ||
); | ||
public static final ExtensionHandler HANDLER = getHandler(false); | ||
|
||
@Override | ||
protected Object apply(TypeConverter converter, Object o) { | ||
if (o == null) { | ||
return null; | ||
} | ||
|
||
if (o instanceof Float) { | ||
return Math.abs((Float) o); | ||
} | ||
if (o instanceof Double) { | ||
return Math.abs((Double) o); | ||
} | ||
if (o instanceof Integer) { | ||
return Math.abs((Integer) o); | ||
} | ||
}; | ||
if (o instanceof Long) { | ||
return Math.abs((Long) o); | ||
} | ||
|
||
throw new IllegalArgumentException( | ||
"Unable to apply abs operator on object of type: " + o.getClass() | ||
); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "+"; | ||
} | ||
|
||
public static ExtensionHandler getHandler(boolean eager) { | ||
return new ExtensionHandler(ExtensionPoint.UNARY) { | ||
|
||
@Override | ||
public AstNode createAstNode(AstNode... children) { | ||
return eager ? new EagerAstUnary(children[0], OP) : new AstUnary(children[0], OP); | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/java/com/hubspot/jinjava/el/ext/DeferredParsingException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.hubspot.jinjava.el.ext; | ||
|
||
import com.hubspot.jinjava.interpret.DeferredValueException; | ||
|
||
public class DeferredParsingException extends DeferredValueException { | ||
private final String deferredEvalResult; | ||
private final Object sourceNode; | ||
|
||
public DeferredParsingException(Object sourceNode, String deferredEvalResult) { | ||
super( | ||
String.format( | ||
"%s could not be parsed more than: %s", | ||
sourceNode.getClass(), | ||
deferredEvalResult | ||
) | ||
); | ||
this.deferredEvalResult = deferredEvalResult; | ||
this.sourceNode = sourceNode; | ||
} | ||
|
||
public String getDeferredEvalResult() { | ||
return deferredEvalResult; | ||
} | ||
|
||
public Object getSourceNode() { | ||
return sourceNode; | ||
} | ||
} |
Oops, something went wrong.