github.com/safing/portbase@v0.19.5/database/query/condition-string.go (about) 1 package query 2 3 import ( 4 "errors" 5 "fmt" 6 "strings" 7 8 "github.com/safing/portbase/database/accessor" 9 ) 10 11 type stringCondition struct { 12 key string 13 operator uint8 14 value string 15 } 16 17 func newStringCondition(key string, operator uint8, value interface{}) *stringCondition { 18 switch v := value.(type) { 19 case string: 20 return &stringCondition{ 21 key: key, 22 operator: operator, 23 value: v, 24 } 25 default: 26 return &stringCondition{ 27 key: fmt.Sprintf("incompatible value %v for string", value), 28 operator: errorPresent, 29 } 30 } 31 } 32 33 func (c *stringCondition) complies(acc accessor.Accessor) bool { 34 comp, ok := acc.GetString(c.key) 35 if !ok { 36 return false 37 } 38 39 switch c.operator { 40 case SameAs: 41 return c.value == comp 42 case Contains: 43 return strings.Contains(comp, c.value) 44 case StartsWith: 45 return strings.HasPrefix(comp, c.value) 46 case EndsWith: 47 return strings.HasSuffix(comp, c.value) 48 default: 49 return false 50 } 51 } 52 53 func (c *stringCondition) check() error { 54 if c.operator == errorPresent { 55 return errors.New(c.key) 56 } 57 return nil 58 } 59 60 func (c *stringCondition) string() string { 61 return fmt.Sprintf("%s %s %s", escapeString(c.key), getOpName(c.operator), escapeString(c.value)) 62 }