-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from breml/escape-logstash-strings
Add escape/unescape for Logstash strings
- Loading branch information
Showing
3 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package astutil | ||
|
||
import ( | ||
"regexp" | ||
) | ||
|
||
var unescapeRe = regexp.MustCompile(`\\.`) | ||
|
||
// Unescape converts an input containing escape sequences as understood by | ||
// Logstash (https://www.elastic.co/guide/en/logstash/current/configuration-file-structure.html#_escape_sequences) | ||
// to an unescaped string. | ||
// | ||
// Unescaped character sequences: | ||
// | ||
// \r : carriage return (ASCII 13) | ||
// \n : new line (ASCII 10) | ||
// \t : tab (ASCII 9) | ||
// \\ : backslash (ASCII 92) | ||
// \" : double quote (ASCII 34) | ||
// \' : single quote (ASCII 39) | ||
// | ||
// Based on: | ||
// https://github.com/elastic/logstash/blob/e9c9865f4066b54048f8d708612a72d25e2fe5d9/logstash-core/lib/logstash/config/string_escape.rb | ||
func Unescape(value string) string { | ||
return unescapeRe.ReplaceAllStringFunc(value, func(s string) string { | ||
switch s[1] { | ||
case '"', '\'', '\\': | ||
return string(s[1]) | ||
case 'n': | ||
return "\n" | ||
case 'r': | ||
return "\r" | ||
case 't': | ||
return "\t" | ||
default: | ||
return s | ||
} | ||
}) | ||
} | ||
|
||
var escapeRe = regexp.MustCompile(`(?s:.)`) | ||
|
||
// Escape converts an input to an escaped representation as understood by | ||
// Logstash (https://www.elastic.co/guide/en/logstash/current/configuration-file-structure.html#_escape_sequences). | ||
// | ||
// Escaped characters: | ||
// | ||
// \r : carriage return (ASCII 13) | ||
// \n : new line (ASCII 10) | ||
// \t : tab (ASCII 9) | ||
// \ : backslash (ASCII 92) | ||
// " : double quote (ASCII 34) | ||
// ' : single quote (ASCII 39) | ||
func Escape(value string) string { | ||
return escapeRe.ReplaceAllStringFunc(value, func(s string) string { | ||
switch s { | ||
case `"`, `'`, `\`: | ||
return `\` + s | ||
case "\n": | ||
return `\n` | ||
case "\r": | ||
return `\r` | ||
case "\t": | ||
return `\t` | ||
default: | ||
return s | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package astutil_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/breml/logstash-config/ast/astutil" | ||
) | ||
|
||
var tt = []struct { | ||
name string | ||
escaped string | ||
unescaped string | ||
}{ | ||
{ | ||
name: "carriage return (ASCII 13)", | ||
escaped: `\r`, | ||
unescaped: "\r", | ||
}, | ||
{ | ||
name: "new line (ASCII 10)", | ||
escaped: `\n`, | ||
unescaped: "\n", | ||
}, | ||
{ | ||
name: "tab (ASCII 9)", | ||
escaped: `\t`, | ||
unescaped: "\t", | ||
}, | ||
{ | ||
name: "backslash (ASCII 92)", | ||
escaped: `\\`, | ||
unescaped: `\`, | ||
}, | ||
{ | ||
name: "double quote (ASCII 34)", | ||
escaped: `\"`, | ||
unescaped: `"`, | ||
}, | ||
{ | ||
name: "single quote (ASCII 39)", | ||
escaped: `\'`, | ||
unescaped: `'`, | ||
}, | ||
{ | ||
name: "value containing all special characters", | ||
escaped: `foo\r\n\t\\\"\'bar`, | ||
unescaped: "foo\r\n\t\\\"'bar", | ||
}, | ||
} | ||
|
||
func TestEscape(t *testing.T) { | ||
for _, tc := range tt { | ||
t.Run(tc.name, func(t *testing.T) { | ||
got := astutil.Escape(tc.unescaped) | ||
if tc.escaped != got { | ||
t.Errorf("Escape(%q) != %q, got: %q", tc.unescaped, tc.escaped, got) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestUnescape(t *testing.T) { | ||
tt := append(tt, struct { | ||
name string | ||
escaped string | ||
unescaped string | ||
}{ | ||
name: "not valid escape", | ||
escaped: `\x`, | ||
unescaped: "\\x", | ||
}) | ||
|
||
for _, tc := range tt { | ||
t.Run(tc.name, func(t *testing.T) { | ||
got := astutil.Unescape(tc.escaped) | ||
if tc.unescaped != got { | ||
t.Errorf("Unescape(%q) != %q, got: %q", tc.escaped, tc.unescaped, got) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters