Skip to content

Commit 114dfbb

Browse files
committed
feat: add temporary errors package until we drop Go <1.20
Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
1 parent 57a3d74 commit 114dfbb

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

internal/errors/errors.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package errors
2+
3+
import "errors"
4+
5+
func New(text string) error {
6+
return errors.New(text)
7+
}

internal/errors/join.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//go:build go1.20
2+
3+
package errors
4+
5+
import "errors"
6+
7+
func Join(errs ...error) error {
8+
return errors.Join(errs...)
9+
}

internal/errors/join_go1_19.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//go:build !go1.20
2+
3+
// Copyright 2022 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package errors
8+
9+
// Join returns an error that wraps the given errors.
10+
// Any nil error values are discarded.
11+
// Join returns nil if every value in errs is nil.
12+
// The error formats as the concatenation of the strings obtained
13+
// by calling the Error method of each element of errs, with a newline
14+
// between each string.
15+
//
16+
// A non-nil error returned by Join implements the Unwrap() []error method.
17+
func Join(errs ...error) error {
18+
n := 0
19+
for _, err := range errs {
20+
if err != nil {
21+
n++
22+
}
23+
}
24+
if n == 0 {
25+
return nil
26+
}
27+
e := &joinError{
28+
errs: make([]error, 0, n),
29+
}
30+
for _, err := range errs {
31+
if err != nil {
32+
e.errs = append(e.errs, err)
33+
}
34+
}
35+
return e
36+
}
37+
38+
type joinError struct {
39+
errs []error
40+
}
41+
42+
func (e *joinError) Error() string {
43+
// Since Join returns nil if every value in errs is nil,
44+
// e.errs cannot be empty.
45+
if len(e.errs) == 1 {
46+
return e.errs[0].Error()
47+
}
48+
49+
b := []byte(e.errs[0].Error())
50+
for _, err := range e.errs[1:] {
51+
b = append(b, '\n')
52+
b = append(b, err.Error()...)
53+
}
54+
// At this point, b has at least one byte '\n'.
55+
// return unsafe.String(&b[0], len(b))
56+
return string(b)
57+
}
58+
59+
func (e *joinError) Unwrap() []error {
60+
return e.errs
61+
}

mapstructure.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,13 @@ package mapstructure
160160

161161
import (
162162
"encoding/json"
163-
"errors"
164163
"fmt"
165164
"reflect"
166165
"sort"
167166
"strconv"
168167
"strings"
168+
169+
"github.com/go-viper/mapstructure/v2/internal/errors"
169170
)
170171

171172
// DecodeHookFunc is the callback function that can be used for

0 commit comments

Comments
 (0)