summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-errors/data.go
blob: 3a32fd69e27c3baf21583ffedf3e7d6cbbaf599a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package errors

import (
	"sync"

	"codeberg.org/gruf/go-bytes"
	"codeberg.org/gruf/go-logger"
)

// global logfmt data formatter
var logfmt = logger.TextFormat{Strict: false}

// KV is a structure for setting key-value pairs in ErrorData
type KV struct {
	Key   string
	Value interface{}
}

// ErrorData defines a way to set and access contextual error data.
// The default implementation of this is thread-safe
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)

	// String returns a string representation of the ErrorData
	String() string
}

// NewData returns a new ErrorData implementation
func NewData() ErrorData {
	return &errorData{
		data: make(map[string]interface{}, 10),
	}
}

// errorData is our ErrorData implementation, this is essentially
// just a thread-safe string-interface map implementation
type errorData struct {
	data map[string]interface{}
	buf  bytes.Buffer
	mu   sync.Mutex
}

func (d *errorData) Value(key string) (interface{}, bool) {
	d.mu.Lock()
	v, ok := d.data[key]
	d.mu.Unlock()
	return v, ok
}

func (d *errorData) Append(kvs ...KV) {
	d.mu.Lock()
	for i := range kvs {
		k := kvs[i].Key
		v := kvs[i].Value
		d.data[k] = v
	}
	d.mu.Unlock()
}

func (d *errorData) String() string {
	d.mu.Lock()

	d.buf.Reset()
	d.buf.B = append(d.buf.B, '{')
	logfmt.AppendFields(&d.buf, d.data)
	d.buf.B = append(d.buf.B, '}')

	d.mu.Unlock()
	return d.buf.StringPtr()
}