2016-12-01 15:18:46 +00:00
|
|
|
package metric
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// escaper is for escaping:
|
|
|
|
// - tag keys
|
|
|
|
// - tag values
|
|
|
|
// - field keys
|
|
|
|
// see https://docs.influxdata.com/influxdb/v1.0/write_protocols/line_protocol_tutorial/#special-characters-and-keywords
|
|
|
|
escaper = strings.NewReplacer(`,`, `\,`, `"`, `\"`, ` `, `\ `, `=`, `\=`)
|
|
|
|
unEscaper = strings.NewReplacer(`\,`, `,`, `\"`, `"`, `\ `, ` `, `\=`, `=`)
|
|
|
|
|
|
|
|
// nameEscaper is for escaping measurement names only.
|
|
|
|
// see https://docs.influxdata.com/influxdb/v1.0/write_protocols/line_protocol_tutorial/#special-characters-and-keywords
|
|
|
|
nameEscaper = strings.NewReplacer(`,`, `\,`, ` `, `\ `)
|
|
|
|
nameUnEscaper = strings.NewReplacer(`\,`, `,`, `\ `, ` `)
|
|
|
|
|
|
|
|
// stringFieldEscaper is for escaping string field values only.
|
|
|
|
// see https://docs.influxdata.com/influxdb/v1.0/write_protocols/line_protocol_tutorial/#special-characters-and-keywords
|
2017-08-23 22:17:26 +00:00
|
|
|
stringFieldEscaper = strings.NewReplacer(
|
|
|
|
`"`, `\"`,
|
|
|
|
`\`, `\\`,
|
|
|
|
)
|
|
|
|
stringFieldUnEscaper = strings.NewReplacer(
|
|
|
|
`\"`, `"`,
|
|
|
|
`\\`, `\`,
|
|
|
|
)
|
2016-12-01 15:18:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func escape(s string, t string) string {
|
|
|
|
switch t {
|
|
|
|
case "fieldkey", "tagkey", "tagval":
|
|
|
|
return escaper.Replace(s)
|
|
|
|
case "name":
|
|
|
|
return nameEscaper.Replace(s)
|
|
|
|
case "fieldval":
|
|
|
|
return stringFieldEscaper.Replace(s)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func unescape(s string, t string) string {
|
|
|
|
switch t {
|
|
|
|
case "fieldkey", "tagkey", "tagval":
|
|
|
|
return unEscaper.Replace(s)
|
|
|
|
case "name":
|
|
|
|
return nameUnEscaper.Replace(s)
|
|
|
|
case "fieldval":
|
|
|
|
return stringFieldUnEscaper.Replace(s)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|