Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions internal/commands/checkouts/checkouts.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,9 @@ func listCheckouts(ctx context.Context, cmd *cli.Command) error {
return display.PrintJSON(appCtx.Output, checkoutList)
}

rows := make([][]attribute.Value, 0, len(*checkoutList))
for _, checkout := range *checkoutList {
checkouts := util.SliceOrEmpty(checkoutList)
rows := make([][]attribute.Value, 0, len(checkouts))
for _, checkout := range checkouts {
status := "-"
if checkout.Status != nil {
status = string(*checkout.Status)
Expand Down
5 changes: 3 additions & 2 deletions internal/commands/customers/customers.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,9 @@ func listPaymentInstruments(ctx context.Context, cmd *cli.Command) error {
return display.PrintJSON(appCtx.Output, instruments)
}

rows := make([][]attribute.Value, 0, len(*instruments))
for _, instrument := range *instruments {
paymentInstruments := util.SliceOrEmpty(instruments)
rows := make([][]attribute.Value, 0, len(paymentInstruments))
for _, instrument := range paymentInstruments {
rows = append(rows, []attribute.Value{
attribute.OptionalStringValue(instrument.Token),
attribute.OptionalValue(instrument.Type),
Expand Down
6 changes: 4 additions & 2 deletions internal/commands/payouts/payouts.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/sumup/sumup-go/datetime"

"github.com/sumup/sumup-cli/internal/app"
"github.com/sumup/sumup-cli/internal/commands/util"
"github.com/sumup/sumup-cli/internal/display"
"github.com/sumup/sumup-cli/internal/display/attribute"
)
Expand Down Expand Up @@ -97,8 +98,9 @@ func listPayouts(ctx context.Context, cmd *cli.Command) error {
return display.PrintJSON(appCtx.Output, payoutList)
}

rows := make([][]attribute.Value, 0, len(*payoutList))
for _, payout := range *payoutList {
payouts := util.SliceOrEmpty(payoutList)
rows := make([][]attribute.Value, 0, len(payouts))
for _, payout := range payouts {
fee := attribute.OptionalValue(payout.Fee)
if payout.Fee != nil {
fee = attribute.ValueOf(fmt.Sprintf("%.2f", *payout.Fee))
Expand Down
7 changes: 7 additions & 0 deletions internal/commands/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,10 @@ func BoolLabel(value *bool) string {
}
return "No"
}

func SliceOrEmpty[S ~[]E, E any](value *S) S {
if value == nil {
return S{}
}
return *value
}
14 changes: 14 additions & 0 deletions internal/commands/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ func TestBoolLabel(t *testing.T) {
})
}

func TestSliceOrEmpty(t *testing.T) {
t.Parallel()

t.Run("returns empty slice for nil pointer", func(t *testing.T) {
var values *[]string
assert.Empty(t, util.SliceOrEmpty(values))
})

t.Run("returns dereferenced slice when present", func(t *testing.T) {
values := []string{"a", "b"}
assert.Equal(t, values, util.SliceOrEmpty(&values))
})
}

func TestTimeOrDash(t *testing.T) {
t.Parallel()

Expand Down