Skip to content

Commit 136dbf9

Browse files
author
Waldemar Parzonka
committed
AB#9341 fixes for filtering and addition of 'from' filter
1 parent 7380948 commit 136dbf9

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

datatrails-common-api/assets/v2/assets/listevents.proto

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ message ListEventsRequest {
142142

143143
// filter events by mmr index
144144
optional int64 mmr_index = 24 [(validate.rules).int64.gte = 0];
145+
146+
// filter from wallet address
147+
optional string from = 25 [
148+
(validate.rules).string = {
149+
pattern: "^0x[[:xdigit:]]+$"
150+
}
151+
];
145152
}
146153

147154
message ListEventsResponse {

datatrails-common-api/marshalers/query/attributeparser.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,15 +264,15 @@ func getFilterParts(spec string) ([]string, error) {
264264
filters := []string{}
265265

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

273273
// match fiter-ish thing - some attribute equals to a value in quotes with spaces
274-
// or continues string
275-
matchTerm := `(\S+?)=(('.+')|(\S+?))(\s|$)`
274+
// or continous string
275+
matchTerm := `(\S+?)=(('((?:[^'\\]|\\.)*)')|(\S+?))(\s|$)`
276276
r, err := regexp.Compile(matchTerm)
277277
if err != nil {
278278
return filters, err
@@ -285,16 +285,17 @@ func getFilterParts(spec string) ([]string, error) {
285285
term := r.FindStringSubmatch(filterSpec)
286286
// trim excessive unquoted spaces and trim the part we're processing
287287
filterSpec = strings.TrimSpace(strings.TrimPrefix(filterSpec, term[0]))
288-
// trim outer quotes if they are present
288+
// trim outer quotes if they are present and replace the escape sequences
289289
t := q.ReplaceAllString(term[2], "$1")
290+
t = strings.ReplaceAll(t, "\\'", "'")
290291
// append our new term to filters list
291292
filters = append(filters, fmt.Sprintf("%s=%s", strings.TrimSpace(term[1]), t))
292293
// check if we have anything left
293294
if len(filterSpec) > 0 {
294295
// if we have an or we remove it, otherwise we fail
295296
// as we have some additonal unexpected text
296297
if !strings.HasPrefix(strings.ToLower(filterSpec), "or ") {
297-
return filters, fmt.Errorf("malformd filter")
298+
return filters, fmt.Errorf("malformed filter")
298299
}
299300
filterSpec = filterSpec[3:]
300301
}

datatrails-common-api/marshalers/query/attributeparser_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ func TestFilterParsing(t *testing.T) {
3232
{
3333
name: "test multiple or",
3434
message: &assets.ListAssetsRequest{},
35-
values: map[string][]string{"filters": {"attributes.foo=2 OR attributes.bar=2", "attributes.foo=dd OR attributes.bar=77"}},
35+
values: map[string][]string{"filters": {"attributes.foo='2' OR attributes.bar=2", "attributes.foo='dd xx' OR attributes.bar='77'"}},
3636
parser: &runtime.DefaultQueryParser{},
37-
expectedResult: [][]string{{"attributes.foo=2", "attributes.bar=2"}, {"attributes.foo=dd", "attributes.bar=77"}},
37+
expectedResult: [][]string{{"attributes.foo=2", "attributes.bar=2"}, {"attributes.foo=dd xx", "attributes.bar=77"}},
3838
},
3939
{
4040
name: "test or *",
@@ -118,12 +118,12 @@ func TestGetFilterParts(t *testing.T) {
118118
},
119119
{
120120
name: "test different cases",
121-
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",
121+
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`,
122122
expectedResult: []string{"attributes.foo=2", "attributes.bar= stuff or 245677 some 'other' stuff", "foo.bar=single", "bar.baz=popmo", "pop=push_1"},
123123
},
124124
{
125125
name: "test quoted",
126-
filterSpec: "attributes.foo=2 or attributes.bar=''something' or = 'else''",
126+
filterSpec: "attributes.foo=2 or attributes.bar='\\'something\\' or = \\'else\\''",
127127
expectedResult: []string{"attributes.foo=2", "attributes.bar='something' or = 'else'"},
128128
},
129129
{

0 commit comments

Comments
 (0)