-
Notifications
You must be signed in to change notification settings - Fork 318
Add support of Set in Expression Language #6992
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
86 changes: 86 additions & 0 deletions
86
...ent/agent-debugger/debugger-el/src/main/java/com/datadog/debugger/el/values/SetValue.java
This file contains hidden or 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,86 @@ | ||
| package com.datadog.debugger.el.values; | ||
|
|
||
| import com.datadog.debugger.el.Value; | ||
| import com.datadog.debugger.el.Visitor; | ||
| import com.datadog.debugger.el.expressions.ValueExpression; | ||
| import datadog.trace.bootstrap.debugger.el.ValueReferenceResolver; | ||
| import datadog.trace.bootstrap.debugger.el.Values; | ||
| import datadog.trace.bootstrap.debugger.util.WellKnownClasses; | ||
| import java.util.Set; | ||
|
|
||
| public class SetValue implements CollectionValue<Object>, ValueExpression<SetValue> { | ||
|
|
||
| private final Object setHolder; | ||
|
|
||
| public SetValue(Object object) { | ||
| if (object instanceof Set) { | ||
| setHolder = object; | ||
| } else if (object == null || object == Values.NULL_OBJECT) { | ||
| setHolder = Value.nullValue(); | ||
| } else { | ||
| setHolder = Value.undefinedValue(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public SetValue evaluate(ValueReferenceResolver valueRefResolver) { | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public Object getValue() { | ||
| return setHolder; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isEmpty() { | ||
| if (setHolder instanceof Set) { | ||
| return ((Set<?>) setHolder).isEmpty(); | ||
| } else if (setHolder instanceof Value) { | ||
| Value<?> val = (Value<?>) setHolder; | ||
| return val.isNull() || val.isUndefined(); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public int count() { | ||
| if (setHolder instanceof Set) { | ||
| if (WellKnownClasses.isSizeSafe((Set<?>) setHolder)) { | ||
| return ((Set<?>) setHolder).size(); | ||
| } | ||
| throw new RuntimeException("Unsupported Set class: " + setHolder.getClass().getTypeName()); | ||
| } else if (setHolder == Value.nullValue()) { | ||
| return 0; | ||
| } | ||
| return -1; | ||
| } | ||
|
|
||
| @Override | ||
| public Value<?> get(Object key) { | ||
| if (key == Value.undefinedValue() || key == Values.UNDEFINED_OBJECT) { | ||
| return Value.undefinedValue(); | ||
| } | ||
| if (key == null || key == Value.nullValue() || key == Values.NULL_OBJECT) { | ||
| return Value.nullValue(); | ||
| } | ||
|
|
||
| if (setHolder instanceof Set) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why we don't check WellKnownClasses here?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same for Maps. |
||
| Set<?> set = (Set<?>) setHolder; | ||
| key = key instanceof Value ? ((Value<?>) key).getValue() : key; | ||
| return Value.of(set.contains(key)); | ||
| } | ||
| // the result will be either Value.nullValue() or Value.undefinedValue() depending on the holder | ||
| // value | ||
| return (Value<?>) setHolder; | ||
| } | ||
|
|
||
| @Override | ||
| public <R> R accept(Visitor<R> visitor) { | ||
| return visitor.visit(this); | ||
| } | ||
|
|
||
| public Object getSetHolder() { | ||
| return setHolder; | ||
| } | ||
| } | ||
This file contains hidden or 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
46 changes: 46 additions & 0 deletions
46
...-debugger/debugger-el/src/test/java/com/datadog/debugger/el/values/SetValueEmptyTest.java
This file contains hidden or 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,46 @@ | ||
| package com.datadog.debugger.el.values; | ||
|
|
||
| import static com.datadog.debugger.el.PrettyPrintVisitor.print; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import com.datadog.debugger.el.Value; | ||
| import datadog.trace.bootstrap.debugger.el.Values; | ||
| import java.util.Collections; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class SetValueEmptyTest { | ||
| private SetValue instance; | ||
|
|
||
| @BeforeEach | ||
| void setup() throws Exception { | ||
| instance = new SetValue(Collections.emptySet()); | ||
| } | ||
|
|
||
| @Test | ||
| void prettyPrint() { | ||
| assertEquals("Set", print(instance)); | ||
| } | ||
|
|
||
| @Test | ||
| void isEmpty() { | ||
| assertTrue(instance.isEmpty()); | ||
| } | ||
|
|
||
| @Test | ||
| void count() { | ||
| assertEquals(0, instance.count()); | ||
| } | ||
|
|
||
| @Test | ||
| void get() { | ||
| assertEquals(BooleanValue.FALSE, instance.get("a")); | ||
| assertEquals(BooleanValue.FALSE, instance.get("b")); | ||
| assertEquals(Value.undefinedValue(), instance.get(Values.UNDEFINED_OBJECT)); | ||
| assertEquals(Value.undefinedValue(), instance.get(Value.undefinedValue())); | ||
| assertEquals(Value.nullValue(), instance.get(Values.NULL_OBJECT)); | ||
| assertEquals(Value.nullValue(), instance.get(Value.nullValue())); | ||
| assertEquals(Value.nullValue(), instance.get(null)); | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
...t-debugger/debugger-el/src/test/java/com/datadog/debugger/el/values/SetValueNullTest.java
This file contains hidden or 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,39 @@ | ||
| package com.datadog.debugger.el.values; | ||
|
|
||
| import static com.datadog.debugger.el.PrettyPrintVisitor.print; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import com.datadog.debugger.el.Value; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class SetValueNullTest { | ||
| private SetValue instance; | ||
|
|
||
| @BeforeEach | ||
| void setup() throws Exception { | ||
| instance = new SetValue(null); | ||
| } | ||
|
|
||
| @Test | ||
| void prettyPrint() { | ||
| assertEquals("null", print(instance)); | ||
| } | ||
|
|
||
| @Test | ||
| void isEmpty() { | ||
| assertTrue(instance.isEmpty()); | ||
| } | ||
|
|
||
| @Test | ||
| void count() { | ||
| assertEquals(0, instance.count()); | ||
| } | ||
|
|
||
| @Test | ||
| void get() { | ||
| assertEquals(Value.nullValue(), instance.get(0)); | ||
| assertEquals(Value.nullValue(), instance.get(10)); | ||
| } | ||
| } |
50 changes: 50 additions & 0 deletions
50
...agent-debugger/debugger-el/src/test/java/com/datadog/debugger/el/values/SetValueTest.java
This file contains hidden or 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,50 @@ | ||
| package com.datadog.debugger.el.values; | ||
|
|
||
| import static com.datadog.debugger.el.PrettyPrintVisitor.print; | ||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| import com.datadog.debugger.el.Value; | ||
| import datadog.trace.bootstrap.debugger.el.Values; | ||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class SetValueTest { | ||
| private SetValue instance; | ||
|
|
||
| @BeforeEach | ||
| void setup() throws Exception { | ||
| Set<String> set = new HashSet<>(); | ||
| set.add("foo"); | ||
| set.add("bar"); | ||
| instance = new SetValue(set); | ||
| } | ||
|
|
||
| @Test | ||
| void prettyPrint() { | ||
| assertEquals("Set", print(instance)); | ||
| } | ||
|
|
||
| @Test | ||
| void isEmpty() { | ||
| assertFalse(instance.isEmpty()); | ||
| } | ||
|
|
||
| @Test | ||
| void count() { | ||
| assertEquals(2, instance.count()); | ||
| } | ||
|
|
||
| @Test | ||
| void get() { | ||
| assertEquals(Value.of(true), instance.get("foo")); | ||
| assertEquals(BooleanValue.TRUE, instance.get("foo")); | ||
| assertEquals(Value.of(true), instance.get(Value.of("foo"))); | ||
| assertEquals(Value.of(false), instance.get("oof")); | ||
| assertEquals(BooleanValue.FALSE, instance.get("oof")); | ||
| assertEquals(Value.of(false), instance.get(Value.of("oof"))); | ||
| assertEquals(Value.undefinedValue(), instance.get(Values.UNDEFINED_OBJECT)); | ||
| assertEquals(Value.undefinedValue(), instance.get(Value.undefinedValue())); | ||
| } | ||
| } |
This file contains hidden or 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
13 changes: 13 additions & 0 deletions
13
dd-java-agent/agent-debugger/debugger-el/src/test/resources/test_conditional_14.json
This file contains hidden or 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,13 @@ | ||
| { | ||
| "dsl": "", | ||
| "json": { | ||
| "and": [ | ||
| {"eq": [{"count": {"ref": "intArray"}}, 3]}, | ||
| {"eq": [{"len": {"ref": "intArray"}}, 3]}, | ||
| {"eq": [{"count": {"ref": "strArray"}}, 2]}, | ||
| {"eq": [{"count": {"ref": "strMap"}}, 2]}, | ||
| {"eq": [{"count": {"ref": "strSet"}}, 1]}, | ||
| {"eq": [{"count": {"ref": "strList"}}, 1]} | ||
| ] | ||
| } | ||
| } |
This file contains hidden or 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.
why -1? why not undefined? is that in the spec ?
I think count on String also return -1 for null. not sure why we do that.
Uh oh!
There was an error while loading. Please reload this page.
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.
legacy from the initial implementation. should never reached this code.
because method should return an int! otherwise we need to throw an exception