Skip to content

Commit

Permalink
add logical operators
Browse files Browse the repository at this point in the history
  • Loading branch information
oussama4 committed Oct 31, 2021
1 parent ea99e8b commit 81853dd
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions op.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ const (
OpGTE // >=
OpLT // <
OpLTE // <=

// Logical operation
OpAND // AND
OpOR // OR
OpNot // NOT
)

var ops = [...]string{
Expand All @@ -19,6 +24,9 @@ var ops = [...]string{
OpGTE: " >= ",
OpLT: " < ",
OpLTE: " <= ",
OpAND: " AND ",
OpOR: " OR ",
OpNot: " NOT ",
}

// writeComp writes a comparaison operator
Expand Down Expand Up @@ -60,3 +68,38 @@ func Lt(column string, value interface{}) BuildFunc {
func Lte(column string, value interface{}) BuildFunc {
return writeComp(OpLTE, column, value)
}

// buildLog build a logical operator
func buildLog(op Op, fns ...BuildFunc) BuildFunc {
bf := func(b *Builder) {
for i, fn := range fns {
if i > 0 || op == OpNot {
b.WriteString(ops[op])
}

b.WriteString("(")
fn(b)
b.WriteString(")")

if op == OpNot {
break
}
}
}
return bf
}

// And builds an `AND` operator
func And(fns ...BuildFunc) BuildFunc {
return buildLog(OpAND, fns...)
}

// Or builds an `OR` operator
func Or(fns ...BuildFunc) BuildFunc {
return buildLog(OpOR, fns...)
}

// Not builds a `NOT` operator
func Not(fn BuildFunc) BuildFunc {
return buildLog(OpNot, fn)
}

0 comments on commit 81853dd

Please sign in to comment.