Skip to content

Commit

Permalink
Merge pull request #8 from roistat/issue-7-backslash-fix
Browse files Browse the repository at this point in the history
Backslash fix
  • Loading branch information
flrnull authored Nov 7, 2016
2 parents 9aff1a0 + 644ee15 commit b9fe2e5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
12 changes: 9 additions & 3 deletions marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ import (
"time"
)

func escape(s string) string {
s = strings.Replace(s, `\`, `\\`, -1)
s = strings.Replace(s, `'`, `\'`, -1)
return s
}

func unescape(s string) string {
s = strings.Replace(s, "\\\\", "\\", -1)
s = strings.Replace(s, "\\'", "'", -1)
s = strings.Replace(s, `\\`, `\`, -1)
s = strings.Replace(s, `\'`, `'`, -1)
return s
}

Expand Down Expand Up @@ -155,7 +161,7 @@ func marshal(value interface{}) string {
}
switch v := value.(type) {
case string:
return fmt.Sprintf("'%s'", strings.Replace(v, "'", "\\'", -1))
return fmt.Sprintf("'%s'", escape(v))
case int, int8, int16, int32, int64,
uint, uint8, uint16, uint32, uint64,
float32, float64:
Expand Down
3 changes: 3 additions & 0 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,12 @@ func TestMarshal(t *testing.T) {
assert.Equal(t, "'10'", marshal("10"))
assert.Equal(t, "'String1\\''", marshal("String1'"))
assert.Equal(t, "'String\r'", marshal("String\r"))
assert.Equal(t, "'String\r'", marshal("String\r"))
assert.Equal(t, `'String\\'`, marshal(`String\`))
assert.Equal(t, "[10,20,30]", marshal(Array{10, 20, 30}))
assert.Equal(t, "['k10','20','30val']", marshal(Array{"k10", "20", "30val"}))
assert.Equal(t, "['k10','20','30val']", marshal([]string{"k10", "20", "30val"}))
assert.Equal(t, "['k10','20','30val\\\\']", marshal([]string{"k10", "20", "30val\\"}))
assert.Equal(t, "[10,20,30]", marshal([]int{10, 20, 30}))
assert.Equal(t, "''", marshal(t))
}

0 comments on commit b9fe2e5

Please sign in to comment.