Skip to content
This repository has been archived by the owner on Jun 5, 2018. It is now read-only.

Commit

Permalink
Fix crash on parse error
Browse files Browse the repository at this point in the history
  • Loading branch information
calmh committed Jul 21, 2017
1 parent c04d5a6 commit 4dc601a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,13 @@ func usage() {

func printResponse(out io.Writer, res response) {
if res.Error.Code != 0 {
fmt.Fprintf(out, "Error %d: %s", res.Error.Code, res.Error.Message)
fmt.Fprintf(out, "Error %d: %s\n", res.Error.Code, res.Error.Message)
} else if res.Result != nil {
switch result := res.Result.(type) {
case []interface{}:
for _, res := range result {
switch res := res.(type) {
case string, int, json.Number:
case string, float64, int, json.Number:
fmt.Fprintln(out, res)
default:
bs, _ := json.MarshalIndent(res, "", " ")
Expand Down
4 changes: 4 additions & 0 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ func parseCommand(line string) (command, error) {
parts := strings.Split(param, ",")
obj := map[string]string{}
for _, part := range parts {
if !strings.Contains(part, "=") {
obj[part] = ""
continue
}
kv := strings.SplitN(part, "=", 2)
obj[kv[0]] = kv[1]
}
Expand Down
7 changes: 7 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ func TestParseCommand(t *testing.T) {
Params: []interface{}{"subscriber", "1234", map[string]interface{}{"foo": "bar 1", "baz 2": "quux 2"}},
},
},
{
`object update subscriber 1234 foo=,bar`,
command{
Method: "object.update",
Params: []interface{}{"subscriber", "1234", map[string]string{"foo": "", "bar": ""}},
},
},
}

for _, tc := range testcases {
Expand Down

0 comments on commit 4dc601a

Please sign in to comment.