Redo Unstructured to have accessor methods
Add accessor methods that implement pkg/api/unversioned.ObjectKind, pkg/api/meta.Object, pkg/api/meta.Type and pkg/api/meta.List. Removed the convenience fields since writing to them was not reflected in serialized JSON.
This commit is contained in:
@@ -56,17 +56,13 @@ func (unstructuredJSONScheme) EncodeToStream(obj Object, w io.Writer, overrides
|
||||
case *Unstructured:
|
||||
return json.NewEncoder(w).Encode(t.Object)
|
||||
case *UnstructuredList:
|
||||
type encodeList struct {
|
||||
TypeMeta `json:",inline"`
|
||||
Items []map[string]interface{} `json:"items"`
|
||||
}
|
||||
eList := encodeList{
|
||||
TypeMeta: t.TypeMeta,
|
||||
}
|
||||
var items []map[string]interface{}
|
||||
for _, i := range t.Items {
|
||||
eList.Items = append(eList.Items, i.Object)
|
||||
items = append(items, i.Object)
|
||||
}
|
||||
return json.NewEncoder(w).Encode(eList)
|
||||
t.Object["items"] = items
|
||||
defer func() { delete(t.Object, "items") }()
|
||||
return json.NewEncoder(w).Encode(t.Object)
|
||||
case *Unknown:
|
||||
// TODO: Unstructured needs to deal with ContentType.
|
||||
_, err := w.Write(t.Raw)
|
||||
@@ -113,25 +109,6 @@ func (unstructuredJSONScheme) decodeToUnstructured(data []byte, unstruct *Unstru
|
||||
return err
|
||||
}
|
||||
|
||||
if v, ok := m["kind"]; ok {
|
||||
if s, ok := v.(string); ok {
|
||||
unstruct.Kind = s
|
||||
}
|
||||
}
|
||||
if v, ok := m["apiVersion"]; ok {
|
||||
if s, ok := v.(string); ok {
|
||||
unstruct.APIVersion = s
|
||||
}
|
||||
}
|
||||
if metadata, ok := m["metadata"]; ok {
|
||||
if metadata, ok := metadata.(map[string]interface{}); ok {
|
||||
if name, ok := metadata["name"]; ok {
|
||||
if name, ok := name.(string); ok {
|
||||
unstruct.Name = name
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
unstruct.Object = m
|
||||
|
||||
return nil
|
||||
@@ -139,8 +116,7 @@ func (unstructuredJSONScheme) decodeToUnstructured(data []byte, unstruct *Unstru
|
||||
|
||||
func (s unstructuredJSONScheme) decodeToList(data []byte, list *UnstructuredList) error {
|
||||
type decodeList struct {
|
||||
TypeMeta `json:",inline"`
|
||||
Items []gojson.RawMessage
|
||||
Items []gojson.RawMessage
|
||||
}
|
||||
|
||||
var dList decodeList
|
||||
@@ -148,7 +124,11 @@ func (s unstructuredJSONScheme) decodeToList(data []byte, list *UnstructuredList
|
||||
return err
|
||||
}
|
||||
|
||||
list.TypeMeta = dList.TypeMeta
|
||||
if err := json.Unmarshal(data, &list.Object); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
delete(list.Object, "items")
|
||||
list.Items = nil
|
||||
for _, i := range dList.Items {
|
||||
unstruct := &Unstructured{}
|
||||
|
||||
Reference in New Issue
Block a user