Skip to content

Commit 28e8002

Browse files
authored
Merge pull request kubernetes#81848 from seans3/split-handler-entry
Split defaultPrintHandler from handlerEntry in table printing
2 parents bc46e8f + 1bc6fad commit 28e8002

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

pkg/printers/tableprinter.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,13 @@ import (
3535

3636
var _ ResourcePrinter = &HumanReadablePrinter{}
3737

38+
type printHandler struct {
39+
columnDefinitions []metav1beta1.TableColumnDefinition
40+
printFunc reflect.Value
41+
}
42+
3843
var (
39-
statusHandlerEntry = &handlerEntry{
44+
statusHandlerEntry = &printHandler{
4045
columnDefinitions: statusColumnDefinitions,
4146
printFunc: reflect.ValueOf(printStatus),
4247
}
@@ -47,7 +52,7 @@ var (
4752
{Name: "Message", Type: "string"},
4853
}
4954

50-
defaultHandlerEntry = &handlerEntry{
55+
defaultHandlerEntry = &printHandler{
5156
columnDefinitions: objectMetaColumnDefinitions,
5257
printFunc: reflect.ValueOf(printObjectMeta),
5358
}
@@ -142,7 +147,7 @@ func (h *HumanReadablePrinter) PrintObj(obj runtime.Object, output io.Writer) er
142147

143148
// Could not find print handler for "obj"; use the default or status print handler.
144149
// Print with the default or status handler, and use the columns from the last time
145-
var handler *handlerEntry
150+
var handler *printHandler
146151
if _, isStatus := obj.(*metav1.Status); isStatus {
147152
handler = statusHandlerEntry
148153
} else {
@@ -380,7 +385,7 @@ func decorateTable(table *metav1beta1.Table, options PrintOptions) error {
380385
// printRowsForHandlerEntry prints the incremental table output (headers if the current type is
381386
// different from lastType) including all the rows in the object. It returns the current type
382387
// or an error, if any.
383-
func printRowsForHandlerEntry(output io.Writer, handler *handlerEntry, eventType string, obj runtime.Object, options PrintOptions, includeHeaders bool) error {
388+
func printRowsForHandlerEntry(output io.Writer, handler *printHandler, eventType string, obj runtime.Object, options PrintOptions, includeHeaders bool) error {
384389
var results []reflect.Value
385390

386391
args := []reflect.Value{reflect.ValueOf(obj), reflect.ValueOf(options)}

pkg/printers/tableprinter_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func testPrintNamespace(obj *corev1.Namespace, options PrintOptions) ([]metav1be
4141
row := metav1beta1.TableRow{
4242
Object: runtime.RawExtension{Object: obj},
4343
}
44-
row.Cells = append(row.Cells, obj.Name, obj.Status.Phase, "<unknow>")
44+
row.Cells = append(row.Cells, obj.Name, obj.Status.Phase, "<unknown>")
4545
return []metav1beta1.TableRow{row}, nil
4646
}
4747

@@ -50,7 +50,7 @@ func TestPrintRowsForHandlerEntry(t *testing.T) {
5050

5151
testCase := []struct {
5252
name string
53-
h *handlerEntry
53+
h *printHandler
5454
opt PrintOptions
5555
eventType string
5656
obj runtime.Object
@@ -60,7 +60,7 @@ func TestPrintRowsForHandlerEntry(t *testing.T) {
6060
}{
6161
{
6262
name: "no tablecolumndefinition and includeheader flase",
63-
h: &handlerEntry{
63+
h: &printHandler{
6464
columnDefinitions: []metav1beta1.TableColumnDefinition{},
6565
printFunc: printFunc,
6666
},
@@ -69,11 +69,11 @@ func TestPrintRowsForHandlerEntry(t *testing.T) {
6969
ObjectMeta: metav1.ObjectMeta{Name: "test"},
7070
},
7171
includeHeader: false,
72-
expectOut: "test\t\t<unknow>\n",
72+
expectOut: "test\t\t<unknown>\n",
7373
},
7474
{
7575
name: "no tablecolumndefinition and includeheader true",
76-
h: &handlerEntry{
76+
h: &printHandler{
7777
columnDefinitions: []metav1beta1.TableColumnDefinition{},
7878
printFunc: printFunc,
7979
},
@@ -82,11 +82,11 @@ func TestPrintRowsForHandlerEntry(t *testing.T) {
8282
ObjectMeta: metav1.ObjectMeta{Name: "test"},
8383
},
8484
includeHeader: true,
85-
expectOut: "\ntest\t\t<unknow>\n",
85+
expectOut: "\ntest\t\t<unknown>\n",
8686
},
8787
{
8888
name: "have tablecolumndefinition and includeheader true",
89-
h: &handlerEntry{
89+
h: &printHandler{
9090
columnDefinitions: testNamespaceColumnDefinitions,
9191
printFunc: printFunc,
9292
},
@@ -95,11 +95,11 @@ func TestPrintRowsForHandlerEntry(t *testing.T) {
9595
ObjectMeta: metav1.ObjectMeta{Name: "test"},
9696
},
9797
includeHeader: true,
98-
expectOut: "NAME\tSTATUS\tAGE\ntest\t\t<unknow>\n",
98+
expectOut: "NAME\tSTATUS\tAGE\ntest\t\t<unknown>\n",
9999
},
100100
{
101101
name: "with event type",
102-
h: &handlerEntry{
102+
h: &printHandler{
103103
columnDefinitions: testNamespaceColumnDefinitions,
104104
printFunc: printFunc,
105105
},
@@ -109,11 +109,11 @@ func TestPrintRowsForHandlerEntry(t *testing.T) {
109109
ObjectMeta: metav1.ObjectMeta{Name: "test"},
110110
},
111111
includeHeader: true,
112-
expectOut: "EVENT\tNAME\tSTATUS\tAGE\nADDED \ttest\t\t<unknow>\n",
112+
expectOut: "EVENT\tNAME\tSTATUS\tAGE\nADDED \ttest\t\t<unknown>\n",
113113
},
114114
{
115115
name: "print namespace and withnamespace true, should not print header",
116-
h: &handlerEntry{
116+
h: &printHandler{
117117
columnDefinitions: testNamespaceColumnDefinitions,
118118
printFunc: printFunc,
119119
},

0 commit comments

Comments
 (0)