refactor: change validate reserved words

This commit is contained in:
710leo 2020-03-15 12:06:09 +08:00
parent 0ba6c698d1
commit e32178371d
1 changed files with 8 additions and 25 deletions

View File

@ -63,17 +63,14 @@ func (m *MetricValue) CheckValidity() (err error) {
return return
} }
//将保留字替换 //检测保留字
var illegal bool if HasReservedWords(m.Metric) {
m.Metric, illegal = ReplaceReservedWords(m.Metric) err = fmt.Errorf("metric:%s contains reserved words:[\\t] [\\r] [\\n] [,] [ ] [=]", m.Metric)
if illegal {
err = fmt.Errorf("Metric contains reserved word")
return return
} }
m.Endpoint, illegal = ReplaceReservedWords(m.Endpoint) if HasReservedWords(m.Endpoint) {
if illegal { err = fmt.Errorf("endpoint:%s contains reserved words:[\\t] [\\r] [\\n] [,] [ ] [=]", m.Endpoint)
err = fmt.Errorf("Endpoint contains reserved word:%s", m.Endpoint)
return return
} }
@ -151,7 +148,7 @@ func (m *MetricValue) CheckValidity() (err error) {
return return
} }
func ReplaceReservedWords(str string) (string, bool) { func HasReservedWords(str string) bool {
if -1 == strings.IndexFunc(str, if -1 == strings.IndexFunc(str,
func(r rune) bool { func(r rune) bool {
return r == '\t' || return r == '\t' ||
@ -159,27 +156,13 @@ func ReplaceReservedWords(str string) (string, bool) {
r == '\n' || r == '\n' ||
r == ',' || r == ',' ||
r == ' ' || r == ' ' ||
r == ':' ||
r == '=' r == '='
}) { }) {
return str, false return false
} }
return strings.Map(func(r rune) rune { return true
if r == '\t' ||
r == '\r' ||
r == '\n' ||
r == ',' ||
r == ' ' ||
r == ':' ||
r == '=' {
return '_'
}
return r
}, str), true
return str, false
} }
func SortedTags(tags map[string]string) string { func SortedTags(tags map[string]string) string {