-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeta.go
More file actions
69 lines (53 loc) · 1.84 KB
/
meta.go
File metadata and controls
69 lines (53 loc) · 1.84 KB
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
package errors
import (
"fmt"
)
// Meta holds extra meta data around an error. Try adding simple values to the Meta map. Key order is not guaranteed.
type Meta map[string]any
// WithMeta accepts an even number of arguments representing key/value pairs. The first argument "firstKey" forces
// the compiler to fail if the first argument is not a string. In "args" every odd argument must be of type string
// which will be used as the Meta map key. If an odd argument is not a string that pair will be skipped.
func WithMeta(firstKey string, args ...any) Meta {
m := make(Meta, len(args)+1)
// No arguments are passed, return an empty Meta.
if len(args) == 0 {
m[firstKey] = ""
return m
}
// Set the firstKey, if only one args is present return early.
m[firstKey] = args[0]
if len(args) == 1 {
return m
}
// Pop the first argument as this is already set.
args = args[1:]
// If args is not even add an !BADVALUE string as a last value to make it even and to let the user know something's
// wrong.
if len(args)%2 != 0 {
args = append(args, "!BADVALUE")
}
// Loop over the rest of the args and set the key/value pairs in m.
for i := 0; i < len(args); i = i + 2 {
// If the even args are not string, replace it with !BADKEY<index> to let the user know it's settings the Meta values
// wrong.
strKey, ok := args[i].(string)
if ok == false {
strKey = fmt.Sprintf("!BADKEY%d", i+2)
}
m[strKey] = args[i+1]
}
return m
}
// Set will set key to value and returns Meta. Same keys will be overwritten.
func (p Meta) Set(key string, value any) (m Meta) {
p[key] = value
return p
}
// Merge combines the arguments to an existing Meta and returns it. Existing keys will be overwritten.
func (p Meta) Merge(firstKey string, args ...any) (m Meta) {
nm := WithMeta(firstKey, args...)
for k, v := range nm {
p.Set(k, v)
}
return p
}