From 14361e336ab571b14e511788c78d7b391a5c8b51 Mon Sep 17 00:00:00 2001 From: Daniel Smith Date: Fri, 20 Jun 2014 16:18:36 -0700 Subject: [PATCH] Make apiserver work with new encode/decode --- pkg/apiserver/apiserver.go | 4 ++-- pkg/apiserver/apiserver_test.go | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/pkg/apiserver/apiserver.go b/pkg/apiserver/apiserver.go index 19823b97e49..c5fd4b85cd8 100644 --- a/pkg/apiserver/apiserver.go +++ b/pkg/apiserver/apiserver.go @@ -17,7 +17,6 @@ limitations under the License. package apiserver import ( - "encoding/json" "fmt" "io/ioutil" "log" @@ -27,6 +26,7 @@ import ( "strings" "time" + "github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/labels" "github.com/GoogleCloudPlatform/kubernetes/pkg/util" ) @@ -130,7 +130,7 @@ func (server *ApiServer) notFound(req *http.Request, w http.ResponseWriter) { func (server *ApiServer) write(statusCode int, object interface{}, w http.ResponseWriter) { w.WriteHeader(statusCode) - output, err := json.MarshalIndent(object, "", " ") + output, err := api.EncodeIndent(object) if err != nil { server.error(err, w) return diff --git a/pkg/apiserver/apiserver_test.go b/pkg/apiserver/apiserver_test.go index a9194bd6a00..006a6bfc0df 100644 --- a/pkg/apiserver/apiserver_test.go +++ b/pkg/apiserver/apiserver_test.go @@ -26,9 +26,14 @@ import ( "sync" "testing" + "github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/labels" ) +func init() { + api.AddKnownTypes(Simple{}, SimpleList{}) +} + // TODO: This doesn't reduce typing enough to make it worth the less readable errors. Remove. func expectNoError(t *testing.T, err error) { if err != nil { @@ -37,11 +42,13 @@ func expectNoError(t *testing.T, err error) { } type Simple struct { - Name string + JSONBase api.JSONBase `json:",inline"` + Name string } type SimpleList struct { - Items []Simple + JSONBase api.JSONBase `json:",inline"` + Items []Simple } type SimpleRESTStorage struct { @@ -54,7 +61,7 @@ type SimpleRESTStorage struct { } func (storage *SimpleRESTStorage) List(labels.Selector) (interface{}, error) { - result := SimpleList{ + result := &SimpleList{ Items: storage.list, } return result, storage.err @@ -71,7 +78,7 @@ func (storage *SimpleRESTStorage) Delete(id string) (<-chan interface{}, error) func (storage *SimpleRESTStorage) Extract(body []byte) (interface{}, error) { var item Simple - json.Unmarshal(body, &item) + api.DecodeInto(body, &item) return item, storage.err } @@ -90,7 +97,7 @@ func extractBody(response *http.Response, object interface{}) (string, error) { if err != nil { return string(body), err } - err = json.Unmarshal(body, object) + err = api.DecodeInto(body, object) return string(body), err } @@ -150,6 +157,7 @@ func TestNonEmptyList(t *testing.T) { body, err := extractBody(resp, &listOut) if len(listOut.Items) != 1 { t.Errorf("Unexpected response: %#v", listOut) + return } if listOut.Items[0].Name != simpleStorage.list[0].Name { t.Errorf("Unexpected data: %#v, %s", listOut.Items[0], string(body))