Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query: condition builder #101

Merged
merged 8 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Improve filter building capability
  • Loading branch information
marino39 committed Apr 15, 2023
commit 178fe1df78766fadcee3a9c100cfa906a22aae2b
90 changes: 90 additions & 0 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,98 @@ import (
"sort"

"github.com/go-bond/bond/utils"
"github.com/stretchr/testify/assert"
"golang.org/x/exp/constraints"
)

// Evaluable is the interface used to simplify Query.Filter usage
type Evaluable[R any] interface {
Eval(r R) bool
}

type Value[R any, V any] func(r R) V

type Eq[R any, V any] struct {
Record Value[R, V]
Equal V
}

func (e *Eq[R, V]) Eval(r R) bool {
return assert.ObjectsAreEqual(e.Record(r), e.Equal)
}

type Gt[R any, V constraints.Ordered] struct {
Record Value[R, V]
Greater V
}

func (g *Gt[R, V]) Eval(r R) bool {
return g.Record(r) > g.Greater
}

type Gte[R any, V constraints.Ordered] struct {
Record Value[R, V]
GreaterEqual V
}

func (g *Gte[R, V]) Eval(r R) bool {
return g.Record(r) >= g.GreaterEqual
}

type Lt[R any, V constraints.Ordered] struct {
Record Value[R, V]
Less V
}

func (l *Lt[R, V]) Eval(r R) bool {
return l.Record(r) < l.Less
}

type Lte[R any, V constraints.Ordered] struct {
Record Value[R, V]
LessEqual V
}

func (l *Lte[R, V]) Eval(r R) bool {
return l.Record(r) <= l.LessEqual
}

type and[R any] []any

func (a *and[R]) Eval(r R) bool {
evalReturn := true
for _, evaluable := range *a {
evalReturn = evalReturn && evaluable.(Evaluable[R]).Eval(r)
}
return evalReturn
}

func And[R any](evalList ...Evaluable[R]) Evaluable[R] {
var e and[R]
for _, eval := range evalList {
e = append(e, eval)
}
return &e
}

type or[R any] []any

func (o *or[R]) Eval(r R) bool {
evalReturn := false
for _, evaluable := range *o {
evalReturn = evalReturn || evaluable.(Evaluable[R]).Eval(r)
}
return evalReturn
}

func Or[R any](evalList ...Evaluable[R]) Evaluable[R] {
var e or[R]
for _, eval := range evalList {
e = append(e, eval)
}
return &e
}

// FilterFunc is the function template to be used for record filtering.
type FilterFunc[R any] func(r R) bool

Expand Down
109 changes: 109 additions & 0 deletions query_eval_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package bond

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestBond_Eq(t *testing.T) {
eq := &Eq[*TokenBalance, uint32]{func(tb *TokenBalance) uint32 {
return tb.AccountID
}, 1}

evaluable := Evaluable[*TokenBalance](eq)

assert.True(t, evaluable.Eval(&TokenBalance{AccountID: 1}))
assert.False(t, evaluable.Eval(&TokenBalance{AccountID: 2}))
}

func TestBond_Gt(t *testing.T) {
gt := &Gt[*TokenBalance, uint32]{func(tb *TokenBalance) uint32 {
return tb.AccountID
}, 0}

evaluable := Evaluable[*TokenBalance](gt)

assert.True(t, evaluable.Eval(&TokenBalance{AccountID: 1}))
assert.False(t, evaluable.Eval(&TokenBalance{AccountID: 0}))
}

func TestBond_Gte(t *testing.T) {
gte := &Gte[*TokenBalance, uint32]{func(tb *TokenBalance) uint32 {
return tb.AccountID
}, 1}

evaluable := Evaluable[*TokenBalance](gte)

assert.True(t, evaluable.Eval(&TokenBalance{AccountID: 1}))
assert.False(t, evaluable.Eval(&TokenBalance{AccountID: 0}))
}

func TestBond_Lt(t *testing.T) {
lt := &Lt[*TokenBalance, uint32]{func(tb *TokenBalance) uint32 {
return tb.AccountID
}, 1}

evaluable := Evaluable[*TokenBalance](lt)

assert.False(t, evaluable.Eval(&TokenBalance{AccountID: 1}))
assert.True(t, evaluable.Eval(&TokenBalance{AccountID: 0}))
}

func TestBond_Lte(t *testing.T) {
lte := &Lte[*TokenBalance, uint32]{func(tb *TokenBalance) uint32 {
return tb.AccountID
}, 0}

evaluable := Evaluable[*TokenBalance](lte)

assert.False(t, evaluable.Eval(&TokenBalance{AccountID: 1}))
assert.True(t, evaluable.Eval(&TokenBalance{AccountID: 0}))
}

func TestBond_And(t *testing.T) {
andCond := &and[*TokenBalance]{
&Gte[*TokenBalance, uint32]{
func(tb *TokenBalance) uint32 {
return tb.AccountID
},
5,
},
&Lt[*TokenBalance, uint32]{
func(tb *TokenBalance) uint32 {
return tb.AccountID
},
6,
},
}

evaluable := Evaluable[*TokenBalance](andCond)

assert.False(t, evaluable.Eval(&TokenBalance{AccountID: 4}))
assert.True(t, evaluable.Eval(&TokenBalance{AccountID: 5}))
assert.False(t, evaluable.Eval(&TokenBalance{AccountID: 6}))
}

func TestBond_Or(t *testing.T) {
orCond := &or[*TokenBalance]{
&Gte[*TokenBalance, uint32]{
func(tb *TokenBalance) uint32 {
return tb.AccountID
},
5,
},
&Lt[*TokenBalance, uint32]{
func(tb *TokenBalance) uint32 {
return tb.AccountID
},
3,
},
}

evaluable := Evaluable[*TokenBalance](orCond)

assert.True(t, evaluable.Eval(&TokenBalance{AccountID: 2}))
assert.False(t, evaluable.Eval(&TokenBalance{AccountID: 3}))
assert.False(t, evaluable.Eval(&TokenBalance{AccountID: 4}))
assert.True(t, evaluable.Eval(&TokenBalance{AccountID: 5}))
}
88 changes: 88 additions & 0 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,94 @@ func TestBond_Query_Where_Offset_Limit_With_Filter(t *testing.T) {
require.Equal(t, 0, len(tokenBalances))
}

func TestBond_Query_Where_Offset_Limit_With_Filter_With_Evaluable(t *testing.T) {
db, TokenBalanceTable, _, _ := setupDatabaseForQuery()
defer tearDownDatabase(db)

tokenBalanceAccount1 := &TokenBalance{
ID: 1,
AccountID: 1,
ContractAddress: "0xtestContract",
AccountAddress: "0xtestAccount",
Balance: 5,
}

tokenBalance2Account1 := &TokenBalance{
ID: 2,
AccountID: 1,
ContractAddress: "0xtestContract2",
AccountAddress: "0xtestAccount",
Balance: 15,
}

tokenBalance3Account1 := &TokenBalance{
ID: 3,
AccountID: 1,
ContractAddress: "0xtestContract3",
AccountAddress: "0xtestAccount",
Balance: 7,
}

tokenBalance1Account2 := &TokenBalance{
ID: 4,
AccountID: 2,
ContractAddress: "0xtestContract",
AccountAddress: "0xtestAccount2",
Balance: 4,
}

err := TokenBalanceTable.Insert(
context.Background(),
[]*TokenBalance{
tokenBalanceAccount1,
tokenBalance2Account1,
tokenBalance3Account1,
tokenBalance1Account2,
},
)
require.NoError(t, err)

var tokenBalances []*TokenBalance

query := TokenBalanceTable.Query().
Filter(func(tb *TokenBalance) bool {
return tb.AccountAddress == "0xtestAccount"
}).
Limit(2)

err = query.Execute(context.Background(), &tokenBalances)
require.Nil(t, err)
require.Equal(t, 2, len(tokenBalances))

assert.Equal(t, tokenBalanceAccount1, tokenBalances[0])
assert.Equal(t, tokenBalance2Account1, tokenBalances[1])

query = TokenBalanceTable.Query().
Filter(func(tb *TokenBalance) bool {
return tb.AccountAddress == "0xtestAccount"
}).
Offset(1).
Limit(2)

err = query.Execute(context.Background(), &tokenBalances)
require.Nil(t, err)
require.Equal(t, 2, len(tokenBalances))

assert.Equal(t, tokenBalance2Account1, tokenBalances[0])
assert.Equal(t, tokenBalance3Account1, tokenBalances[1])

query = TokenBalanceTable.Query().
Filter(func(tb *TokenBalance) bool {
return tb.AccountAddress == "0xtestAccount"
}).
Offset(3).
Limit(2)

err = query.Execute(context.Background(), &tokenBalances)
require.Nil(t, err)
require.Equal(t, 0, len(tokenBalances))
}

func TestBond_Query_Where_Offset_Limit_With_Order(t *testing.T) {
db, TokenBalanceTable, _, _, _ := setupDatabaseForQuery()
defer tearDownDatabase(db)
Expand Down