code.gitea.io/gitea@v1.22.3/modules/indexer/internal/meilisearch/filter.go (about) 1 // Copyright 2023 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package meilisearch 5 6 import ( 7 "fmt" 8 "strings" 9 ) 10 11 // Filter represents a filter for meilisearch queries. 12 // It's just a simple wrapper around a string. 13 // DO NOT assume that it is a complete implementation. 14 type Filter interface { 15 Statement() string 16 } 17 18 type FilterAnd struct { 19 filters []Filter 20 } 21 22 func (f *FilterAnd) Statement() string { 23 var statements []string 24 for _, filter := range f.filters { 25 if s := filter.Statement(); s != "" { 26 statements = append(statements, fmt.Sprintf("(%s)", s)) 27 } 28 } 29 return strings.Join(statements, " AND ") 30 } 31 32 func (f *FilterAnd) And(filter Filter) *FilterAnd { 33 f.filters = append(f.filters, filter) 34 return f 35 } 36 37 type FilterOr struct { 38 filters []Filter 39 } 40 41 func (f *FilterOr) Statement() string { 42 var statements []string 43 for _, filter := range f.filters { 44 if s := filter.Statement(); s != "" { 45 statements = append(statements, fmt.Sprintf("(%s)", s)) 46 } 47 } 48 return strings.Join(statements, " OR ") 49 } 50 51 func (f *FilterOr) Or(filter Filter) *FilterOr { 52 f.filters = append(f.filters, filter) 53 return f 54 } 55 56 type FilterIn string 57 58 // NewFilterIn creates a new FilterIn. 59 // It supports int64 only, to avoid extra works to handle strings with special characters. 60 func NewFilterIn[T int64](field string, values ...T) FilterIn { 61 if len(values) == 0 { 62 return "" 63 } 64 vs := make([]string, len(values)) 65 for i, v := range values { 66 vs[i] = fmt.Sprintf("%v", v) 67 } 68 return FilterIn(fmt.Sprintf("%s IN [%v]", field, strings.Join(vs, ", "))) 69 } 70 71 func (f FilterIn) Statement() string { 72 return string(f) 73 } 74 75 type FilterEq string 76 77 // NewFilterEq creates a new FilterEq. 78 // It supports int64 and bool only, to avoid extra works to handle strings with special characters. 79 func NewFilterEq[T bool | int64](field string, value T) FilterEq { 80 return FilterEq(fmt.Sprintf("%s = %v", field, value)) 81 } 82 83 func (f FilterEq) Statement() string { 84 return string(f) 85 } 86 87 type FilterNot string 88 89 func NewFilterNot(filter Filter) FilterNot { 90 return FilterNot(fmt.Sprintf("NOT (%s)", filter.Statement())) 91 } 92 93 func (f FilterNot) Statement() string { 94 return string(f) 95 } 96 97 type FilterGte string 98 99 // NewFilterGte creates a new FilterGte. 100 // It supports int64 only, to avoid extra works to handle strings with special characters. 101 func NewFilterGte[T int64](field string, value T) FilterGte { 102 return FilterGte(fmt.Sprintf("%s >= %v", field, value)) 103 } 104 105 func (f FilterGte) Statement() string { 106 return string(f) 107 } 108 109 type FilterLte string 110 111 // NewFilterLte creates a new FilterLte. 112 // It supports int64 only, to avoid extra works to handle strings with special characters. 113 func NewFilterLte[T int64](field string, value T) FilterLte { 114 return FilterLte(fmt.Sprintf("%s <= %v", field, value)) 115 } 116 117 func (f FilterLte) Statement() string { 118 return string(f) 119 }