dorisbot/pkg/cmdparser/cmdparser.go

46 lines
686 B
Go
Raw Normal View History

2019-07-03 09:58:38 +00:00
package cmdparser
2019-07-04 09:00:16 +00:00
import (
"regexp"
"strings"
)
2019-07-03 09:58:38 +00:00
// CommandType -
type CommandType int
// CmdAction -
type CmdAction struct {
2019-07-04 09:00:16 +00:00
Key string
Value string
Origin string
2019-07-03 09:58:38 +00:00
}
var actRegex = regexp.MustCompile("{{(.+?)}}")
2019-07-04 09:00:16 +00:00
// ParseCmdAction -
func ParseCmdAction(value string) (acts []*CmdAction) {
2019-07-03 09:58:38 +00:00
strs := actRegex.FindAllStringSubmatch(value, -1)
if len(strs) == 0 {
return nil
}
acts = make([]*CmdAction, 0, len(strs))
for _, key := range strs {
2019-07-04 09:00:16 +00:00
act := &CmdAction{}
act.Origin = key[1]
keys := strings.Split(key[1], "=")
if len(keys) == 2 {
act.Key = keys[0]
act.Value = keys[1]
} else {
act.Key = key[1]
}
acts = append(acts, act)
2019-07-03 09:58:38 +00:00
}
return
}