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