github.com/safing/portbase@v0.19.5/database/query/condition-bool.go (about) 1 package query 2 3 import ( 4 "errors" 5 "fmt" 6 "strconv" 7 8 "github.com/safing/portbase/database/accessor" 9 ) 10 11 type boolCondition struct { 12 key string 13 operator uint8 14 value bool 15 } 16 17 func newBoolCondition(key string, operator uint8, value interface{}) *boolCondition { 18 var parsedValue bool 19 20 switch v := value.(type) { 21 case bool: 22 parsedValue = v 23 case string: 24 var err error 25 parsedValue, err = strconv.ParseBool(v) 26 if err != nil { 27 return &boolCondition{ 28 key: fmt.Sprintf("could not parse \"%s\" to bool: %s", v, err), 29 operator: errorPresent, 30 } 31 } 32 default: 33 return &boolCondition{ 34 key: fmt.Sprintf("incompatible value %v for int64", value), 35 operator: errorPresent, 36 } 37 } 38 39 return &boolCondition{ 40 key: key, 41 operator: operator, 42 value: parsedValue, 43 } 44 } 45 46 func (c *boolCondition) complies(acc accessor.Accessor) bool { 47 comp, ok := acc.GetBool(c.key) 48 if !ok { 49 return false 50 } 51 52 switch c.operator { 53 case Is: 54 return comp == c.value 55 default: 56 return false 57 } 58 } 59 60 func (c *boolCondition) check() error { 61 if c.operator == errorPresent { 62 return errors.New(c.key) 63 } 64 return nil 65 } 66 67 func (c *boolCondition) string() string { 68 return fmt.Sprintf("%s %s %t", escapeString(c.key), getOpName(c.operator), c.value) 69 }