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

refactor: cli: address comments from #4240#4259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
johnstcn merged 2 commits intomainfromcj/4240-fixup
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletionscli/cliui/table.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -67,7 +67,7 @@ func DisplayTable(out any, sort string, filterColumns []string) (string, error)
}

// Get the list of table column headers.
headersRaw, err :=TypeToTableHeaders(v.Type().Elem())
headersRaw, err :=typeToTableHeaders(v.Type().Elem())
if err != nil {
return "", xerrors.Errorf("get table headers recursively for type %q: %w", v.Type().Elem().String(), err)
}
Expand DownExpand Up@@ -207,10 +207,10 @@ func isStructOrStructPointer(t reflect.Type) bool {
return t.Kind() == reflect.Struct || (t.Kind() == reflect.Pointer && t.Elem().Kind() == reflect.Struct)
}

//TypeToTableHeaders converts a type to a slice of column names. If the given
//typeToTableHeaders converts a type to a slice of column names. If the given
// type is invalid (not a struct or a pointer to a struct, has invalid table
// tags, etc.), an error is returned.
funcTypeToTableHeaders(t reflect.Type) ([]string, error) {
functypeToTableHeaders(t reflect.Type) ([]string, error) {
if !isStructOrStructPointer(t) {
return nil, xerrors.Errorf("typeToTableHeaders called with a non-struct or a non-pointer-to-a-struct type")
}
Expand All@@ -235,7 +235,7 @@ func TypeToTableHeaders(t reflect.Type) ([]string, error) {
return nil, 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())
}

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

return row, nil
}

// TableHeaders returns the table header names of all
// fields in tSlice. tSlice must be a slice of some type.
func TableHeaders(tSlice any) ([]string, error) {
v := reflect.Indirect(reflect.ValueOf(tSlice))
rawHeaders, err := typeToTableHeaders(v.Type().Elem())
if err != nil {
return nil, xerrors.Errorf("type to table headers: %w", err)
}
out := make([]string, 0, len(rawHeaders))
for _, hdr := range rawHeaders {
out = append(out, strings.Replace(hdr, " ", "_", -1))
}
return out, nil
}
22 changes: 22 additions & 0 deletionscli/cliui/table_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -327,6 +327,28 @@ baz baz1 baz3 Aug 2 15:49:10
})
}

func Test_TableHeaders(t *testing.T) {
t.Parallel()
s := []tableTest1{}
expectedFields := []string{
"name",
"age",
"roles",
"sub_1_name",
"sub_1_age",
"sub_2_name",
"sub_2_age",
"sub_3_inner_name",
"sub_3_inner_age",
"sub_4",
"time",
"time_ptr",
}
headers, err := cliui.TableHeaders(s)
require.NoError(t, err)
require.EqualValues(t, expectedFields, headers)
}

// compareTables normalizes the incoming table lines
func compareTables(t *testing.T, expected, out string) {
t.Helper()
Expand Down
7 changes: 1 addition & 6 deletionscli/list.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,7 +2,6 @@ package cli

import (
"fmt"
"reflect"
"strings"
"time"

Expand DownExpand Up@@ -128,14 +127,10 @@ func list() *cobra.Command {
},
}

v := reflect.Indirect(reflect.ValueOf(displayWorkspaces))
availColumns, err := cliui.TypeToTableHeaders(v.Type().Elem())
availColumns, err := cliui.TableHeaders(displayWorkspaces)
if err != nil {
panic(err)
}
for i, s := range availColumns {
availColumns[i] = strings.Replace(s, " ", "_", -1)
}
columnString := strings.Join(availColumns[:], ", ")

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

[8]ページ先頭

©2009-2025 Movatter.jp