Make apiserver work with new encode/decode

This commit is contained in:
Daniel Smith 2014-06-20 16:18:36 -07:00
parent d7b4915111
commit 14361e336a
2 changed files with 15 additions and 7 deletions

View File

@ -17,7 +17,6 @@ limitations under the License.
package apiserver package apiserver
import ( import (
"encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
@ -27,6 +26,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels" "github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util" "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) { func (server *ApiServer) write(statusCode int, object interface{}, w http.ResponseWriter) {
w.WriteHeader(statusCode) w.WriteHeader(statusCode)
output, err := json.MarshalIndent(object, "", " ") output, err := api.EncodeIndent(object)
if err != nil { if err != nil {
server.error(err, w) server.error(err, w)
return return

View File

@ -26,9 +26,14 @@ import (
"sync" "sync"
"testing" "testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels" "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. // TODO: This doesn't reduce typing enough to make it worth the less readable errors. Remove.
func expectNoError(t *testing.T, err error) { func expectNoError(t *testing.T, err error) {
if err != nil { if err != nil {
@ -37,11 +42,13 @@ func expectNoError(t *testing.T, err error) {
} }
type Simple struct { type Simple struct {
Name string JSONBase api.JSONBase `json:",inline"`
Name string
} }
type SimpleList struct { type SimpleList struct {
Items []Simple JSONBase api.JSONBase `json:",inline"`
Items []Simple
} }
type SimpleRESTStorage struct { type SimpleRESTStorage struct {
@ -54,7 +61,7 @@ type SimpleRESTStorage struct {
} }
func (storage *SimpleRESTStorage) List(labels.Selector) (interface{}, error) { func (storage *SimpleRESTStorage) List(labels.Selector) (interface{}, error) {
result := SimpleList{ result := &SimpleList{
Items: storage.list, Items: storage.list,
} }
return result, storage.err 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) { func (storage *SimpleRESTStorage) Extract(body []byte) (interface{}, error) {
var item Simple var item Simple
json.Unmarshal(body, &item) api.DecodeInto(body, &item)
return item, storage.err return item, storage.err
} }
@ -90,7 +97,7 @@ func extractBody(response *http.Response, object interface{}) (string, error) {
if err != nil { if err != nil {
return string(body), err return string(body), err
} }
err = json.Unmarshal(body, object) err = api.DecodeInto(body, object)
return string(body), err return string(body), err
} }
@ -150,6 +157,7 @@ func TestNonEmptyList(t *testing.T) {
body, err := extractBody(resp, &listOut) body, err := extractBody(resp, &listOut)
if len(listOut.Items) != 1 { if len(listOut.Items) != 1 {
t.Errorf("Unexpected response: %#v", listOut) t.Errorf("Unexpected response: %#v", listOut)
return
} }
if listOut.Items[0].Name != simpleStorage.list[0].Name { if listOut.Items[0].Name != simpleStorage.list[0].Name {
t.Errorf("Unexpected data: %#v, %s", listOut.Items[0], string(body)) t.Errorf("Unexpected data: %#v, %s", listOut.Items[0], string(body))