-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Support env variable in condition #13608
Merged
kaisecheng
merged 8 commits into
elastic:main
from
kaisecheng:support_env_variable_in_condition_java
Jan 25, 2022
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7becec3
This commit supports env variable in condition by building a substitu…
kaisecheng 6341e32
support recursive and, or, nand, xor
kaisecheng d2c325f
move substitution before execution
kaisecheng f9a92e7
fix missing args for test
kaisecheng 9e062aa
clean up BooleanExpression cache
kaisecheng 0bfe4a8
add integration test.
kaisecheng 0fe64a0
more test
kaisecheng 51c0f24
add comment. remove java 9 syntax
kaisecheng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
42 changes: 42 additions & 0 deletions
42
logstash-core/src/main/java/org/logstash/config/ir/expression/ExpressionSubstitution.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,42 @@ | ||
package org.logstash.config.ir.expression; | ||
|
||
import org.logstash.common.SourceWithMetadata; | ||
import org.logstash.config.ir.CompiledPipeline; | ||
import org.logstash.config.ir.InvalidIRException; | ||
import org.logstash.plugins.ConfigVariableExpander; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.Map; | ||
|
||
public class ExpressionSubstitution { | ||
public static Expression substituteBoolExpression(ConfigVariableExpander cve, Expression expression) { | ||
try { | ||
if (expression instanceof BinaryBooleanExpression) { | ||
BinaryBooleanExpression binaryBoolExp = (BinaryBooleanExpression) expression; | ||
Expression substitutedLeftExp = substituteBoolExpression(cve, binaryBoolExp.getLeft()); | ||
Expression substitutedRightExp = substituteBoolExpression(cve, binaryBoolExp.getRight()); | ||
if (substitutedLeftExp != binaryBoolExp.getLeft() || substitutedRightExp != binaryBoolExp.getRight()) { | ||
Constructor<? extends BinaryBooleanExpression> constructor = binaryBoolExp.getClass().getConstructor(SourceWithMetadata.class, Expression.class, Expression.class); | ||
return constructor.newInstance(binaryBoolExp.getSourceWithMetadata(), substitutedLeftExp, substitutedRightExp); | ||
} | ||
} else if (expression instanceof UnaryBooleanExpression) { | ||
UnaryBooleanExpression unaryBoolExp = (UnaryBooleanExpression) expression; | ||
Expression substitutedExp = substituteBoolExpression(cve, unaryBoolExp.getExpression()); | ||
if (substitutedExp != unaryBoolExp.getExpression()) { | ||
Constructor<? extends UnaryBooleanExpression> constructor = unaryBoolExp.getClass().getConstructor(SourceWithMetadata.class, Expression.class); | ||
return constructor.newInstance(unaryBoolExp.getSourceWithMetadata(), substitutedExp); | ||
} | ||
} else if (expression instanceof ValueExpression && !(expression instanceof RegexValueExpression) && (((ValueExpression) expression).get() != null)) { | ||
String key = "placeholder"; | ||
Map<String, Object> args = Map.of(key, ((ValueExpression) expression).get()); | ||
Map<String, Object> substitutedArgs = CompiledPipeline.expandConfigVariables(cve, args); | ||
return new ValueExpression(expression.getSourceWithMetadata(), substitutedArgs.get(key)); | ||
} | ||
|
||
return expression; | ||
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException | InvalidIRException e) { | ||
throw new IllegalStateException("Unable to instantiate substituted condition expression", e); | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We tend do avoid wildcard imports, and you can make intellij not do them automatically, see: https://sergiodelamo.com/blog/intellij-idea-disable-wildcard-imports.html