dorisbot/pkg/cmdparser/cmdparser.go

60 lines
911 B
Go
Raw Normal View History

2019-07-03 09:58:38 +00:00
package cmdparser
import "regexp"
// CommandType -
type CommandType int
// Cmd Types
const (
TextCmd CommandType = 1 << iota
ImageCmd
VideoCmd
)
// Command -
type Command struct {
Type CommandType
Message string
}
// CmdAction -
type CmdAction struct {
Key string
Act string
}
var actRegex = regexp.MustCompile("{{(.+?)}}")
// CommandParser -
func CommandParser(value string) (cmd *Command, err error) {
acts := parseCmdAction(value)
if len(acts) == 0 {
// no get actions return origin value
return &Command{
Type: TextCmd,
Message: value,
}, nil
}
return
}
func parseCmdAction(value string) (acts []*CmdAction) {
strs := actRegex.FindAllStringSubmatch(value, -1)
if len(strs) == 0 {
return nil
}
acts = make([]*CmdAction, 0, len(strs))
for _, key := range strs {
acts = append(acts, &CmdAction{Key: key[1]})
}
return
}
func selectAction(act *CmdAction) {
}