Skip to content
This repository has been archived by the owner on Mar 11, 2024. It is now read-only.

Commit

Permalink
Add suport for parsing unicode characters (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
1uka authored and fabioxgn committed Oct 7, 2018
1 parent b9d139e commit cf98806
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/gorilla/websocket v1.4.0 // indirect
github.com/martinusso/go-docs v0.0.0-20161215163720-81905d575a58 // indirect
github.com/mattn/go-shellwords v1.0.3
github.com/mozillazg/go-unidecode v0.1.0
github.com/nlopes/slack v0.4.0
github.com/pkg/errors v0.8.0 // indirect
github.com/pyinx/gorocket v0.0.0-20170810024322-78ae1353729f
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ github.com/martinusso/go-docs v0.0.0-20161215163720-81905d575a58 h1:VmcrkkMjTdCG
github.com/martinusso/go-docs v0.0.0-20161215163720-81905d575a58/go.mod h1:QymHbiLXXhrSGV5xTWYfEBt9mau3hHwVOT9Y7tpolJU=
github.com/mattn/go-shellwords v1.0.3 h1:K/VxK7SZ+cvuPgFSLKi5QPI9Vr/ipOf4C1gN+ntueUk=
github.com/mattn/go-shellwords v1.0.3/go.mod h1:3xCvwCdWdlDJUrvuMn7Wuy9eWs4pE8vqg+NOMyg4B2o=
github.com/mozillazg/go-unidecode v0.1.0 h1:wAIMDf/yTexXKxT5TkctLwmClGSyuoJaZDRMclFNq8U=
github.com/mozillazg/go-unidecode v0.1.0/go.mod h1:fYMdhyjni9ZeEmS6OE/GJHDLsF8TQvIVDwYR/drR26Q=
github.com/nlopes/slack v0.4.0 h1:OVnHm7lv5gGT5gkcHsZAyw++oHVFihbjWbL3UceUpiA=
github.com/nlopes/slack v0.4.0/go.mod h1:jVI4BBK3lSktibKahxBF74txcK2vyvkza1z/+rRnVAM=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
Expand Down
5 changes: 3 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/mattn/go-shellwords"
unidecode "github.com/mozillazg/go-unidecode"
)

var (
Expand Down Expand Up @@ -35,12 +36,12 @@ func parse(s string, channel *ChannelData, user *User) (*Cmd, error) {

// get the command
pieces := strings.SplitN(c.Message, " ", 2)
c.Command = strings.ToLower(pieces[0])
c.Command = strings.ToLower(unidecode.Unidecode(pieces[0]))

if len(pieces) > 1 {
// get the arguments and remove extra spaces
c.RawArgs = strings.TrimSpace(pieces[1])
parsedArgs, err := shellwords.Parse(c.RawArgs)
parsedArgs, err := shellwords.Parse(unidecode.Unidecode(c.RawArgs))
if err != nil {
return nil, errors.New("Error parsing arguments: " + err.Error())
}
Expand Down
22 changes: 22 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ func TestParser(t *testing.T) {
cmdMixedCaseWithoutArgs := CmdPrefix + "Cmd"
cmdMixedCaseWithArgs := CmdPrefix + "Cmd arg1 arg2 "
cmdMixedCaseWithQuotes := CmdPrefix + "Cmd \"arg1 arg2\""
cmdUnicode := CmdPrefix + "Çmd"
cmdUnicodeWithArgs := CmdPrefix + "çмд årg1 årg2"

tests := []struct {
msg string
Expand Down Expand Up @@ -84,6 +86,26 @@ func TestParser(t *testing.T) {
Args: []string{"arg1 arg2"},
MessageData: &Message{Text: strings.TrimLeft("Cmd \"arg1 arg2\"", CmdPrefix)},
}},
{cmdUnicode, &Cmd{
Raw: cmdUnicode,
Command: "cmd",
Channel: channel.Channel,
ChannelData: channel,
User: user,
Message: "Çmd",
MessageData: &Message{Text: strings.TrimLeft("Çmd", CmdPrefix)},
}},
{cmdUnicodeWithArgs, &Cmd{
Raw: cmdUnicodeWithArgs,
Command: "cmd",
Channel: channel.Channel,
ChannelData: channel,
User: user,
Message: "çмд årg1 årg2",
RawArgs: "årg1 årg2",
Args: []string{"arg1", "arg2"},
MessageData: &Message{Text: strings.TrimLeft("çмд årg1 årg2", CmdPrefix)},
}},
}

for _, test := range tests {
Expand Down

0 comments on commit cf98806

Please sign in to comment.