From a9cff5b05e1c46a0ea0ac5d5d652217ccf28d13f Mon Sep 17 00:00:00 2001 From: Stephan Renatus Date: Wed, 28 Jun 2023 11:22:02 +0200 Subject: [PATCH] build: add errors.Join workaround for go <1.20 Signed-off-by: Stephan Renatus --- internal/errors/join.go | 53 ++++++++++++++++++++++++++++++++++ internal/errors/join_go1.20.go | 7 +++++ plugins/discovery/discovery.go | 2 +- plugins/plugins.go | 2 +- 4 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 internal/errors/join.go create mode 100644 internal/errors/join_go1.20.go diff --git a/internal/errors/join.go b/internal/errors/join.go new file mode 100644 index 0000000000..8d8e1f301d --- /dev/null +++ b/internal/errors/join.go @@ -0,0 +1,53 @@ +// Copyright 2022 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build !go1.20 + +package errors + +// Join returns an error that wraps the given errors. +// Any nil error values are discarded. +// Join returns nil if errs contains no non-nil values. +// 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. +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 { + var b []byte + for i, err := range e.errs { + if i > 0 { + b = append(b, '\n') + } + b = append(b, err.Error()...) + } + return string(b) +} + +func (e *joinError) Unwrap() []error { + return e.errs +} diff --git a/internal/errors/join_go1.20.go b/internal/errors/join_go1.20.go new file mode 100644 index 0000000000..666f3c783e --- /dev/null +++ b/internal/errors/join_go1.20.go @@ -0,0 +1,7 @@ +//go:build go1.20 + +package errors + +import "errors" + +var Join = errors.Join diff --git a/plugins/discovery/discovery.go b/plugins/discovery/discovery.go index e322b5a835..550a2b4903 100644 --- a/plugins/discovery/discovery.go +++ b/plugins/discovery/discovery.go @@ -8,7 +8,6 @@ package discovery import ( "context" "encoding/json" - "errors" "fmt" "io" "os" @@ -23,6 +22,7 @@ import ( "github.com/open-policy-agent/opa/hooks" bundleUtils "github.com/open-policy-agent/opa/internal/bundle" cfg "github.com/open-policy-agent/opa/internal/config" + "github.com/open-policy-agent/opa/internal/errors" "github.com/open-policy-agent/opa/keys" "github.com/open-policy-agent/opa/logging" "github.com/open-policy-agent/opa/metrics" diff --git a/plugins/plugins.go b/plugins/plugins.go index dcf65412da..ef4d31ceeb 100644 --- a/plugins/plugins.go +++ b/plugins/plugins.go @@ -7,7 +7,6 @@ package plugins import ( "context" - "errors" "fmt" "sync" "time" @@ -22,6 +21,7 @@ import ( "github.com/open-policy-agent/opa/hooks" bundleUtils "github.com/open-policy-agent/opa/internal/bundle" cfg "github.com/open-policy-agent/opa/internal/config" + "github.com/open-policy-agent/opa/internal/errors" initload "github.com/open-policy-agent/opa/internal/runtime/init" "github.com/open-policy-agent/opa/keys" "github.com/open-policy-agent/opa/loader"