From fb991fb84e7432c412ca0ab0f7565e94b7afcc38 Mon Sep 17 00:00:00 2001 From: Daniel Smith Date: Fri, 20 Jun 2014 15:24:56 -0700 Subject: [PATCH] Change type to []byte --- pkg/api/helper.go | 6 ++++-- pkg/apiserver/apiserver.go | 6 +++--- pkg/apiserver/apiserver_test.go | 4 ++-- pkg/registry/controller_registry.go | 4 ++-- pkg/registry/controller_registry_test.go | 2 +- pkg/registry/pod_registry.go | 4 ++-- pkg/registry/pod_registry_test.go | 2 +- pkg/registry/service_registry.go | 2 +- 8 files changed, 16 insertions(+), 14 deletions(-) diff --git a/pkg/api/helper.go b/pkg/api/helper.go index 3aeddb0b0ff..515778d7be2 100644 --- a/pkg/api/helper.go +++ b/pkg/api/helper.go @@ -65,7 +65,7 @@ func Encode(obj interface{}) (data []byte, err error) { if _, contains := knownTypes[name]; !contains { return nil, fmt.Errorf("struct %v can't be unmarshalled because it's not in knownTypes", name) } - jsonBase.FieldByName("Kind").Set(reflect.ValueOf(name)) + jsonBase.FieldByName("Kind").SetString(name) return json.Marshal(obj) } @@ -104,7 +104,9 @@ func DecodeInto(data []byte, obj interface{}) error { return err } foundName := jsonBase.FieldByName("Kind").Interface().(string) - if foundName != "" && foundName != name { + if foundName == "" { + jsonBase.FieldByName("Kind").SetString(name) + } else if foundName != name { return fmt.Errorf("data had kind %v, but passed object was of type %v", foundName, name) } return nil diff --git a/pkg/apiserver/apiserver.go b/pkg/apiserver/apiserver.go index 74b019f60d5..19823b97e49 100644 --- a/pkg/apiserver/apiserver.go +++ b/pkg/apiserver/apiserver.go @@ -36,7 +36,7 @@ type RESTStorage interface { List(labels.Selector) (interface{}, error) Get(id string) (interface{}, error) Delete(id string) (<-chan interface{}, error) - Extract(body string) (interface{}, error) + Extract(body []byte) (interface{}, error) Create(interface{}) (<-chan interface{}, error) Update(interface{}) (<-chan interface{}, error) } @@ -143,10 +143,10 @@ func (server *ApiServer) error(err error, w http.ResponseWriter) { fmt.Fprintf(w, "Internal Error: %#v", err) } -func (server *ApiServer) readBody(req *http.Request) (string, error) { +func (server *ApiServer) readBody(req *http.Request) ([]byte, error) { defer req.Body.Close() body, err := ioutil.ReadAll(req.Body) - return string(body), err + return body, err } func (server *ApiServer) waitForObject(out <-chan interface{}, timeout time.Duration) (interface{}, error) { diff --git a/pkg/apiserver/apiserver_test.go b/pkg/apiserver/apiserver_test.go index 93748debe01..a9194bd6a00 100644 --- a/pkg/apiserver/apiserver_test.go +++ b/pkg/apiserver/apiserver_test.go @@ -69,9 +69,9 @@ func (storage *SimpleRESTStorage) Delete(id string) (<-chan interface{}, error) return storage.channel, storage.err } -func (storage *SimpleRESTStorage) Extract(body string) (interface{}, error) { +func (storage *SimpleRESTStorage) Extract(body []byte) (interface{}, error) { var item Simple - json.Unmarshal([]byte(body), &item) + json.Unmarshal(body, &item) return item, storage.err } diff --git a/pkg/registry/controller_registry.go b/pkg/registry/controller_registry.go index d99ad90ff5c..d71e7f90b1f 100644 --- a/pkg/registry/controller_registry.go +++ b/pkg/registry/controller_registry.go @@ -61,9 +61,9 @@ func (storage *ControllerRegistryStorage) Delete(id string) (<-chan interface{}, return apiserver.MakeAsync(func() interface{} { return apiserver.Status{Success: true} }), storage.registry.DeleteController(id) } -func (storage *ControllerRegistryStorage) Extract(body string) (interface{}, error) { +func (storage *ControllerRegistryStorage) Extract(body []byte) (interface{}, error) { result := api.ReplicationController{} - err := json.Unmarshal([]byte(body), &result) + err := json.Unmarshal(body, &result) result.Kind = "cluster#replicationController" return result, err } diff --git a/pkg/registry/controller_registry_test.go b/pkg/registry/controller_registry_test.go index 8d2257ec0d5..5a96d211470 100644 --- a/pkg/registry/controller_registry_test.go +++ b/pkg/registry/controller_registry_test.go @@ -123,7 +123,7 @@ func TestExtractControllerJson(t *testing.T) { } body, err := json.Marshal(controller) expectNoError(t, err) - controllerOut, err := storage.Extract(string(body)) + controllerOut, err := storage.Extract(body) expectNoError(t, err) // Extract adds a Kind controller.Kind = "cluster#replicationController" diff --git a/pkg/registry/pod_registry.go b/pkg/registry/pod_registry.go index fd9c6eb3c93..f1b1de4fa75 100644 --- a/pkg/registry/pod_registry.go +++ b/pkg/registry/pod_registry.go @@ -136,9 +136,9 @@ func (storage *PodRegistryStorage) Delete(id string) (<-chan interface{}, error) return apiserver.MakeAsync(func() interface{} { return apiserver.Status{Success: true} }), storage.registry.DeletePod(id) } -func (storage *PodRegistryStorage) Extract(body string) (interface{}, error) { +func (storage *PodRegistryStorage) Extract(body []byte) (interface{}, error) { pod := api.Pod{} - err := json.Unmarshal([]byte(body), &pod) + err := json.Unmarshal(body, &pod) pod.Kind = "cluster#pod" return pod, err } diff --git a/pkg/registry/pod_registry_test.go b/pkg/registry/pod_registry_test.go index 783bbe1c905..d984feac56a 100644 --- a/pkg/registry/pod_registry_test.go +++ b/pkg/registry/pod_registry_test.go @@ -104,7 +104,7 @@ func TestExtractJson(t *testing.T) { } body, err := json.Marshal(pod) expectNoError(t, err) - podOut, err := storage.Extract(string(body)) + podOut, err := storage.Extract(body) expectNoError(t, err) // Extract adds in a kind pod.Kind = "cluster#pod" diff --git a/pkg/registry/service_registry.go b/pkg/registry/service_registry.go index 37691e24d99..6ea48c3ea77 100644 --- a/pkg/registry/service_registry.go +++ b/pkg/registry/service_registry.go @@ -105,7 +105,7 @@ func (sr *ServiceRegistryStorage) Delete(id string) (<-chan interface{}, error) return apiserver.MakeAsync(func() interface{} { return apiserver.Status{Success: true} }), sr.registry.DeleteService(id) } -func (sr *ServiceRegistryStorage) Extract(body string) (interface{}, error) { +func (sr *ServiceRegistryStorage) Extract(body []byte) (interface{}, error) { var svc api.Service err := json.Unmarshal([]byte(body), &svc) svc.Kind = "cluster#service"