refactor: make lock in error Builder optional

This commit is contained in:
yusing 2025-04-10 06:08:37 +08:00
parent e3699b406c
commit 3cd9e47fd0

View file

@ -5,44 +5,65 @@ import (
"sync" "sync"
) )
type noLock struct{}
func (noLock) Lock() {}
func (noLock) Unlock() {}
func (noLock) RLock() {}
func (noLock) RUnlock() {}
type rwLock interface {
sync.Locker
RLock()
RUnlock()
}
type Builder struct { type Builder struct {
about string about string
errs []error errs []error
sync.Mutex rwLock
} }
func NewBuilder(about string) *Builder { // NewBuilder creates a new Builder.
return &Builder{about: about} //
// If about is not provided, the Builder will not have a subject
// and will expand when adding to another builder.
func NewBuilder(about ...string) *Builder {
if len(about) == 0 {
return &Builder{rwLock: noLock{}}
}
return &Builder{about: about[0], rwLock: noLock{}}
}
func NewBuilderWithConcurrency(about ...string) *Builder {
if len(about) == 0 {
return &Builder{rwLock: new(sync.RWMutex)}
}
return &Builder{about: about[0], rwLock: new(sync.RWMutex)}
}
func (b *Builder) EnableConcurrency() {
b.rwLock = new(sync.RWMutex)
} }
func (b *Builder) About() string { func (b *Builder) About() string {
if !b.HasError() {
return ""
}
return b.about return b.about
} }
//go:inline
func (b *Builder) HasError() bool { func (b *Builder) HasError() bool {
// no need to lock, when this is called, the Builder is not used anymore
return len(b.errs) > 0 return len(b.errs) > 0
} }
func (b *Builder) error() Error { func (b *Builder) Error() Error {
if !b.HasError() { if len(b.errs) == 0 {
return nil return nil
} }
return &nestedError{Err: New(b.about), Extras: b.errs} return &nestedError{Err: New(b.about), Extras: b.errs}
} }
func (b *Builder) Error() Error {
if len(b.errs) == 1 {
return wrap(b.errs[0])
}
return b.error()
}
func (b *Builder) String() string { func (b *Builder) String() string {
err := b.error() err := b.Error()
if err == nil { if err == nil {
return "" return ""
} }
@ -57,10 +78,12 @@ func (b *Builder) Add(err error) *Builder {
return b return b
} }
wrapped := wrap(err)
b.Lock() b.Lock()
defer b.Unlock() defer b.Unlock()
switch err := wrap(err).(type) { switch err := wrapped.(type) {
case *baseError: case *baseError:
b.errs = append(b.errs, err.Err) b.errs = append(b.errs, err.Err)
case *nestedError: case *nestedError:
@ -105,26 +128,33 @@ func (b *Builder) AddFrom(other *Builder, flatten bool) *Builder {
if flatten { if flatten {
b.errs = append(b.errs, other.errs...) b.errs = append(b.errs, other.errs...)
} else { } else {
b.errs = append(b.errs, other.error()) b.errs = append(b.errs, other.Error())
} }
return b return b
} }
func (b *Builder) AddRange(errs ...error) *Builder { func (b *Builder) AddRange(errs ...error) *Builder {
nonNilErrs := make([]error, 0, len(errs))
for _, err := range errs {
if err != nil {
nonNilErrs = append(nonNilErrs, err)
}
}
b.Lock() b.Lock()
defer b.Unlock() defer b.Unlock()
for _, err := range errs { b.errs = append(b.errs, nonNilErrs...)
if err != nil {
b.errs = append(b.errs, err)
}
}
return b return b
} }
func (b *Builder) ForEach(fn func(error)) { func (b *Builder) ForEach(fn func(error)) {
for _, err := range b.errs { b.RLock()
errs := b.errs
b.RUnlock()
for _, err := range errs {
fn(err) fn(err)
} }
} }