mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-30 06:54:01 +00:00
Change type to []byte
This commit is contained in:
parent
a9d9bb9d47
commit
fb991fb84e
@ -65,7 +65,7 @@ func Encode(obj interface{}) (data []byte, err error) {
|
|||||||
if _, contains := knownTypes[name]; !contains {
|
if _, contains := knownTypes[name]; !contains {
|
||||||
return nil, fmt.Errorf("struct %v can't be unmarshalled because it's not in knownTypes", name)
|
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)
|
return json.Marshal(obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,7 +104,9 @@ func DecodeInto(data []byte, obj interface{}) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
foundName := jsonBase.FieldByName("Kind").Interface().(string)
|
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 fmt.Errorf("data had kind %v, but passed object was of type %v", foundName, name)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -36,7 +36,7 @@ type RESTStorage interface {
|
|||||||
List(labels.Selector) (interface{}, error)
|
List(labels.Selector) (interface{}, error)
|
||||||
Get(id string) (interface{}, error)
|
Get(id string) (interface{}, error)
|
||||||
Delete(id string) (<-chan interface{}, error)
|
Delete(id string) (<-chan interface{}, error)
|
||||||
Extract(body string) (interface{}, error)
|
Extract(body []byte) (interface{}, error)
|
||||||
Create(interface{}) (<-chan interface{}, error)
|
Create(interface{}) (<-chan interface{}, error)
|
||||||
Update(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)
|
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()
|
defer req.Body.Close()
|
||||||
body, err := ioutil.ReadAll(req.Body)
|
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) {
|
func (server *ApiServer) waitForObject(out <-chan interface{}, timeout time.Duration) (interface{}, error) {
|
||||||
|
@ -69,9 +69,9 @@ func (storage *SimpleRESTStorage) Delete(id string) (<-chan interface{}, error)
|
|||||||
return storage.channel, storage.err
|
return storage.channel, storage.err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (storage *SimpleRESTStorage) Extract(body string) (interface{}, error) {
|
func (storage *SimpleRESTStorage) Extract(body []byte) (interface{}, error) {
|
||||||
var item Simple
|
var item Simple
|
||||||
json.Unmarshal([]byte(body), &item)
|
json.Unmarshal(body, &item)
|
||||||
return item, storage.err
|
return item, storage.err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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)
|
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{}
|
result := api.ReplicationController{}
|
||||||
err := json.Unmarshal([]byte(body), &result)
|
err := json.Unmarshal(body, &result)
|
||||||
result.Kind = "cluster#replicationController"
|
result.Kind = "cluster#replicationController"
|
||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
@ -123,7 +123,7 @@ func TestExtractControllerJson(t *testing.T) {
|
|||||||
}
|
}
|
||||||
body, err := json.Marshal(controller)
|
body, err := json.Marshal(controller)
|
||||||
expectNoError(t, err)
|
expectNoError(t, err)
|
||||||
controllerOut, err := storage.Extract(string(body))
|
controllerOut, err := storage.Extract(body)
|
||||||
expectNoError(t, err)
|
expectNoError(t, err)
|
||||||
// Extract adds a Kind
|
// Extract adds a Kind
|
||||||
controller.Kind = "cluster#replicationController"
|
controller.Kind = "cluster#replicationController"
|
||||||
|
@ -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)
|
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{}
|
pod := api.Pod{}
|
||||||
err := json.Unmarshal([]byte(body), &pod)
|
err := json.Unmarshal(body, &pod)
|
||||||
pod.Kind = "cluster#pod"
|
pod.Kind = "cluster#pod"
|
||||||
return pod, err
|
return pod, err
|
||||||
}
|
}
|
||||||
|
@ -104,7 +104,7 @@ func TestExtractJson(t *testing.T) {
|
|||||||
}
|
}
|
||||||
body, err := json.Marshal(pod)
|
body, err := json.Marshal(pod)
|
||||||
expectNoError(t, err)
|
expectNoError(t, err)
|
||||||
podOut, err := storage.Extract(string(body))
|
podOut, err := storage.Extract(body)
|
||||||
expectNoError(t, err)
|
expectNoError(t, err)
|
||||||
// Extract adds in a kind
|
// Extract adds in a kind
|
||||||
pod.Kind = "cluster#pod"
|
pod.Kind = "cluster#pod"
|
||||||
|
@ -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)
|
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
|
var svc api.Service
|
||||||
err := json.Unmarshal([]byte(body), &svc)
|
err := json.Unmarshal([]byte(body), &svc)
|
||||||
svc.Kind = "cluster#service"
|
svc.Kind = "cluster#service"
|
||||||
|
Loading…
Reference in New Issue
Block a user