gotosocial/vendor/codeberg.org/gruf/go-errors/data.go

107 lines
2.1 KiB
Go
Raw Normal View History

2021-11-13 11:29:08 +00:00
package errors
import (
"fmt"
2021-11-13 11:29:08 +00:00
"sync"
"codeberg.org/gruf/go-bytes"
"codeberg.org/gruf/go-logger"
)
2021-11-27 14:26:58 +00:00
// global logfmt data formatter.
var logfmt = logger.TextFormat{
Strict: false,
Verbose: true,
MaxDepth: 10,
2021-11-27 14:26:58 +00:00
}
2021-11-13 11:29:08 +00:00
2021-11-27 14:26:58 +00:00
// KV is a structure for setting key-value pairs in ErrorData.
2021-11-13 11:29:08 +00:00
type KV struct {
Key string
Value interface{}
}
// ErrorData defines a way to set and access contextual error data.
2021-11-27 14:26:58 +00:00
// The default implementation of this is thread-safe.
2021-11-13 11:29:08 +00:00
type ErrorData interface {
// Value will attempt to fetch value for given key in ErrorData
Value(string) (interface{}, bool)
// Append adds the supplied key-values to ErrorData, similar keys DO overwrite
Append(...KV)
// Implement byte slice representation formatter.
logger.Formattable
// Implement string representation formatter.
fmt.Stringer
2021-11-13 11:29:08 +00:00
}
2021-11-27 14:26:58 +00:00
// NewData returns a new ErrorData implementation.
2021-11-13 11:29:08 +00:00
func NewData() ErrorData {
return &errorData{
data: make([]KV, 0, 10),
2021-11-13 11:29:08 +00:00
}
}
// errorData is our ErrorData implementation, this is essentially
2021-11-27 14:26:58 +00:00
// just a thread-safe string-interface map implementation.
2021-11-13 11:29:08 +00:00
type errorData struct {
data []KV
2021-11-13 11:29:08 +00:00
mu sync.Mutex
}
func (d *errorData) set(key string, value interface{}) {
for i := range d.data {
if d.data[i].Key == key {
// Found existing, update!
d.data[i].Value = value
return
}
}
// Add new KV entry to slice
d.data = append(d.data, KV{
Key: key,
Value: value,
})
}
2021-11-13 11:29:08 +00:00
func (d *errorData) Value(key string) (interface{}, bool) {
d.mu.Lock()
for i := range d.data {
if d.data[i].Key == key {
v := d.data[i].Value
d.mu.Unlock()
return v, true
}
}
2021-11-13 11:29:08 +00:00
d.mu.Unlock()
return nil, false
2021-11-13 11:29:08 +00:00
}
func (d *errorData) Append(kvs ...KV) {
d.mu.Lock()
for i := range kvs {
d.set(kvs[i].Key, kvs[i].Value)
2021-11-13 11:29:08 +00:00
}
d.mu.Unlock()
}
func (d *errorData) AppendFormat(b []byte) []byte {
buf := bytes.Buffer{B: b}
2021-11-13 11:29:08 +00:00
d.mu.Lock()
buf.B = append(buf.B, '{')
for i := range d.data {
logfmt.AppendKey(&buf, d.data[i].Key)
logfmt.AppendValue(&buf, d.data[i].Value)
}
buf.B = append(buf.B, '}')
2021-11-13 11:29:08 +00:00
d.mu.Unlock()
return buf.B
}
func (d *errorData) String() string {
return string(d.AppendFormat(nil))
2021-11-13 11:29:08 +00:00
}