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

feat: add contains filter [FluxDSL] #354

Merged
merged 6 commits into from
May 23, 2022
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## 6.2.0 [unreleased]

1. [#353](https://github.com/influxdata/influxdb-client-java/pull/353): Supports `contains` filter [FluxDSL]

## 6.1.0 [2022-05-20]

### Breaking Changes
Expand Down
1 change: 1 addition & 0 deletions flux-dsl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ Supported Record restrictions:
- `exists`
- `lessOrEqual`
- `greaterOrEqual`
- `contains`
- `custom` - the custom restriction by `Restrictions.value().custom(15L, "=~")`

```java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
package com.influxdb.query.dsl.functions.restriction;

import java.util.Arrays;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;

import com.influxdb.query.dsl.functions.properties.FunctionsParameters;
Expand Down Expand Up @@ -143,6 +145,32 @@ public String toString() {
}
}

/**
* Check if an record contains a key or if that key’s value is null.
*
* @return restriction
*/
@Nonnull
public Restrictions contains(@Nonnull final String[] set) {
return new ContainsRestrictions(fieldName, set);
}

private final class ContainsRestrictions extends Restrictions {
private final String fieldName;
private final String[] set;

public ContainsRestrictions(@Nonnull final String fieldName, @Nonnull final String[] set) {
this.fieldName = fieldName;
this.set = set;
}

@Override
public String toString() {
return "contains(value: r[\"" + fieldName + "\"], set:["
+ Arrays.stream(set).collect(Collectors.joining("\", \"", "\"", "\"")) + "])";
}
}

private final class OperatorRestrictions extends Restrictions {
private final String fieldName;
private final Object fieldValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ void exists() {
Assertions.assertThat(restrictions.toString()).isEqualTo("exists r[\"_value\"]");
}

@Test
void contains() {

Restrictions restrictions = Restrictions.value().contains(new String[]{"value1", "value2"});

Assertions.assertThat(restrictions.toString()).isEqualTo("contains(value: r[\"_value\"], set:[\"value1\", \"value2\"])");
}

@Test
void not() {

Expand All @@ -87,4 +95,4 @@ void emptyLogical() {
restrictions = Restrictions.or(Restrictions.or());
Assertions.assertThat(restrictions.toString()).isEqualTo("");
}
}
}