Skip to content

Commit 14fafa0

Browse files
author
Thomas Charlot
authored
Escape a path (#5)
1 parent 8c01528 commit 14fafa0

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

utils.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,13 @@ func createPath(path string) []string {
2525
}
2626
return keys
2727
}
28+
29+
// EscapePath to escape a path
30+
// Example
31+
// - By default jsonmap.Set("message.raw", "hello world !")
32+
// => { "message": { "raw": "hello world !" }}
33+
// - With escape jsonmap.Set(jsonmap.EscapePath("message.raw", "hello world !"))
34+
// => { "message.raw": "hello world !" }}
35+
func EscapePath(path string) string {
36+
return strings.Replace(path, ".", "\\.", -1)
37+
}

utils_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package jsonmap_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/datasweet/jsonmap"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestEscapePath(t *testing.T) {
11+
json := jsonmap.New()
12+
json.Set("message.raw", "hello world !")
13+
assert.JSONEq(t, `{ "message": { "raw": "hello world !" }}`, json.Stringify())
14+
15+
json = jsonmap.New()
16+
json.Set(jsonmap.EscapePath("message.raw"), "hello world !")
17+
assert.JSONEq(t, `{ "message.raw": "hello world !" }`, json.Stringify())
18+
}

0 commit comments

Comments
 (0)