From ea8a9e98457500b1fe73a6578aed91f174053382 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Matczuk?= Date: Wed, 30 Aug 2017 10:27:18 +0200 Subject: [PATCH] cmp: token builder --- qb/token.go | 114 +++++++++++++++++++++++++++++++++++++++++++++++ qb/token_test.go | 82 ++++++++++++++++++++++++++++++++++ 2 files changed, 196 insertions(+) create mode 100644 qb/token.go create mode 100644 qb/token_test.go diff --git a/qb/token.go b/qb/token.go new file mode 100644 index 0000000..dc32cc1 --- /dev/null +++ b/qb/token.go @@ -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) 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, + } +} diff --git a/qb/token_test.go b/qb/token_test.go new file mode 100644 index 0000000..58146fc --- /dev/null +++ b/qb/token_test.go @@ -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").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").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) + } + } +}