@@ -70,7 +70,7 @@ func DisplayTable(out any, sort string, filterColumns []string) (string, error)
70
70
}
71
71
72
72
// Get the list of table column headers.
73
- headersRaw , defaultSort , err := typeToTableHeaders (v .Type ().Elem ())
73
+ headersRaw , defaultSort , err := typeToTableHeaders (v .Type ().Elem (), true )
74
74
if err != nil {
75
75
return "" , xerrors .Errorf ("get table headers recursively for type %q: %w" , v .Type ().Elem ().String (), err )
76
76
}
@@ -230,7 +230,10 @@ func isStructOrStructPointer(t reflect.Type) bool {
230
230
// typeToTableHeaders converts a type to a slice of column names. If the given
231
231
// type is invalid (not a struct or a pointer to a struct, has invalid table
232
232
// tags, etc.), an error is returned.
233
- func typeToTableHeaders (t reflect.Type ) ([]string , string , error ) {
233
+ //
234
+ // requireDefault is only needed for the root call. This is recursive, so nested
235
+ // structs do not need the default sort name.
236
+ func typeToTableHeaders (t reflect.Type , requireDefault bool ) ([]string , string , error ) {
234
237
if ! isStructOrStructPointer (t ) {
235
238
return nil , "" , xerrors .Errorf ("typeToTableHeaders called with a non-struct or a non-pointer-to-a-struct type" )
236
239
}
@@ -246,6 +249,12 @@ func typeToTableHeaders(t reflect.Type) ([]string, string, error) {
246
249
if err != nil {
247
250
return nil , "" , xerrors .Errorf ("parse struct tags for field %q in type %q: %w" , field .Name , t .String (), err )
248
251
}
252
+
253
+ if name == "" && (recursive && skip ) {
254
+ return nil , "" , xerrors .Errorf ("a name is required for the field %q. " +
255
+ "recursive_line will ensure this is never shown to the user, but is still needed" , field .Name )
256
+ }
257
+ // If recurse and skip is set, the name is intentionally empty.
249
258
if name == "" {
250
259
continue
251
260
}
@@ -262,7 +271,7 @@ func typeToTableHeaders(t reflect.Type) ([]string, string, error) {
262
271
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 ())
263
272
}
264
273
265
- childNames , _ , err := typeToTableHeaders (fieldType )
274
+ childNames , defaultSort , err := typeToTableHeaders (fieldType , false )
266
275
if err != nil {
267
276
return nil , "" , xerrors .Errorf ("get child field header names for field %q in type %q: %w" , field .Name , fieldType .String (), err )
268
277
}
@@ -273,13 +282,16 @@ func typeToTableHeaders(t reflect.Type) ([]string, string, error) {
273
282
}
274
283
headers = append (headers , fullName )
275
284
}
285
+ if defaultSortName == "" {
286
+ defaultSortName = defaultSort
287
+ }
276
288
continue
277
289
}
278
290
279
291
headers = append (headers , name )
280
292
}
281
293
282
- if defaultSortName == "" {
294
+ if defaultSortName == "" && requireDefault {
283
295
return nil , "" , xerrors .Errorf ("no field marked as default_sort in type %q" , t .String ())
284
296
}
285
297
0 commit comments