native output

This commit is contained in:
Josh Baker 2016-03-28 17:38:21 -07:00
parent f00d5ca9e8
commit a47377ae49
2 changed files with 20 additions and 10 deletions

View File

@ -169,20 +169,29 @@ func (c *Controller) handleInputCommand(conn *server.Conn, msg *server.Message,
for _, v := range msg.Values { for _, v := range msg.Values {
words = append(words, v.String()) words = append(words, v.String())
} }
// line := strings.Join(words, " ")
// if core.ShowDebugMessages && line != "pInG" {
// log.Debug(line)
// }
start := time.Now() start := time.Now()
writeOutput := func(res string) error {
switch msg.ConnType {
default:
panic(fmt.Sprintf("unsupported conn type: %v", msg.ConnType))
case server.RESP:
_, err := io.WriteString(w, res)
return err
case server.Native:
_, err := fmt.Fprintf(w, "$%d %s\r\n", len(res), res)
return err
}
}
// Ping. Just send back the response. No need to put through the pipeline. // Ping. Just send back the response. No need to put through the pipeline.
if msg.Command == "ping" { if msg.Command == "ping" {
switch msg.OutputType { switch msg.OutputType {
case server.JSON: case server.JSON:
w.Write([]byte(`{"ok":true,"ping":"pong","elapsed":"` + time.Now().Sub(start).String() + `"}`)) return writeOutput(`{"ok":true,"ping":"pong","elapsed":"` + time.Now().Sub(start).String() + `"}`)
case server.RESP: case server.RESP:
io.WriteString(w, "+PONG\r\n") return writeOutput("+PONG\r\n")
} }
return nil return nil
} }
@ -190,13 +199,13 @@ func (c *Controller) handleInputCommand(conn *server.Conn, msg *server.Message,
writeErr := func(err error) error { writeErr := func(err error) error {
switch msg.OutputType { switch msg.OutputType {
case server.JSON: case server.JSON:
io.WriteString(w, `{"ok":false,"err":`+jsonString(err.Error())+`,"elapsed":"`+time.Now().Sub(start).String()+"\"}") return writeOutput(`{"ok":false,"err":` + jsonString(err.Error()) + `,"elapsed":"` + time.Now().Sub(start).String() + "\"}")
case server.RESP: case server.RESP:
if err == errInvalidNumberOfArguments { if err == errInvalidNumberOfArguments {
io.WriteString(w, "-ERR wrong number of arguments for '"+msg.Command+"' command\r\n") return writeOutput("-ERR wrong number of arguments for '" + msg.Command + "' command\r\n")
} else { } else {
v, _ := resp.ErrorValue(errors.New("ERR " + err.Error())).MarshalRESP() v, _ := resp.ErrorValue(errors.New("ERR " + err.Error())).MarshalRESP()
io.WriteString(w, string(v)) return writeOutput(string(v))
} }
} }
return nil return nil
@ -280,7 +289,7 @@ func (c *Controller) handleInputCommand(conn *server.Conn, msg *server.Message,
} }
} }
if res != "" { if res != "" {
if _, err := io.WriteString(w, res); err != nil { if err := writeOutput(res); err != nil {
return err return err
} }
} }

View File

@ -51,6 +51,7 @@ func (c *Controller) cmdStats(msg *server.Message) (res string, err error) {
} }
switch msg.OutputType { switch msg.OutputType {
case server.JSON: case server.JSON:
data, err := json.Marshal(ms) data, err := json.Marshal(ms)
if err != nil { if err != nil {
return "", err return "", err