-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlb_array.go
115 lines (95 loc) · 3.26 KB
/
sqlb_array.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package sqlb
import "database/sql/driver"
/*
Intermediary tool for implementing SQL array encoding. Has the same behavior as
`CommaAppender`, but the text output is always enclosed in `{}`.
*/
type ArrayAppender[A AppenderTo] []A
/*
Implement `fmt.Stringer`. Same as `CommaAppender.String`, but the output is
always enclosed in `{}`.
*/
func (self ArrayAppender[_]) String() string { return AppenderString(&self) }
/*
Implement `AppenderTo`. Same as `CommaAppender.AppendTo`, but the output is always
enclosed in `{}`.
*/
func (self ArrayAppender[A]) AppendTo(buf []byte) []byte {
buf = append(buf, `{`...)
buf = CommaAppender[A](self).AppendTo(buf)
buf = append(buf, `}`...)
return buf
}
func (self ArrayAppender[_]) Get() any { return self.String() }
func (self ArrayAppender[_]) Value() (driver.Value, error) { return self.Get(), nil }
/*
Intermediary tool for implementing SQL array encoding. Combines multiple
arbitrary text encoders. On demand (on a call to `.AppendTo` or `.String`),
combines their text representations, separating them with a comma, while
skipping any empty representations. The output will never contain a dangling
leading comma, double comma, or leading trailing comma, unless they were
explicitly generated by the inner encoders. Compare `SliceCommaAppender`
which takes an arbitrary slice.
*/
type CommaAppender[A AppenderTo] []A
// Implement `fmt.Stringer` by calling `.AppendTo`.
func (self CommaAppender[_]) String() string { return AppenderString(&self) }
/*
Implement `AppenderTo`. Appends comma-separated text representations of the inner
encoders to the output buffer, skipping any empty representations.
*/
func (self CommaAppender[_]) AppendTo(buf []byte) []byte {
var found bool
for _, val := range self {
if (found && TryAppendWith(&buf, `,`, val)) || TryAppendWith(&buf, ``, val) {
found = true
}
}
return buf
}
/*
Intermediary tool for implementing SQL array encoding. The inner value must be
either nil, a slice/array, or a pointer to a slice/array, where each element
must implement `AppenderTo`. When `.AppendTo` or `.String` is called, this combines
the text representations of the elements, separating them with a comma, while
skipping any empty representations. The output will never contain a dangling
leading comma, double comma, or leading trailing comma, unless they were
explicitly generated by the inner encoders. Compare `CommaAppender` which
itself is a slice.
*/
type SliceCommaAppender [1]any
// Implement `fmt.Stringer` by calling `.AppendTo`.
func (self SliceCommaAppender) String() string { return AppenderString(&self) }
/*
Implement `AppenderTo`. Appends comma-separated text representations of the inner
encoders to the output buffer, skipping any empty representations.
*/
func (self SliceCommaAppender) AppendTo(buf []byte) []byte {
if self[0] == nil {
return buf
}
val, _ := self[0].(AppenderTo)
if val != nil {
return val.AppendTo(buf)
}
src := valueOf(self[0])
if !src.IsValid() {
return buf
}
var found bool
for ind := range counter(src.Len()) {
elem := src.Index(ind)
if !elem.IsValid() {
continue
}
iface := elem.Interface()
if iface == nil {
continue
}
val := iface.(AppenderTo)
if (found && TryAppendWith(&buf, `,`, val)) || TryAppendWith(&buf, ``, val) {
found = true
}
}
return buf
}