Change type to []byte

This commit is contained in:
Daniel Smith 2014-06-20 15:24:56 -07:00
parent a9d9bb9d47
commit fb991fb84e
8 changed files with 16 additions and 14 deletions

View File

@ -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

View File

@ -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) {

View File

@ -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
}

View File

@ -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
}

View File

@ -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"

View File

@ -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
}

View File

@ -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"

View File

@ -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"