...
Source file
src/log/slog/attr.go
1
2
3
4
5 package slog
6
7 import (
8 "fmt"
9 "time"
10 )
11
12
13 type Attr struct {
14 Key string
15 Value Value
16 }
17
18
19 func String(key, value string) Attr {
20 return Attr{key, StringValue(value)}
21 }
22
23
24 func Int64(key string, value int64) Attr {
25 return Attr{key, Int64Value(value)}
26 }
27
28
29
30 func Int(key string, value int) Attr {
31 return Int64(key, int64(value))
32 }
33
34
35 func Uint64(key string, v uint64) Attr {
36 return Attr{key, Uint64Value(v)}
37 }
38
39
40 func Float64(key string, v float64) Attr {
41 return Attr{key, Float64Value(v)}
42 }
43
44
45 func Bool(key string, v bool) Attr {
46 return Attr{key, BoolValue(v)}
47 }
48
49
50
51 func Time(key string, v time.Time) Attr {
52 return Attr{key, TimeValue(v)}
53 }
54
55
56 func Duration(key string, v time.Duration) Attr {
57 return Attr{key, DurationValue(v)}
58 }
59
60
61
62
63
64
65
66
67 func Group(key string, args ...any) Attr {
68 return Attr{key, GroupValue(argsToAttrSlice(args)...)}
69 }
70
71 func argsToAttrSlice(args []any) []Attr {
72 var (
73 attr Attr
74 attrs []Attr
75 )
76 for len(args) > 0 {
77 attr, args = argsToAttr(args)
78 attrs = append(attrs, attr)
79 }
80 return attrs
81 }
82
83
84
85 func Any(key string, value any) Attr {
86 return Attr{key, AnyValue(value)}
87 }
88
89
90 func (a Attr) Equal(b Attr) bool {
91 return a.Key == b.Key && a.Value.Equal(b.Value)
92 }
93
94 func (a Attr) String() string {
95 return fmt.Sprintf("%s=%s", a.Key, a.Value)
96 }
97
98
99
100 func (a Attr) isEmpty() bool {
101 return a.Key == "" && a.Value.num == 0 && a.Value.any == nil
102 }
103
View as plain text