From aaee5183a91050415de90776d1cbb08c28578b19 Mon Sep 17 00:00:00 2001 From: Antonio Murdaca Date: Tue, 4 Oct 2016 12:00:48 +0200 Subject: [PATCH] api/server/httputils: compile with go < 1.7 Signed-off-by: Antonio Murdaca Upstream-commit: d1d505fa701bfe28a59afee8158994cf33ef043c Component: engine --- .../engine/api/server/httputils/httputils.go | 10 ---------- .../server/httputils/httputils_write_json.go | 17 +++++++++++++++++ .../httputils/httputils_write_json_go16.go | 16 ++++++++++++++++ 3 files changed, 33 insertions(+), 10 deletions(-) create mode 100644 components/engine/api/server/httputils/httputils_write_json.go create mode 100644 components/engine/api/server/httputils/httputils_write_json_go16.go diff --git a/components/engine/api/server/httputils/httputils.go b/components/engine/api/server/httputils/httputils.go index 2a0418f9bd..7930ff7a07 100644 --- a/components/engine/api/server/httputils/httputils.go +++ b/components/engine/api/server/httputils/httputils.go @@ -1,7 +1,6 @@ package httputils import ( - "encoding/json" "fmt" "io" "net/http" @@ -77,15 +76,6 @@ func ParseForm(r *http.Request) error { return nil } -// WriteJSON writes the value v to the http response stream as json with standard json encoding. -func WriteJSON(w http.ResponseWriter, code int, v interface{}) error { - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(code) - enc := json.NewEncoder(w) - enc.SetEscapeHTML(false) - return enc.Encode(v) -} - // VersionFromContext returns an API version from the context using APIVersionKey. // It panics if the context value does not have version.Version type. func VersionFromContext(ctx context.Context) (ver string) { diff --git a/components/engine/api/server/httputils/httputils_write_json.go b/components/engine/api/server/httputils/httputils_write_json.go new file mode 100644 index 0000000000..4787cc3c33 --- /dev/null +++ b/components/engine/api/server/httputils/httputils_write_json.go @@ -0,0 +1,17 @@ +// +build go1.7 + +package httputils + +import ( + "encoding/json" + "net/http" +) + +// WriteJSON writes the value v to the http response stream as json with standard json encoding. +func WriteJSON(w http.ResponseWriter, code int, v interface{}) error { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(code) + enc := json.NewEncoder(w) + enc.SetEscapeHTML(false) + return enc.Encode(v) +} diff --git a/components/engine/api/server/httputils/httputils_write_json_go16.go b/components/engine/api/server/httputils/httputils_write_json_go16.go new file mode 100644 index 0000000000..bdc6981738 --- /dev/null +++ b/components/engine/api/server/httputils/httputils_write_json_go16.go @@ -0,0 +1,16 @@ +// +build go1.6,!go1.7 + +package httputils + +import ( + "encoding/json" + "net/http" +) + +// WriteJSON writes the value v to the http response stream as json with standard json encoding. +func WriteJSON(w http.ResponseWriter, code int, v interface{}) error { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(code) + enc := json.NewEncoder(w) + return enc.Encode(v) +}