package httpapi import ( "fmt" "sort" "strings" ) // sys_query is the filter a list operation takes: conditions joined by ^, all // of which a row must meet. A condition names a field the operation offers and // compares the row's value for it as text: field=value keeps the rows that // equal it, field!=value the rows that differ, and fieldLIKEtext the rows whose // value contains text. Every comparison ignores case. // Fields names what a list's filter may compare: each field a condition can // name, reading one row's value as text. type Fields[T any] map[string]func(T) string // op is how a condition compares a row's value. type op int const ( opEqual op = iota opNotEqual opLike ) type condition struct { field string op op value string } // Filter keeps the rows whose values meet every condition in raw, a list's // sys_query. An empty raw keeps every row. A malformed condition, or a field // the list does not offer, is an error for the caller: it names what the list // accepts, so a list answers a bad filter with a 400 rather than an empty page. // Conditions apply to the rows the operation already read. func Filter[T any](raw string, fields Fields[T], rows []T) ([]T, error) { terms := strings.Split(strings.TrimSpace(raw), "^") if len(terms) == 1 && terms[0] == "" { return rows, nil } conds := make([]condition, 0, len(terms)) for _, term := range terms { c, err := parseCondition(strings.TrimSpace(term), fields) if err != nil { return nil, err } conds = append(conds, c) } out := make([]T, 0, len(rows)) for _, row := range rows { if meets(conds, fields, row) { out = append(out, row) } } return out, nil } func parseCondition[T any](term string, fields Fields[T]) (condition, error) { field, value, o, ok := splitOperator(term) if !ok { if term == "" { return condition{}, fmt.Errorf("sys_query: an empty condition; join conditions with ^") } return condition{}, fmt.Errorf("sys_query: %q has no operator; write field=value, field!=value or fieldLIKEtext", term) } if _, offered := fields[field]; !offered { return condition{}, fmt.Errorf("sys_query: %q is not a field here; filter on %s", field, strings.Join(fieldNames(fields), ", ")) } return condition{field: field, op: o, value: value}, nil } // splitOperator splits a condition at the first operator it contains, so a // value may itself contain the characters of another one. func splitOperator(term string) (field, value string, o op, ok bool) { at, width := -1, 0 o = opEqual consider := func(index, size int, which op) { if index <= 0 || (at >= 0 && index >= at) { return } at, width, o = index, size, which } consider(strings.Index(term, "!="), 2, opNotEqual) consider(strings.Index(term, "="), 1, opEqual) consider(indexFold(term, "LIKE"), 4, opLike) if at < 0 { return "", "", opEqual, false } return strings.TrimSpace(term[:at]), strings.TrimSpace(term[at+width:]), o, true } func meets[T any](conds []condition, fields Fields[T], row T) bool { for _, c := range conds { have := strings.ToLower(fields[c.field](row)) want := strings.ToLower(c.value) switch c.op { case opEqual: if have != want { return false } case opNotEqual: if have == want { return false } case opLike: if !strings.Contains(have, want) { return false } } } return true } // indexFold finds where ASCII needle appears in s, ignoring case. It compares // windows of the original string so an offset stays a byte offset into s, which // upper-casing would not guarantee for a field name outside ASCII. func indexFold(s, needle string) int { for i := 0; i+len(needle) <= len(s); i++ { if strings.EqualFold(s[i:i+len(needle)], needle) { return i } } return -1 } // fieldNames lists a list's filterable fields in a stable order, for the error // a bad filter gets. func fieldNames[T any](fields Fields[T]) []string { names := make([]string, 0, len(fields)) for name := range fields { names = append(names, name) } sort.Strings(names) return names }