Skip to content
This repository was archived by the owner on Jun 14, 2019. It is now read-only.

Commit 0c156df

Browse files
authored
fix bug on condif (#54)
1 parent 2d4759f commit 0c156df

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

cond_if.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,8 @@ func (condIf condIf) Or(conds ...Cond) Cond {
4242
}
4343

4444
func (condIf condIf) IsValid() bool {
45-
return condIf.condTrue != nil
45+
if condIf.condition {
46+
return condIf.condTrue != nil
47+
}
48+
return condIf.condFalse != nil
4649
}

cond_if_test.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,28 @@ import (
1111
)
1212

1313
func TestCond_If(t *testing.T) {
14-
var cond = If(1 > 0, Eq{"a": 1}, Eq{"b": 1})
15-
sql, err := ToBoundSQL(cond)
14+
cond1 := If(1 > 0, Eq{"a": 1}, Eq{"b": 1})
15+
sql, err := ToBoundSQL(cond1)
1616
assert.NoError(t, err)
1717
assert.EqualValues(t, "a=1", sql)
1818

19-
cond = If(1 < 0, Eq{"a": 1}, Eq{"b": 1})
20-
sql, err = ToBoundSQL(cond)
19+
cond2 := If(1 < 0, Eq{"a": 1}, Eq{"b": 1})
20+
sql, err = ToBoundSQL(cond2)
2121
assert.NoError(t, err)
2222
assert.EqualValues(t, "b=1", sql)
2323

24-
cond = If(1 > 0, cond, Eq{"c": 1})
25-
sql, err = ToBoundSQL(cond)
24+
cond3 := If(1 > 0, cond2, Eq{"c": 1})
25+
sql, err = ToBoundSQL(cond3)
2626
assert.NoError(t, err)
2727
assert.EqualValues(t, "b=1", sql)
28+
29+
cond4 := If(2 < 0, Eq{"d": "a"})
30+
sql, err = ToBoundSQL(cond4)
31+
assert.NoError(t, err)
32+
assert.EqualValues(t, "", sql)
33+
34+
cond5 := And(cond1, cond2, cond3, cond4)
35+
sql, err = ToBoundSQL(cond5)
36+
assert.NoError(t, err)
37+
assert.EqualValues(t, "a=1 AND b=1 AND b=1", sql)
2838
}

0 commit comments

Comments
 (0)