Skip to content

Commit

Permalink
Use stock errors.Join in place of custom allerrors type. (openconfig#…
Browse files Browse the repository at this point in the history
…2659)

* Use stock errors.Join in place of custom allerrors type.

* Use len instead of nil check for empty
  • Loading branch information
greg-dennis authored and frasieroh committed Feb 13, 2024
1 parent dd23cbc commit 9cce150
Showing 1 changed file with 9 additions and 35 deletions.
44 changes: 9 additions & 35 deletions topologies/binding/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"net"
"net/http"
"os"
"strings"
"time"

grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
Expand Down Expand Up @@ -276,31 +275,8 @@ func (a *staticATE) DialIxNetwork(ctx context.Context) (*binding.IxNetwork, erro
return &binding.IxNetwork{Session: ixs}, nil
}

// allerrors implements the error interface and will accumulate and
// report all errors.
type allerrors []error

var _ = error(allerrors{})

func (errs allerrors) Error() string {
// Shortcut for no error or a single error.
switch len(errs) {
case 0:
return ""
case 1:
return errs[0].Error()
}
var b strings.Builder
fmt.Fprintf(&b, "%d errors occurred:", len(errs))
for _, err := range errs {
// Replace indentation for proper nesting.
fmt.Fprintf(&b, "\n * %s", strings.ReplaceAll(err.Error(), "\n", "\n "))
}
return b.String()
}

func reservation(tb *opb.Testbed, r resolver) (*binding.Reservation, error) {
var errs allerrors
var errs []error

duts := make(map[string]binding.DUT)
for _, tdut := range tb.Duts {
Expand Down Expand Up @@ -352,15 +328,13 @@ func reservation(tb *opb.Testbed, r resolver) (*binding.Reservation, error) {
}
}

if errs != nil {
return nil, errs
if len(errs) > 0 {
return nil, errors.Join(errs...)
}

resv := &binding.Reservation{
return &binding.Reservation{
DUTs: duts,
ATEs: ates,
}
return resv, nil
}, nil
}

func dims(td *opb.Device, bd *bindpb.Device) (*binding.Dims, error) {
Expand Down Expand Up @@ -400,8 +374,6 @@ func dims(td *opb.Device, bd *bindpb.Device) (*binding.Dims, error) {
}

func ports(tports []*opb.Port, bports []*bindpb.Port) (map[string]*binding.Port, error) {
var errs allerrors

portmap := make(map[string]*binding.Port)
for _, tport := range tports {
portmap[tport.Id] = &binding.Port{
Expand All @@ -427,14 +399,16 @@ func ports(tports []*opb.Port, bports []*bindpb.Port) (map[string]*binding.Port,
}
}
}

var errs []error
for id, p := range portmap {
if p.Name == "" {
errs = append(errs, fmt.Errorf("testbed port %q is missing in binding", id))
}
}

if errs != nil {
return nil, errs
if len(errs) > 0 {
return nil, errors.Join(errs...)
}
return portmap, nil
}
Expand Down

0 comments on commit 9cce150

Please sign in to comment.