Skip to content

Commit

Permalink
server: allow regexp in meta filters (#852)
Browse files Browse the repository at this point in the history
  • Loading branch information
brig authored Dec 26, 2023
1 parent d07ffc9 commit 8b8f152
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ public static Condition jsonbTextNotExistsByPath(Field<JSONB> field, List<String
return DSL.condition("not {0} #> {1} ?? {2}", field, inline(toPath(path)), DSL.value(value));
}

public static Condition jsonbTextMatch(Field<JSONB> field, List<String> path, String value) {
return DSL.condition("{0} #>> {1} ~ {2}", field, inline(toPath(path)), DSL.value(value));
}

/**
* Returns a JOOQ field "now - d" where "d" is the specified duration.
* The result is rounded down to seconds.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public final class FilterUtils {
SuffixMapping.of(".notEndsWith", ProcessFilter.FilterType.NOT_ENDS_WITH),

SuffixMapping.of(".ge", ProcessFilter.FilterType.GREATER_OR_EQUALS),
SuffixMapping.of(".len", ProcessFilter.FilterType.LESS_OR_EQUALS_OR_NULL)
SuffixMapping.of(".len", ProcessFilter.FilterType.LESS_OR_EQUALS_OR_NULL),

SuffixMapping.of(".regexp", ProcessFilter.FilterType.REGEXP_MATCH)
};

public static List<ProcessFilter.DateFilter> parseDate(String paramName, UriInfo uriInfo) {
Expand Down Expand Up @@ -152,6 +154,10 @@ public static void applyJson(SelectQuery<Record> q, Field<JSONB> column, List<Pr
q.addConditions(jsonbTextNotExistsByPath(column, f.path(), f.value()));
break;
}
case REGEXP_MATCH: {
q.addConditions(jsonbTextMatch(column, f.path(), f.value()));
break;
}
case STARTS_WITH: {
q.addConditions(jsonbTextByPath(column, f.path()).startsWith(f.value()));
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ enum FilterType {
NOT_STARTS_WITH,

GREATER_OR_EQUALS,
LESS_OR_EQUALS_OR_NULL
LESS_OR_EQUALS_OR_NULL,

REGEXP_MATCH
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.walmartlabs.concord.server.process.queue;

/*-
* *****
* Concord
* -----
* Copyright (C) 2017 - 2023 Walmart Inc.
* -----
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =====
*/

import org.junit.jupiter.api.Test;

import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.UriInfo;
import java.util.Collections;
import java.util.List;

import static com.walmartlabs.concord.server.process.queue.ProcessFilter.FilterType.REGEXP_MATCH;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class FilterUtilsTest {

@Test
public void testRegularExpressionMatch() {
MultivaluedHashMap<String, String> m = new MultivaluedHashMap<>();
m.put("test.regexp", Collections.singletonList("myValue"));

UriInfo uriInfo = mock(UriInfo.class);
when(uriInfo.getQueryParameters()).thenReturn(m);

List<ProcessFilter.JsonFilter> filter = FilterUtils.parseJson("test", uriInfo);
assertEquals(1, filter.size());
assertEquals(REGEXP_MATCH, filter.get(0).type());
assertEquals("myValue", filter.get(0).value());
}
}

0 comments on commit 8b8f152

Please sign in to comment.