mirror of
https://github.com/safing/portbase
synced 2025-09-10 15:34:26 +00:00
Add query package with first set of conditions and tests
This commit is contained in:
parent
1bf7e42d8a
commit
6ed50f34fb
16 changed files with 933 additions and 0 deletions
56
database/query/condition-string.go
Normal file
56
database/query/condition-string.go
Normal file
|
@ -0,0 +1,56 @@
|
|||
package query
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type stringCondition struct {
|
||||
key string
|
||||
operator uint8
|
||||
value string
|
||||
}
|
||||
|
||||
func newStringCondition(key string, operator uint8, value interface{}) *stringCondition {
|
||||
switch v := value.(type) {
|
||||
case string:
|
||||
return &stringCondition{
|
||||
key: key,
|
||||
operator: operator,
|
||||
value: v,
|
||||
}
|
||||
default:
|
||||
return &stringCondition{
|
||||
key: fmt.Sprintf("incompatible value %v for string", value),
|
||||
operator: errorPresent,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *stringCondition) complies(f Fetcher) bool {
|
||||
comp, ok := f.GetString(c.key)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
switch c.operator {
|
||||
case Matches:
|
||||
return c.value == comp
|
||||
case Contains:
|
||||
return strings.Contains(comp, c.value)
|
||||
case StartsWith:
|
||||
return strings.HasPrefix(comp, c.value)
|
||||
case EndsWith:
|
||||
return strings.HasSuffix(comp, c.value)
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (c *stringCondition) check() error {
|
||||
if c.operator == errorPresent {
|
||||
return errors.New(c.key)
|
||||
}
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue