Skip to content

Commit ac91358

Browse files
authored
Merge pull request #196 from klihub/eliminate-multierror
pkg,schema,internal: eliminate multierror.
2 parents 2c40f2d + 7ce7168 commit ac91358

File tree

9 files changed

+27
-142
lines changed

9 files changed

+27
-142
lines changed

.github/workflows/sanity.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ on: [push, pull_request]
22
name: Sanity
33

44
env:
5-
GO_VERSION: '1.19.x'
5+
GO_VERSION: '1.20.x'
66

77
jobs:
88
build:

cmd/cdi/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module tags.cncf.io/container-device-interface/cmd/cdi
22

3-
go 1.19
3+
go 1.20
44

55
require (
66
github.com/fsnotify/fsnotify v1.5.1

cmd/validate/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module tags.cncf.io/container-device-interface/cmd/validate
22

3-
go 1.19
3+
go 1.20
44

55
require tags.cncf.io/container-device-interface v0.0.0
66

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module tags.cncf.io/container-device-interface
22

3-
go 1.19
3+
go 1.20
44

55
require (
66
github.com/fsnotify/fsnotify v1.5.1

internal/multierror/multierror.go

Lines changed: 0 additions & 82 deletions
This file was deleted.

internal/multierror/multierror_test.go

Lines changed: 0 additions & 38 deletions
This file was deleted.

internal/validation/k8s/objectmeta.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,27 @@ limitations under the License.
2020
package k8s
2121

2222
import (
23+
"errors"
2324
"fmt"
2425
"strings"
25-
26-
"tags.cncf.io/container-device-interface/internal/multierror"
2726
)
2827

2928
// TotalAnnotationSizeLimitB defines the maximum size of all annotations in characters.
3029
const TotalAnnotationSizeLimitB int = 256 * (1 << 10) // 256 kB
3130

3231
// ValidateAnnotations validates that a set of annotations are correctly defined.
3332
func ValidateAnnotations(annotations map[string]string, path string) error {
34-
errors := multierror.New()
33+
errs := []error{}
3534
for k := range annotations {
3635
// The rule is QualifiedName except that case doesn't matter, so convert to lowercase before checking.
3736
for _, msg := range IsQualifiedName(strings.ToLower(k)) {
38-
errors = multierror.Append(errors, fmt.Errorf("%v.%v is invalid: %v", path, k, msg))
37+
errs = append(errs, fmt.Errorf("%v.%v is invalid: %v", path, k, msg))
3938
}
4039
}
4140
if err := ValidateAnnotationsSize(annotations); err != nil {
42-
errors = multierror.Append(errors, fmt.Errorf("%v is too long: %v", path, err))
41+
errs = append(errs, fmt.Errorf("%v is too long: %v", path, err))
4342
}
44-
return errors
43+
return errors.Join(errs...)
4544
}
4645

4746
// ValidateAnnotationsSize validates that a set of annotations is not too large.

pkg/cdi/cache.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828

2929
"github.com/fsnotify/fsnotify"
3030
oci "github.com/opencontainers/runtime-spec/specs-go"
31-
"tags.cncf.io/container-device-interface/internal/multierror"
3231
cdi "tags.cncf.io/container-device-interface/specs-go"
3332
)
3433

@@ -133,11 +132,11 @@ func (c *Cache) Refresh() error {
133132
}
134133

135134
// collect and return cached errors, much like refresh() does it
136-
var result error
137-
for _, errors := range c.errors {
138-
result = multierror.Append(result, errors...)
135+
errs := []error{}
136+
for _, specErrs := range c.errors {
137+
errs = append(errs, errors.Join(specErrs...))
139138
}
140-
return result
139+
return errors.Join(errs...)
141140
}
142141

143142
// Refresh the Cache by rescanning CDI Spec directories and files.
@@ -147,12 +146,10 @@ func (c *Cache) refresh() error {
147146
devices = map[string]*Device{}
148147
conflicts = map[string]struct{}{}
149148
specErrors = map[string][]error{}
150-
result []error
151149
)
152150

153151
// collect errors per spec file path and once globally
154152
collectError := func(err error, paths ...string) {
155-
result = append(result, err)
156153
for _, path := range paths {
157154
specErrors[path] = append(specErrors[path], err)
158155
}
@@ -205,7 +202,11 @@ func (c *Cache) refresh() error {
205202
c.devices = devices
206203
c.errors = specErrors
207204

208-
return multierror.New(result...)
205+
errs := []error{}
206+
for _, specErrs := range specErrors {
207+
errs = append(errs, errors.Join(specErrs...))
208+
}
209+
return errors.Join(errs...)
209210
}
210211

211212
// RefreshIfRequired triggers a refresh if necessary.

schema/schema.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bytes"
2121
"embed"
2222
"encoding/json"
23+
"errors"
2324
"fmt"
2425
"io"
2526
"net/http"
@@ -30,7 +31,6 @@ import (
3031
"sigs.k8s.io/yaml"
3132

3233
schema "github.com/xeipuuv/gojsonschema"
33-
"tags.cncf.io/container-device-interface/internal/multierror"
3434
"tags.cncf.io/container-device-interface/internal/validation"
3535
)
3636

@@ -333,11 +333,16 @@ func (e *Error) Error() string {
333333
return ""
334334
}
335335

336-
var multi error
336+
errs := []error{}
337337
for _, err := range e.Result.Errors() {
338-
multi = multierror.Append(multi, fmt.Errorf("%v", err))
338+
errs = append(errs, fmt.Errorf("%v", err))
339339
}
340-
return multi.Error()
340+
341+
if err := errors.Join(errs...); err != nil {
342+
return fmt.Sprintf("%v", err)
343+
}
344+
345+
return ""
341346
}
342347

343348
var (

0 commit comments

Comments
 (0)