From 20e2314128d735da34a26d8db56226baf8025b73 Mon Sep 17 00:00:00 2001 From: kolaente Date: Sat, 6 Apr 2024 11:48:27 +0200 Subject: [PATCH] fix(filters): enclose values with a slash in them as strings so that date math values work Previously, in a filter like "due_date = now/d", the / was parsed as the beginning of a comment, but as it did not contain the full value, this is an invalid comment, resulting in an error message. Resolves https://community.vikunja.io/t/filter-setting-s/1791/12 --- pkg/models/task_collection_filter.go | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/pkg/models/task_collection_filter.go b/pkg/models/task_collection_filter.go index 9ac4ad847e..f11a9d1b49 100644 --- a/pkg/models/task_collection_filter.go +++ b/pkg/models/task_collection_filter.go @@ -24,10 +24,9 @@ import ( "strings" "time" - "github.com/ganigeorgiev/fexpr" - "code.vikunja.io/api/pkg/config" + "github.com/ganigeorgiev/fexpr" "github.com/iancoleman/strcase" "github.com/jszwedko/go-datemath" "xorm.io/xorm/schemas" @@ -155,15 +154,27 @@ func getTaskFiltersFromFilterString(filter string, filterTimezone string) (filte filter = strings.ReplaceAll(filter, " in ", " ?= ") // Replaces all occurrences with in with a string so that it passes the filter - pattern := `\?=\s+([^&|']+)` + pattern := `(\?=\s+([^&|']+))|(([<>]?=|[<>])[^&|')]+\/[^&|')]+([&|')]+))` re := regexp.MustCompile(pattern) filter = re.ReplaceAllStringFunc(filter, func(match string) string { - value := strings.TrimSpace(strings.TrimPrefix(match, "?=")) + + comparator := match[:2] + value := strings.TrimSpace(match[2:]) + if match[1] == ' ' { + comparator = match[:1] + } + + var end string + if value[len(value)-1:] == ")" { + end = ")" + value = value[0 : len(value)-1] + } + value = strings.ReplaceAll(value, "'", `\'`) enclosedValue := "'" + value + "'" - return "?= " + enclosedValue + return comparator + " " + enclosedValue + end }) parsedFilter, err := fexpr.Parse(filter)