mirror of
https://github.com/safing/portbase
synced 2026-04-28 11:50:02 +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
60
database/query/condition-stringslice.go
Normal file
60
database/query/condition-stringslice.go
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
package query
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/Safing/portbase/utils"
|
||||
)
|
||||
|
||||
type stringSliceCondition struct {
|
||||
key string
|
||||
operator uint8
|
||||
value []string
|
||||
}
|
||||
|
||||
func newStringSliceCondition(key string, operator uint8, value interface{}) *stringSliceCondition {
|
||||
|
||||
switch v := value.(type) {
|
||||
case string:
|
||||
parsedValue := strings.Split(v, ",")
|
||||
if len(parsedValue) < 2 {
|
||||
return &stringSliceCondition{
|
||||
key: fmt.Sprintf("could not parse \"%s\" to []string", v),
|
||||
operator: errorPresent,
|
||||
}
|
||||
}
|
||||
return &stringSliceCondition{
|
||||
key: key,
|
||||
operator: operator,
|
||||
value: parsedValue,
|
||||
}
|
||||
default:
|
||||
return &stringSliceCondition{
|
||||
key: fmt.Sprintf("incompatible value %v for []string", value),
|
||||
operator: errorPresent,
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (c *stringSliceCondition) complies(f Fetcher) bool {
|
||||
comp, ok := f.GetString(c.key)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
switch c.operator {
|
||||
case In:
|
||||
return utils.StringInSlice(c.value, comp)
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (c *stringSliceCondition) check() error {
|
||||
if c.operator == errorPresent {
|
||||
return fmt.Errorf("could not parse \"%s\" to []string", c.key)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue