summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-errors/data.go
blob: b5226172c23467ba7b72abcd1bfb9f3a04afe9ab (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package errors

import (
	"fmt"
	"sync"

	"codeberg.org/gruf/go-format"
)

// 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)

	// Implement byte slice representation formatter.
	format.Formattable

	// Implement string representation formatter.
	fmt.Stringer
}

// NewData returns a new ErrorData implementation.
func NewData() ErrorData {
	return &errorData{
		data: make([]KV, 0, 10),
	}
}

// errorData is our ErrorData implementation, this is essentially
// just a thread-safe string-interface map implementation.
type errorData struct {
	data []KV
	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,
	})
}

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
		}
	}
	d.mu.Unlock()
	return nil, false
}

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

func (d *errorData) AppendFormat(b []byte) []byte {
	buf := format.Buffer{B: b}
	d.mu.Lock()
	buf.B = append(buf.B, '{')

	// Append data as kv pairs
	for i := range d.data {
		key := d.data[i].Key
		val := d.data[i].Value
		format.Appendf(&buf, "{:k}={:v} ", key, val)
	}

	// Drop trailing space
	if len(d.data) > 0 {
		buf.Truncate(1)
	}

	buf.B = append(buf.B, '}')
	d.mu.Unlock()
	return buf.B
}

func (d *errorData) String() string {
	return string(d.AppendFormat(nil))
}