Properly escape null character in the consumer too
This commit is contained in:
parent
693ae1ba0a
commit
ffa2faa420
4 changed files with 20 additions and 19 deletions
14
util/fix/postgres_json.go
Normal file
14
util/fix/postgres_json.go
Normal file
|
@ -0,0 +1,14 @@
|
|||
package fix
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
var postgresFixRegexp = regexp.MustCompile(`([^\\](\\\\)*)(\\u0000)+`)
|
||||
|
||||
func EscapeNullCharForPostgres(b []byte) []byte {
|
||||
return postgresFixRegexp.ReplaceAllFunc(b, func(b []byte) []byte {
|
||||
return bytes.ReplaceAll(b, []byte(`\u0000`), []byte(`<0x00>`))
|
||||
})
|
||||
}
|
26
util/fix/postgres_json_test.go
Normal file
26
util/fix/postgres_json_test.go
Normal file
|
@ -0,0 +1,26 @@
|
|||
package fix
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPostgresFix(t *testing.T) {
|
||||
type testCase struct{ input, want string }
|
||||
|
||||
cases := []testCase{
|
||||
{`"a"`, `"a"`},
|
||||
{`"\u0000"`, `"<0x00>"`},
|
||||
{`"description":"\u0000"`, `"description":"<0x00>"`},
|
||||
{`"\\u0000"`, `"\\u0000"`},
|
||||
{`"\\\u0000"`, `"\\<0x00>"`},
|
||||
{`\n\n\u0000\u0000 \u0000\u0000\u0000\u0000 \u0000\u0000\u0000\u0000\u0000`,
|
||||
`\n\n<0x00><0x00> <0x00><0x00><0x00><0x00> <0x00><0x00><0x00><0x00><0x00>`},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
got := EscapeNullCharForPostgres([]byte(tc.input))
|
||||
if string(got) != tc.want {
|
||||
t.Errorf("escapeNullCharForPostgres(%s) = %s, want %s", tc.input, string(got), tc.want)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue