@@ -67,7 +67,7 @@ func DisplayTable(out any, sort string, filterColumns []string) (string, error)
67
67
}
68
68
69
69
// Get the list of table column headers.
70
- headersRaw , err := TypeToTableHeaders (v .Type ().Elem ())
70
+ headersRaw , err := typeToTableHeaders (v .Type ().Elem ())
71
71
if err != nil {
72
72
return "" , xerrors .Errorf ("get table headers recursively for type %q: %w" , v .Type ().Elem ().String (), err )
73
73
}
@@ -207,10 +207,10 @@ func isStructOrStructPointer(t reflect.Type) bool {
207
207
return t .Kind () == reflect .Struct || (t .Kind () == reflect .Pointer && t .Elem ().Kind () == reflect .Struct )
208
208
}
209
209
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
211
211
// type is invalid (not a struct or a pointer to a struct, has invalid table
212
212
// tags, etc.), an error is returned.
213
- func TypeToTableHeaders (t reflect.Type ) ([]string , error ) {
213
+ func typeToTableHeaders (t reflect.Type ) ([]string , error ) {
214
214
if ! isStructOrStructPointer (t ) {
215
215
return nil , xerrors .Errorf ("typeToTableHeaders called with a non-struct or a non-pointer-to-a-struct type" )
216
216
}
@@ -235,7 +235,7 @@ func TypeToTableHeaders(t reflect.Type) ([]string, error) {
235
235
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 ())
236
236
}
237
237
238
- childNames , err := TypeToTableHeaders (fieldType )
238
+ childNames , err := typeToTableHeaders (fieldType )
239
239
if err != nil {
240
240
return nil , xerrors .Errorf ("get child field header names for field %q in type %q: %w" , field .Name , fieldType .String (), err )
241
241
}
@@ -305,3 +305,18 @@ func valueToTableMap(val reflect.Value) (map[string]any, error) {
305
305
306
306
return row , nil
307
307
}
308
+
309
+ // TableHeaders returns the table header names of all
310
+ // fields in tSlice. tSlice must be a slice of some type.
311
+ func TableHeaders (tSlice any ) ([]string , error ) {
312
+ v := reflect .Indirect (reflect .ValueOf (tSlice ))
313
+ rawHeaders , err := typeToTableHeaders (v .Type ().Elem ())
314
+ if err != nil {
315
+ return nil , xerrors .Errorf ("type to table headers: %w" , err )
316
+ }
317
+ out := make ([]string , 0 , len (rawHeaders ))
318
+ for _ , hdr := range rawHeaders {
319
+ out = append (out , strings .Replace (hdr , " " , "_" , - 1 ))
320
+ }
321
+ return out , nil
322
+ }
0 commit comments