cmp: token builder

This commit is contained in:
Michał Matczuk
2017-08-30 10:27:18 +02:00
parent 2e903a54e0
commit ea8a9e9845
2 changed files with 196 additions and 0 deletions

114
qb/token.go Normal file
View File

@@ -0,0 +1,114 @@
package qb
import (
"fmt"
"strings"
)
// Token creates a new TokenBuilder.
func Token(columns ...string) TokenBuilder {
return TokenBuilder(columns)
}
// TokenBuilder helps implement pagination using token function.
type TokenBuilder []string
// Eq produces token(column)=token(?).
func (t TokenBuilder) Eq() Cmp {
return Cmp{
op: eq,
fn: "token",
column: fmt.Sprint("token(", strings.Join(t, ","), ")"),
names: t,
}
}
// EqNamed produces token(column)=token(?) with a custom parameter name.
func (t TokenBuilder) EqNamed(names ...string) Cmp {
return Cmp{
op: eq,
fn: "token",
column: fmt.Sprint("token(", strings.Join(t, ","), ")"),
names: names,
}
}
// Lt produces token(column)<token(?).
func (t TokenBuilder) Lt() Cmp {
return Cmp{
op: lt,
fn: "token",
column: fmt.Sprint("token(", strings.Join(t, ","), ")"),
names: t,
}
}
// LtNamed produces token(column)<token(?) with a custom parameter name.
func (t TokenBuilder) LtNamed(names ...string) Cmp {
return Cmp{
op: lt,
fn: "token",
column: fmt.Sprint("token(", strings.Join(t, ","), ")"),
names: names,
}
}
// LtOrEq produces token(column)<=token(?).
func (t TokenBuilder) LtOrEq() Cmp {
return Cmp{
op: leq,
fn: "token",
column: fmt.Sprint("token(", strings.Join(t, ","), ")"),
names: t,
}
}
// LtOrEqNamed produces token(column)<=token(?) with a custom parameter name.
func (t TokenBuilder) LtOrEqNamed(names ...string) Cmp {
return Cmp{
op: leq,
fn: "token",
column: fmt.Sprint("token(", strings.Join(t, ","), ")"),
names: names,
}
}
// Gt produces token(column)>token(?).
func (t TokenBuilder) Gt() Cmp {
return Cmp{
op: gt,
fn: "token",
column: fmt.Sprint("token(", strings.Join(t, ","), ")"),
names: t,
}
}
// GtNamed produces token(column)>token(?) with a custom parameter name.
func (t TokenBuilder) GtNamed(names ...string) Cmp {
return Cmp{
op: gt,
fn: "token",
column: fmt.Sprint("token(", strings.Join(t, ","), ")"),
names: names,
}
}
// GtOrEq produces token(column)>=token(?).
func (t TokenBuilder) GtOrEq() Cmp {
return Cmp{
op: geq,
fn: "token",
column: fmt.Sprint("token(", strings.Join(t, ","), ")"),
names: t,
}
}
// GtOrEqNamed produces token(column)>=token(?) with a custom parameter name.
func (t TokenBuilder) GtOrEqNamed(names ...string) Cmp {
return Cmp{
op: geq,
fn: "token",
column: fmt.Sprint("token(", strings.Join(t, ","), ")"),
names: names,
}
}

82
qb/token_test.go Normal file
View File

@@ -0,0 +1,82 @@
package qb
import (
"bytes"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestToken(t *testing.T) {
table := []struct {
C Cmp
S string
N []string
}{
// Basic comparators
{
C: Token("a", "b").Eq(),
S: "token(a,b)=token(?,?)",
N: []string{"a", "b"},
},
{
C: Token("a", "b").Lt(),
S: "token(a,b)<token(?,?)",
N: []string{"a", "b"},
},
{
C: Token("a", "b").LtOrEq(),
S: "token(a,b)<=token(?,?)",
N: []string{"a", "b"},
},
{
C: Token("a", "b").Gt(),
S: "token(a,b)>token(?,?)",
N: []string{"a", "b"},
},
{
C: Token("a", "b").GtOrEq(),
S: "token(a,b)>=token(?,?)",
N: []string{"a", "b"},
},
// Custom bind names
{
C: Token("a", "b").EqNamed("c", "d"),
S: "token(a,b)=token(?,?)",
N: []string{"c", "d"},
},
{
C: Token("a", "b").LtNamed("c", "d"),
S: "token(a,b)<token(?,?)",
N: []string{"c", "d"},
},
{
C: Token("a", "b").LtOrEqNamed("c", "d"),
S: "token(a,b)<=token(?,?)",
N: []string{"c", "d"},
},
{
C: Token("a", "b").GtNamed("c", "d"),
S: "token(a,b)>token(?,?)",
N: []string{"c", "d"},
},
{
C: Token("a", "b").GtOrEqNamed("c", "d"),
S: "token(a,b)>=token(?,?)",
N: []string{"c", "d"},
},
}
buf := bytes.Buffer{}
for _, test := range table {
buf.Reset()
name := test.C.writeCql(&buf)
if diff := cmp.Diff(test.S, buf.String()); diff != "" {
t.Error(diff)
}
if diff := cmp.Diff(test.N, name); diff != "" {
t.Error(diff)
}
}
}