From 57f6940dba6d04168d2cb0bb0710891a9e341f39 Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Wed, 13 May 2015 02:34:46 +0200 Subject: [PATCH] Fixes JSON rendering --- render/ssevent.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/render/ssevent.go b/render/ssevent.go index 34f4e475..a0819987 100644 --- a/render/ssevent.go +++ b/render/ssevent.go @@ -24,18 +24,18 @@ func WriteSSEvent(w http.ResponseWriter, eventName string, data interface{}) err } var stringData string switch typeOfData(data) { - case reflect.Struct, reflect.Slice: + case reflect.Struct, reflect.Slice, reflect.Map: if jsonBytes, err := json.Marshal(data); err == nil { stringData = string(jsonBytes) } else { return err } case reflect.Ptr: - stringData = escape(fmt.Sprintf("%v", &data)) + "\n" + stringData = escape(fmt.Sprintf("%v", &data)) default: - stringData = escape(fmt.Sprintf("%v", data)) + "\n" + stringData = escape(fmt.Sprintf("%v", data)) } - _, err := fmt.Fprintf(w, "event: %s\ndata: %s\n", escape(eventName), stringData) + _, err := fmt.Fprintf(w, "event: %s\ndata: %s\n\n", escape(eventName), stringData) return err } @@ -44,7 +44,10 @@ func typeOfData(data interface{}) reflect.Kind { valueType := value.Kind() if valueType == reflect.Ptr { newValue := value.Elem().Kind() - if newValue == reflect.Struct || newValue == reflect.Slice { + fmt.Println(newValue) + if newValue == reflect.Struct || + newValue == reflect.Slice || + newValue == reflect.Map { return newValue } else { return valueType