fix(cabana): show to-many relation list columns joined with commas
This commit is contained in:
@@ -580,16 +580,48 @@ func relatedSelect(row reflect.Value, controller pact.AdminController, col ListC
|
||||
}
|
||||
field = field.Elem()
|
||||
}
|
||||
column := listRelationColumn(controller, col.Relation, col.Select)
|
||||
if field.Kind() == reflect.Slice {
|
||||
return relatedSelectMany(field, column)
|
||||
}
|
||||
if field.Kind() != reflect.Struct {
|
||||
return nil, false
|
||||
}
|
||||
selected := fieldByColumn(field, listRelationColumn(controller, col.Relation, col.Select))
|
||||
selected := fieldByColumn(field, column)
|
||||
if !selected.IsValid() || !selected.CanInterface() {
|
||||
return nil, false
|
||||
}
|
||||
return selected.Interface(), true
|
||||
}
|
||||
|
||||
// relatedSelectMany joins the selected column of every related record with
|
||||
// ", ", as Winter's list does for a to-many relation column. No related
|
||||
// records leave the key unset, so the list shows its empty placeholder.
|
||||
func relatedSelectMany(records reflect.Value, column string) (any, bool) {
|
||||
values := make([]string, 0, records.Len())
|
||||
for i := 0; i < records.Len(); i++ {
|
||||
record := records.Index(i)
|
||||
for record.Kind() == reflect.Pointer && !record.IsNil() {
|
||||
record = record.Elem()
|
||||
}
|
||||
if record.Kind() != reflect.Struct {
|
||||
continue
|
||||
}
|
||||
selected := fieldByColumn(record, column)
|
||||
for selected.IsValid() && selected.Kind() == reflect.Pointer && !selected.IsNil() {
|
||||
selected = selected.Elem()
|
||||
}
|
||||
if !selected.IsValid() || !selected.CanInterface() || selected.Kind() == reflect.Pointer {
|
||||
continue
|
||||
}
|
||||
values = append(values, fmt.Sprint(selected.Interface()))
|
||||
}
|
||||
if len(values) == 0 {
|
||||
return nil, false
|
||||
}
|
||||
return strings.Join(values, ", "), true
|
||||
}
|
||||
|
||||
func listRelationColumn(controller pact.AdminController, relation, column string) string {
|
||||
mapper, ok := controller.(pact.ListRelationColumnMapper)
|
||||
if !ok || mapper == nil {
|
||||
|
||||
Reference in New Issue
Block a user