Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite6f568f

Browse files
authored
refactor: cli: address comments from#4240 (#4259)
1 parent028a4ed commite6f568f

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

‎cli/cliui/table.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func DisplayTable(out any, sort string, filterColumns []string) (string, error)
6767
}
6868

6969
// Get the list of table column headers.
70-
headersRaw,err:=TypeToTableHeaders(v.Type().Elem())
70+
headersRaw,err:=typeToTableHeaders(v.Type().Elem())
7171
iferr!=nil {
7272
return"",xerrors.Errorf("get table headers recursively for type %q: %w",v.Type().Elem().String(),err)
7373
}
@@ -207,10 +207,10 @@ func isStructOrStructPointer(t reflect.Type) bool {
207207
returnt.Kind()==reflect.Struct|| (t.Kind()==reflect.Pointer&&t.Elem().Kind()==reflect.Struct)
208208
}
209209

210-
//TypeToTableHeaders converts a type to a slice of column names. If the given
210+
//typeToTableHeaders converts a type to a slice of column names. If the given
211211
// type is invalid (not a struct or a pointer to a struct, has invalid table
212212
// tags, etc.), an error is returned.
213-
funcTypeToTableHeaders(t reflect.Type) ([]string,error) {
213+
functypeToTableHeaders(t reflect.Type) ([]string,error) {
214214
if!isStructOrStructPointer(t) {
215215
returnnil,xerrors.Errorf("typeToTableHeaders called with a non-struct or a non-pointer-to-a-struct type")
216216
}
@@ -235,7 +235,7 @@ func TypeToTableHeaders(t reflect.Type) ([]string, error) {
235235
returnnil,xerrors.Errorf("field %q in type %q is marked as recursive but does not contain a struct or a pointer to a struct",field.Name,t.String())
236236
}
237237

238-
childNames,err:=TypeToTableHeaders(fieldType)
238+
childNames,err:=typeToTableHeaders(fieldType)
239239
iferr!=nil {
240240
returnnil,xerrors.Errorf("get child field header names for field %q in type %q: %w",field.Name,fieldType.String(),err)
241241
}
@@ -305,3 +305,18 @@ func valueToTableMap(val reflect.Value) (map[string]any, error) {
305305

306306
returnrow,nil
307307
}
308+
309+
// TableHeaders returns the table header names of all
310+
// fields in tSlice. tSlice must be a slice of some type.
311+
funcTableHeaders(tSliceany) ([]string,error) {
312+
v:=reflect.Indirect(reflect.ValueOf(tSlice))
313+
rawHeaders,err:=typeToTableHeaders(v.Type().Elem())
314+
iferr!=nil {
315+
returnnil,xerrors.Errorf("type to table headers: %w",err)
316+
}
317+
out:=make([]string,0,len(rawHeaders))
318+
for_,hdr:=rangerawHeaders {
319+
out=append(out,strings.Replace(hdr," ","_",-1))
320+
}
321+
returnout,nil
322+
}

‎cli/cliui/table_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,28 @@ baz baz1 baz3 Aug 2 15:49:10
327327
})
328328
}
329329

330+
funcTest_TableHeaders(t*testing.T) {
331+
t.Parallel()
332+
s:= []tableTest1{}
333+
expectedFields:= []string{
334+
"name",
335+
"age",
336+
"roles",
337+
"sub_1_name",
338+
"sub_1_age",
339+
"sub_2_name",
340+
"sub_2_age",
341+
"sub_3_inner_name",
342+
"sub_3_inner_age",
343+
"sub_4",
344+
"time",
345+
"time_ptr",
346+
}
347+
headers,err:=cliui.TableHeaders(s)
348+
require.NoError(t,err)
349+
require.EqualValues(t,expectedFields,headers)
350+
}
351+
330352
// compareTables normalizes the incoming table lines
331353
funccompareTables(t*testing.T,expected,outstring) {
332354
t.Helper()

‎cli/list.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cli
22

33
import (
44
"fmt"
5-
"reflect"
65
"strings"
76
"time"
87

@@ -128,14 +127,10 @@ func list() *cobra.Command {
128127
},
129128
}
130129

131-
v:=reflect.Indirect(reflect.ValueOf(displayWorkspaces))
132-
availColumns,err:=cliui.TypeToTableHeaders(v.Type().Elem())
130+
availColumns,err:=cliui.TableHeaders(displayWorkspaces)
133131
iferr!=nil {
134132
panic(err)
135133
}
136-
fori,s:=rangeavailColumns {
137-
availColumns[i]=strings.Replace(s," ","_",-1)
138-
}
139134
columnString:=strings.Join(availColumns[:],", ")
140135

141136
cmd.Flags().BoolVarP(&all,"all","a",false,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp