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

Fixes for filtering and addition of 'from' filter #21

Merged
merged 1 commit into from
Apr 29, 2024
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
7 changes: 7 additions & 0 deletions datatrails-common-api/assets/v2/assets/listevents.proto
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ message ListEventsRequest {

// filter events by mmr index
optional int64 mmr_index = 24 [(validate.rules).int64.gte = 0];

// filter from wallet address
optional string from = 25 [
(validate.rules).string = {
pattern: "^0x[[:xdigit:]]+$"
}
];
}

message ListEventsResponse {
Expand Down
11 changes: 6 additions & 5 deletions datatrails-common-api/marshalers/query/attributeparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,15 +264,15 @@ func getFilterParts(spec string) ([]string, error) {
filters := []string{}

// greedy match for stuff surrounded by single quotes
matchQuote := `'((?:[^']|.)*)'`
matchQuote := `'((?:[^'\\]|\\.)*)'`
q, err := regexp.Compile(matchQuote)
if err != nil {
return filters, err
}

// match fiter-ish thing - some attribute equals to a value in quotes with spaces
// or continues string
matchTerm := `(\S+?)=(('.+')|(\S+?))(\s|$)`
// or continous string
matchTerm := `(\S+?)=(('((?:[^'\\]|\\.)*)')|(\S+?))(\s|$)`
r, err := regexp.Compile(matchTerm)
if err != nil {
return filters, err
Expand All @@ -285,16 +285,17 @@ func getFilterParts(spec string) ([]string, error) {
term := r.FindStringSubmatch(filterSpec)
// trim excessive unquoted spaces and trim the part we're processing
filterSpec = strings.TrimSpace(strings.TrimPrefix(filterSpec, term[0]))
// trim outer quotes if they are present
// trim outer quotes if they are present and replace the escape sequences
t := q.ReplaceAllString(term[2], "$1")
t = strings.ReplaceAll(t, "\\'", "'")
// append our new term to filters list
filters = append(filters, fmt.Sprintf("%s=%s", strings.TrimSpace(term[1]), t))
// check if we have anything left
if len(filterSpec) > 0 {
// if we have an or we remove it, otherwise we fail
// as we have some additonal unexpected text
if !strings.HasPrefix(strings.ToLower(filterSpec), "or ") {
return filters, fmt.Errorf("malformd filter")
return filters, fmt.Errorf("malformed filter")
}
filterSpec = filterSpec[3:]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ func TestFilterParsing(t *testing.T) {
{
name: "test multiple or",
message: &assets.ListAssetsRequest{},
values: map[string][]string{"filters": {"attributes.foo=2 OR attributes.bar=2", "attributes.foo=dd OR attributes.bar=77"}},
values: map[string][]string{"filters": {"attributes.foo='2' OR attributes.bar=2", "attributes.foo='dd xx' OR attributes.bar='77'"}},
parser: &runtime.DefaultQueryParser{},
expectedResult: [][]string{{"attributes.foo=2", "attributes.bar=2"}, {"attributes.foo=dd", "attributes.bar=77"}},
expectedResult: [][]string{{"attributes.foo=2", "attributes.bar=2"}, {"attributes.foo=dd xx", "attributes.bar=77"}},
},
{
name: "test or *",
Expand Down Expand Up @@ -118,12 +118,12 @@ func TestGetFilterParts(t *testing.T) {
},
{
name: "test different cases",
filterSpec: "attributes.foo=2 OR attributes.bar=' stuff or 245677 some 'other' stuff' oR foo.bar=single or bar.baz=popmo Or pop=push_1",
filterSpec: `attributes.foo=2 OR attributes.bar=' stuff or 245677 some \'other\' stuff' oR foo.bar=single or bar.baz=popmo Or pop=push_1`,
expectedResult: []string{"attributes.foo=2", "attributes.bar= stuff or 245677 some 'other' stuff", "foo.bar=single", "bar.baz=popmo", "pop=push_1"},
},
{
name: "test quoted",
filterSpec: "attributes.foo=2 or attributes.bar=''something' or = 'else''",
filterSpec: "attributes.foo=2 or attributes.bar='\\'something\\' or = \\'else\\''",
expectedResult: []string{"attributes.foo=2", "attributes.bar='something' or = 'else'"},
},
{
Expand Down
Loading