2018-03-28 00:30:51 +00:00
|
|
|
package influx
|
|
|
|
|
|
|
|
import "strings"
|
|
|
|
|
|
|
|
const (
|
2018-04-09 22:29:52 +00:00
|
|
|
escapes = "\t\n\f\r ,="
|
|
|
|
nameEscapes = "\t\n\f\r ,"
|
|
|
|
stringFieldEscapes = "\t\n\f\r\\\""
|
2018-03-28 00:30:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
escaper = strings.NewReplacer(
|
2018-04-09 22:29:52 +00:00
|
|
|
"\t", `\t`,
|
|
|
|
"\n", `\n`,
|
|
|
|
"\f", `\f`,
|
|
|
|
"\r", `\r`,
|
2018-03-28 00:30:51 +00:00
|
|
|
`,`, `\,`,
|
|
|
|
` `, `\ `,
|
|
|
|
`=`, `\=`,
|
|
|
|
)
|
|
|
|
|
|
|
|
nameEscaper = strings.NewReplacer(
|
2018-04-09 22:29:52 +00:00
|
|
|
"\t", `\t`,
|
|
|
|
"\n", `\n`,
|
|
|
|
"\f", `\f`,
|
|
|
|
"\r", `\r`,
|
2018-03-28 00:30:51 +00:00
|
|
|
`,`, `\,`,
|
|
|
|
` `, `\ `,
|
|
|
|
)
|
|
|
|
|
|
|
|
stringFieldEscaper = strings.NewReplacer(
|
|
|
|
`"`, `\"`,
|
|
|
|
`\`, `\\`,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2018-04-09 22:29:52 +00:00
|
|
|
// Escape a tagkey, tagvalue, or fieldkey
|
2018-03-28 00:30:51 +00:00
|
|
|
func escape(s string) string {
|
|
|
|
if strings.ContainsAny(s, escapes) {
|
|
|
|
return escaper.Replace(s)
|
|
|
|
} else {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-09 22:29:52 +00:00
|
|
|
// Escape a measurement name
|
2018-03-28 00:30:51 +00:00
|
|
|
func nameEscape(s string) string {
|
|
|
|
if strings.ContainsAny(s, nameEscapes) {
|
|
|
|
return nameEscaper.Replace(s)
|
|
|
|
} else {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-09 22:29:52 +00:00
|
|
|
// Escape a string field
|
2018-03-28 00:30:51 +00:00
|
|
|
func stringFieldEscape(s string) string {
|
|
|
|
if strings.ContainsAny(s, stringFieldEscapes) {
|
|
|
|
return stringFieldEscaper.Replace(s)
|
|
|
|
} else {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|