-
Notifications
You must be signed in to change notification settings - Fork 0
/
context_fragment.go
74 lines (67 loc) · 1.63 KB
/
context_fragment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package sqlf
import (
"errors"
"fmt"
"strings"
)
// fragment returns the fragment context.
//
// it's used usually in the implementation of a FragmentBuilder,
// most users don't need to care about it.
func (c *Context) fragment() (*FragmentContext, bool) {
return contextValue(c, func(c *Context) (*FragmentContext, bool) {
return c.frag, c.frag != nil
})
}
func (c *Context) mustFragment() (*FragmentContext, error) {
fc, ok := c.fragment()
if !ok {
return nil, errors.New("no fragment context")
}
return fc, nil
}
// contextWithFragment returns a new context with the fragment.
func contextWithFragment(ctx *Context, f *Fragment) *Context {
c, _ := contextWith(ctx, func(c *Context) error {
c.frag = newFragmentContext(f)
return nil
})
return c
}
// FragmentContext is the context for current fragment building.
type FragmentContext struct {
Fragment *Fragment
Args Properties
Fragments Properties
}
func newFragmentContext(f *Fragment) *FragmentContext {
if f == nil {
return &FragmentContext{}
}
return &FragmentContext{
Fragment: f,
Args: NewArgsProperties(f.Args...),
Fragments: NewFragmentProperties(f.Fragments...),
}
}
// checkUsage checks if all args and properties are used.
func (c *FragmentContext) checkUsage() error {
if c == nil {
return nil
}
msgs := make([]string, 0, 5)
if err := c.Args.checkUsage(); err != nil {
msgs = append(msgs, fmt.Sprintf(
"args %s", err.Error(),
))
}
if err := c.Fragments.checkUsage(); err != nil {
msgs = append(msgs, fmt.Sprintf(
"properties %s", err.Error(),
))
}
if len(msgs) > 0 {
return errors.New(strings.Join(msgs, "; "))
}
return nil
}