Skip to content

Commit

Permalink
feat: escape quotes on key. (#55)
Browse files Browse the repository at this point in the history
* escaping key
  • Loading branch information
Erkanerkisi authored Jan 2, 2024
1 parent 8f26ccb commit c4b0420
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion elasticsearch/bulk/bulk.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func getEsActionJSON(docID []byte, action document.EsAction, indexName string, r
}
meta = append(meta, helper.Byte(indexName)...)
meta = append(meta, idPrefix...)
meta = append(meta, docID...)
meta = append(meta, helper.EscapePredefinedBytes(docID)...)
if routing != nil {
meta = append(meta, routingPrefix...)
meta = append(meta, helper.Byte(*routing)...)
Expand Down
21 changes: 21 additions & 0 deletions helper/escape.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package helper

var (
EscapeBytes = []byte{
34, // quote
}
BackSlash byte = 92
)

func EscapePredefinedBytes(docID []byte) []byte {
newByteArr := make([]byte, 0, len(docID))
for _, byt := range docID {
for _, escapeByte := range EscapeBytes {
if escapeByte == byt {
newByteArr = append(newByteArr, BackSlash)
}
}
newByteArr = append(newByteArr, byt)
}
return newByteArr
}
29 changes: 29 additions & 0 deletions helper/escape_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package helper

import (
"testing"
)

func TestEscapeQuote(t *testing.T) {
input := "12345-999\""
byteArr := []byte(input)
result := EscapePredefinedBytes(byteArr)
if len(result) == 10 || result[len(result)-1] != 34 {
t.Error("Expected backslash byte")
}
}

func TestDoNotEscapeQuote(t *testing.T) {
input := "12345-999"
byteArr := []byte(input)

result := EscapePredefinedBytes(byteArr)
if len(result) != 9 {
t.Error("Not expected any change related with length")
}
for _, b := range result {
if b == 34 {
t.Error("Not expected any backslash")
}
}
}

0 comments on commit c4b0420

Please sign in to comment.