Skip to content

Commit

Permalink
fix filter_agg
Browse files Browse the repository at this point in the history
  • Loading branch information
ryandotsmith committed Aug 9, 2024
1 parent a475e62 commit 69e8e05
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
25 changes: 10 additions & 15 deletions dig/dig.go
Original file line number Diff line number Diff line change
Expand Up @@ -961,34 +961,29 @@ func (lwc *logWithCtx) get(name string) any {

type filterResults struct {
kind string
used bool
set bool
val bool
}

func (fr *filterResults) add(b bool) {
fr.used = true
if !fr.set {
fr.set = true
fr.val = b
return
}
switch fr.kind {
case "and":
if !b {
fr.val = false
}
fr.val = fr.val && b
default:
if b {
fr.val = true
}
fr.val = fr.val || b
}
}

func (fr *filterResults) accept() bool {
if !fr.used {
if !fr.set {
return true
}
switch fr.kind {
case "and":
return !fr.val
default:
return fr.val
}
return fr.val
}

func (ig Integration) processTx(rows [][]any, lwc *logWithCtx, pgmut *sync.Mutex, pg wpg.Conn) ([][]any, bool, error) {
Expand Down
22 changes: 22 additions & 0 deletions dig/dig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,3 +463,25 @@ func TestFilter(t *testing.T) {
tc.WantGot(t, c.want, frs.accept())
}
}

func TestFilterResults(t *testing.T) {
cases := []struct {
kind string
want bool
input []bool
}{
{"and", false, []bool{true, false}},
{"and", false, []bool{false, false}},
{"and", true, []bool{true, true}},
{"or", true, []bool{true, false}},
{"or", true, []bool{true, true}},
{"or", false, []bool{false, false}},
}
for _, c := range cases {
frs := filterResults{kind: c.kind}
for _, b := range c.input {
frs.add(b)
}
tc.WantGot(t, c.want, frs.accept())
}
}

0 comments on commit 69e8e05

Please sign in to comment.