-
Notifications
You must be signed in to change notification settings - Fork 0
/
append_jsonb.go
49 lines (44 loc) · 882 Bytes
/
append_jsonb.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
package types
import "github.com/jamscloud/pg/v10/internal/parser"
func AppendJSONB(b, jsonb []byte, flags int) []byte {
if hasFlag(flags, arrayFlag) {
b = append(b, '"')
} else if hasFlag(flags, quoteFlag) {
b = append(b, '\'')
}
p := parser.New(jsonb)
for p.Valid() {
c := p.Read()
switch c {
case '"':
if hasFlag(flags, arrayFlag) {
b = append(b, '\\')
}
b = append(b, '"')
case '\'':
if hasFlag(flags, quoteFlag) {
b = append(b, '\'')
}
b = append(b, '\'')
case '\000':
continue
case '\\':
if p.SkipBytes([]byte("u0000")) {
b = append(b, "\\\\u0000"...)
} else {
b = append(b, '\\')
if p.Valid() {
b = append(b, p.Read())
}
}
default:
b = append(b, c)
}
}
if hasFlag(flags, arrayFlag) {
b = append(b, '"')
} else if hasFlag(flags, quoteFlag) {
b = append(b, '\'')
}
return b
}