diff --git a/utils.go b/utils.go index f328c9b..5c7b152 100644 --- a/utils.go +++ b/utils.go @@ -25,3 +25,13 @@ func createPath(path string) []string { } return keys } + +// EscapePath to escape a path +// Example +// - By default jsonmap.Set("message.raw", "hello world !") +// => { "message": { "raw": "hello world !" }} +// - With escape jsonmap.Set(jsonmap.EscapePath("message.raw", "hello world !")) +// => { "message.raw": "hello world !" }} +func EscapePath(path string) string { + return strings.Replace(path, ".", "\\.", -1) +} diff --git a/utils_test.go b/utils_test.go new file mode 100644 index 0000000..2df944d --- /dev/null +++ b/utils_test.go @@ -0,0 +1,18 @@ +package jsonmap_test + +import ( + "testing" + + "github.com/datasweet/jsonmap" + "github.com/stretchr/testify/assert" +) + +func TestEscapePath(t *testing.T) { + json := jsonmap.New() + json.Set("message.raw", "hello world !") + assert.JSONEq(t, `{ "message": { "raw": "hello world !" }}`, json.Stringify()) + + json = jsonmap.New() + json.Set(jsonmap.EscapePath("message.raw"), "hello world !") + assert.JSONEq(t, `{ "message.raw": "hello world !" }`, json.Stringify()) +}