• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stephenafamo / bob / 19668632728

25 Nov 2025 11:54AM UTC coverage: 42.301%. Remained the same
19668632728

push

github

web-flow
Merge pull request #589 from stephenafamo/string-writer

Use io.StringWriter instead of io.Writer for efficiency

196 of 295 new or added lines in 67 files covered. (66.44%)

9 existing lines in 5 files now uncovered.

9750 of 23049 relevant lines covered (42.3%)

569.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.08
/expr/case.go
1
package expr
2

3
import (
4
        "context"
5
        "errors"
6
        "io"
7

8
        "github.com/stephenafamo/bob"
9
)
10

11
type (
12
        caseExpr struct {
13
                whens    []when
14
                elseExpr bob.Expression
15
        }
16
        when struct {
17
                condition bob.Expression
18
                then      bob.Expression
19
        }
20
)
21

22
func (c caseExpr) WriteSQL(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
12✔
23
        var args []any
12✔
24

12✔
25
        if len(c.whens) == 0 {
12✔
26
                return nil, errors.New("case must have at least one when expression")
×
27
        }
×
28

29
        w.WriteString("CASE")
12✔
30
        for _, when := range c.whens {
24✔
31
                w.WriteString(" WHEN ")
12✔
32
                whenArgs, err := when.condition.WriteSQL(ctx, w, d, start+len(args))
12✔
33
                if err != nil {
12✔
34
                        return nil, err
×
35
                }
×
36
                args = append(args, whenArgs...)
12✔
37

12✔
38
                w.WriteString(" THEN ")
12✔
39
                thenArgs, err := when.then.WriteSQL(ctx, w, d, start+len(args))
12✔
40
                if err != nil {
12✔
41
                        return nil, err
×
42
                }
×
43
                args = append(args, thenArgs...)
12✔
44
        }
45

46
        if c.elseExpr != nil {
18✔
47
                w.WriteString(" ELSE ")
6✔
48
                elseArgs, err := c.elseExpr.WriteSQL(ctx, w, d, start+len(args))
6✔
49
                if err != nil {
6✔
50
                        return nil, err
×
51
                }
×
52
                args = append(args, elseArgs...)
6✔
53
        }
54
        w.WriteString(" END")
12✔
55

12✔
56
        return args, nil
12✔
57
}
58

59
type CaseChain[T bob.Expression, B builder[T]] func() caseExpr
60

61
func NewCase[T bob.Expression, B builder[T]]() CaseChain[T, B] {
12✔
62
        return CaseChain[T, B](func() caseExpr { return caseExpr{} })
24✔
63
}
64

NEW
65
func (cc CaseChain[T, B]) WriteSQL(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
×
66
        return cc().WriteSQL(ctx, w, d, start)
×
67
}
×
68

69
func (cc CaseChain[T, B]) When(condition, then bob.Expression) CaseChain[T, B] {
12✔
70
        c := cc()
12✔
71
        c.whens = append(c.whens, when{condition: condition, then: then})
12✔
72
        return CaseChain[T, B](func() caseExpr { return c })
24✔
73
}
74

75
func (cc CaseChain[T, B]) Else(then bob.Expression) T {
6✔
76
        c := cc()
6✔
77
        c.elseExpr = then
6✔
78
        return X[T, B](c)
6✔
79
}
6✔
80

81
func (cc CaseChain[T, B]) End() T {
6✔
82
        return X[T, B](cc())
6✔
83
}
6✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc