Remove unnecessary roundtripping in get.go

This commit is contained in:
Maciej Szulik 2018-05-15 22:15:23 +02:00
parent dc62a73742
commit 582128a837
No known key found for this signature in database
GPG Key ID: F15E55D276FA84C4

View File

@ -343,7 +343,7 @@ func (o *GetOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []string) e
objs := make([]runtime.Object, len(infos)) objs := make([]runtime.Object, len(infos))
for ix := range infos { for ix := range infos {
if o.ServerPrint { if o.ServerPrint {
table, err := o.decodeIntoTable(cmdutil.InternalVersionJSONEncoder(), infos[ix].Object) table, err := o.decodeIntoTable(infos[ix].Object)
if err == nil { if err == nil {
infos[ix].Object = table infos[ix].Object = table
} else { } else {
@ -589,34 +589,28 @@ func attemptToConvertToInternal(obj runtime.Object, converter runtime.ObjectConv
return internalObject return internalObject
} }
func (o *GetOptions) decodeIntoTable(encoder runtime.Encoder, obj runtime.Object) (runtime.Object, error) { func (o *GetOptions) decodeIntoTable(obj runtime.Object) (runtime.Object, error) {
if obj.GetObjectKind().GroupVersionKind().Kind != "Table" { if obj.GetObjectKind().GroupVersionKind().Kind != "Table" {
return nil, fmt.Errorf("attempt to decode non-Table object into a v1beta1.Table") return nil, fmt.Errorf("attempt to decode non-Table object into a v1beta1.Table")
} }
b, err := runtime.Encode(encoder, obj) unstr, ok := obj.(*unstructured.Unstructured)
if err != nil { if !ok {
return nil, err return nil, fmt.Errorf("attempt to decode non-Unstructured object")
} }
table := &metav1beta1.Table{} table := &metav1beta1.Table{}
err = json.Unmarshal(b, table) if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstr.Object, table); err != nil {
if err != nil {
return nil, err return nil, err
} }
for i := range table.Rows { for i := range table.Rows {
row := &table.Rows[i] row := &table.Rows[i]
if row.Object.Raw == nil || row.Object.Object != nil { if row.Object.Raw == nil || row.Object.Object != nil {
//if row already has Object.Object
//we don't change it
continue continue
} }
converted, err := runtime.Decode(unstructured.UnstructuredJSONScheme, row.Object.Raw) converted, err := runtime.Decode(unstructured.UnstructuredJSONScheme, row.Object.Raw)
if err != nil { if err != nil {
//if error happens, we just continue return nil, err
continue
} }
row.Object.Object = converted row.Object.Object = converted
} }