From ab920da1167f1ba0c619d7e2a1307fe893a6998d Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Fri, 10 Nov 2023 10:12:59 +0100 Subject: [PATCH] try fix join issue --- join.go | 74 ------------------------------ join_test.go => join_go120_test.go | 3 ++ 2 files changed, 3 insertions(+), 74 deletions(-) delete mode 100644 join.go rename join_test.go => join_go120_test.go (90%) diff --git a/join.go b/join.go deleted file mode 100644 index e8c8619..0000000 --- a/join.go +++ /dev/null @@ -1,74 +0,0 @@ -//go:build !go1.20 -// +build !go1.20 - -// original code copied from -// https://github.com/go-faster/errors/blob/main/join.go - -package errors - -import "unsafe" - -// Join returns an error that wraps the given errors. -// Any nil error values are discarded. -// Join returns nil if every value in errs is nil. -// The error formats as the concatenation of the strings obtained -// by calling the Error method of each element of errs, with a newline -// between each string. -// -// A non-nil error returned by Join implements the Unwrap() []error method. -func Join(errs ...error) error { - n := 0 - for _, err := range errs { - if err != nil { - n++ - } - } - if n == 0 { - return nil - } - e := &joinError{ - errs: make([]error, 0, n), - } - for _, err := range errs { - if err != nil { - e.errs = append(e.errs, err) - } - } - return e -} - -type joinError struct { - errs []error -} - -func (e *joinError) Error() string { - // Since Join returns nil if every value in errs is nil, - // e.errs cannot be empty. - if len(e.errs) == 1 { - return e.errs[0].Error() - } - - b := []byte(e.errs[0].Error()) - for _, err := range e.errs[1:] { - b = append(b, '\n') - b = append(b, err.Error()...) - } - // At this point, b has at least one byte '\n'. - return b2s(b) -} - -func (e *joinError) Unwrap() []error { - return e.errs -} - -// b2s converts byte slice to a string without memory allocation. -// See https://groups.google.com/forum/#!msg/Golang-Nuts/ENgbUzYvCuU/90yGx7GUAgAJ . -// -// Note it may break if string and/or slice header will change -// in the future go versions. -// -// original code copied from -// https://github.com/valyala/fasthttp/blob/master/b2s_old.go -func b2s(b []byte) string { - return *(*string)(unsafe.Pointer(&b)) -} diff --git a/join_test.go b/join_go120_test.go similarity index 90% rename from join_test.go rename to join_go120_test.go index a4cefc0..00a6b0f 100644 --- a/join_test.go +++ b/join_go120_test.go @@ -1,3 +1,6 @@ +//go:build go1.20 +// +build go1.20 + package errors_test import (