Skip to content

Commit 2795d79

Browse files
committed
Get golint passing
1 parent fc368cc commit 2795d79

File tree

10 files changed

+122
-110
lines changed

10 files changed

+122
-110
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ go get go.coder.com/slog
2020
- First class [context.Context](https://blog.golang.org/context) support
2121
- Beautiful logging output by default
2222
- Multiple adapters
23-
- First class [\testing.TB](https://godoc.org/go.coder.com/slog/slogtest) support
23+
- First class [testing.TB](https://godoc.org/go.coder.com/slog/slogtest) support
2424

2525
## Example
2626

examples_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package slog_test
22

33
import (
4-
"golang.org/x/xerrors"
54
"io"
65
"testing"
76

7+
"golang.org/x/xerrors"
8+
89
"go.coder.com/slog"
910
"go.coder.com/slog/sloggers/slogtest"
1011
)

internal/humanfmt/humanfmt.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,16 @@ func Entry(ent slog.Entry, enableColor bool) string {
5252
}
5353

5454
func pinnedFields(ent slog.Entry) string {
55-
pinned := slogval.Map{}
56-
57-
if ent.SpanContext != (trace.SpanContext{}) {
58-
pinned = pinned.Append("trace", slogval.String(ent.SpanContext.TraceID.String()))
59-
pinned = pinned.Append("span", slogval.String(ent.SpanContext.SpanID.String()))
55+
if ent.SpanContext == (trace.SpanContext{}) {
56+
return ""
6057
}
6158

62-
return humanFields(pinned)
59+
m := slog.Map(
60+
slog.F("trace", ent.SpanContext.TraceID),
61+
slog.F("span", ent.SpanContext.SpanID),
62+
)
63+
64+
return humanFields(slogval.Reflect(m))
6365
}
6466

6567
func stringFields(ent slog.Entry) string {
@@ -92,6 +94,7 @@ func levelColor(level slog.Level) color.Attribute {
9294
panic("humanfmt: unexpected level: " + string(level))
9395
}
9496

97+
// IsTTY checks whether the given writer is a *os.File TTY.
9598
func IsTTY(w io.Writer) bool {
9699
f, ok := w.(interface {
97100
Fd() uintptr

internal/humanfmt/marshal_test.go

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package humanfmt
33
import (
44
"testing"
55

6+
"go.coder.com/slog"
67
"go.coder.com/slog/internal/diff"
78
"go.coder.com/slog/slogval"
89
)
@@ -12,60 +13,60 @@ func Test_marshalFields(t *testing.T) {
1213

1314
testCases := []struct {
1415
name string
15-
in map[string]interface{}
16+
in []slog.Field
1617
out string
1718
}{
1819
{
1920
name: "stringWithNewlines",
20-
in: map[string]interface{}{
21-
"a": `hi
21+
in: slog.Map(
22+
slog.F("a", `hi
2223
two
23-
three`,
24-
},
24+
three`),
25+
),
2526
out: `a: hi
2627
two
2728
three`,
2829
},
2930
{
3031
name: "bool",
31-
in: map[string]interface{}{
32-
"a": false,
33-
},
32+
in: slog.Map(
33+
slog.F("a", false),
34+
),
3435
out: `a: false`,
3536
},
3637
{
3738
name: "float",
38-
in: map[string]interface{}{
39-
"a": 0.3,
40-
},
39+
in: slog.Map(
40+
slog.F("a", 0.3),
41+
),
4142
out: `a: 0.3`,
4243
},
4344
{
4445
name: "int",
45-
in: map[string]interface{}{
46-
"a": -1,
47-
},
46+
in: slog.Map(
47+
slog.F("a", -1),
48+
),
4849
out: `a: -1`,
4950
},
5051
{
5152
name: "uint",
52-
in: map[string]interface{}{
53-
"a": uint(3),
54-
},
53+
in: slog.Map(
54+
slog.F("a", uint(3)),
55+
),
5556
out: `a: 3`,
5657
},
5758
{
5859
name: "list",
59-
in: map[string]interface{}{
60-
"a": []interface{}{
61-
map[string]interface{}{
62-
"hi": "hello",
63-
"hi3": "hello",
64-
},
60+
in: slog.Map(
61+
slog.F("a", []interface{}{
62+
slog.Map(
63+
slog.F("hi", "hello"),
64+
slog.F("hi3", "hello"),
65+
),
6566
"3",
6667
[]string{"a", "b", "c"},
6768
},
68-
},
69+
)),
6970
out: `a:
7071
- hi: hello
7172
hi3: hello
@@ -77,37 +78,37 @@ three`,
7778
},
7879
{
7980
name: "emptyStruct",
80-
in: map[string]interface{}{
81-
"a": struct{}{},
82-
"b": struct{}{},
83-
"c": struct{}{},
84-
},
81+
in: slog.Map(
82+
slog.F("a", struct{}{}),
83+
slog.F("b", struct{}{}),
84+
slog.F("c", struct{}{}),
85+
),
8586
out: `a:
8687
b:
8788
c:`,
8889
},
8990
{
9091
name: "nestedMap",
91-
in: map[string]interface{}{
92-
"a": map[string]string{
93-
"0": "hi",
92+
in: slog.Map(
93+
slog.F("a", map[string]string{
9494
"1": "hi",
95-
},
96-
},
95+
"0": "hi",
96+
}),
97+
),
9798
out: `a:
9899
0: hi
99100
1: hi`,
100101
},
101102
{
102103
name: "specialCharacterKey",
103-
in: map[string]interface{}{
104-
"nhooyr \tsoftware™️": "hi",
105-
"\rxeow\r": `mdsla
106-
dsamkld`,
107-
},
108-
out: `"\rxeow\r": mdsla
109-
dsamkld
110-
"nhooyr_\tsoftware™️": hi`,
104+
in: slog.Map(
105+
slog.F("nhooyr \tsoftware™️", "hi"),
106+
slog.F("\rxeow\r", `mdsla
107+
dsamkld`),
108+
),
109+
out: `"nhooyr_\tsoftware™️": hi
110+
"\rxeow\r": mdsla
111+
dsamkld`,
111112
},
112113
}
113114

@@ -116,8 +117,8 @@ dsamkld`,
116117
t.Run(tc.name, func(t *testing.T) {
117118
t.Parallel()
118119

119-
v := slogval.ReflectUnsafe(tc.in)
120-
actOut := humanFields(v.(slogval.Map))
120+
v := slogval.Reflect(tc.in)
121+
actOut := humanFields(v)
121122
t.Logf("yaml:\n%v", actOut)
122123
if diff := diff.Diff(tc.out, actOut); diff != "" {
123124
t.Fatalf("unexpected output: %v", diff)

slog.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,17 @@ func fromContext(ctx context.Context) parsedFields {
8686
}
8787

8888
// Context returns a context that contains the given fields.
89-
// Any logs written with the provided context will contain
90-
// the given fields.
89+
// Any logs written with the provided context will have
90+
// the given logs prepended.
9191
// It will append to any fields already in ctx.
9292
func Context(ctx context.Context, fields ...Field) context.Context {
9393
l := fromContext(ctx)
9494
l = l.withFields(fields)
9595
return withContext(ctx, l)
9696
}
9797

98+
// Entry represents the structure of a log entry.
99+
// It is the argument to the sink when logging.
98100
type Entry struct {
99101
Time time.Time
100102

@@ -112,8 +114,10 @@ type Entry struct {
112114
Fields []Field
113115
}
114116

117+
// Level represents a log level.
115118
type Level int
116119

120+
// The supported log levels.
117121
const (
118122
LevelDebug Level = iota
119123
LevelInfo
@@ -171,6 +175,8 @@ type sink struct {
171175
pl parsedFields
172176
}
173177

178+
// Logger allows logging a ordered slice of fields
179+
// to an underlying set of sinks.
174180
type Logger struct {
175181
testingHelper func()
176182

@@ -183,36 +189,45 @@ func (l Logger) clone() Logger {
183189
return l
184190
}
185191

192+
// Debug logs the msg and fields at LevelDebug.
186193
func (l Logger) Debug(ctx context.Context, msg string, fields ...Field) {
187194
l.testingHelper()
188195
l.log(ctx, LevelDebug, msg, fields)
189196
}
190197

198+
// Info logs the msg and fields at LevelInfo.
191199
func (l Logger) Info(ctx context.Context, msg string, fields ...Field) {
192200
l.testingHelper()
193201
l.log(ctx, LevelInfo, msg, fields)
194202
}
195203

204+
// Warn logs the msg and fields at LevelWarn.
196205
func (l Logger) Warn(ctx context.Context, msg string, fields ...Field) {
197206
l.testingHelper()
198207
l.log(ctx, LevelWarn, msg, fields)
199208
}
200209

210+
// Error logs the msg and fields at LevelError.
201211
func (l Logger) Error(ctx context.Context, msg string, fields ...Field) {
202212
l.testingHelper()
203213
l.log(ctx, LevelError, msg, fields)
204214
}
205215

216+
// Critical logs the msg and fields at LevelCritical.
206217
func (l Logger) Critical(ctx context.Context, msg string, fields ...Field) {
207218
l.testingHelper()
208219
l.log(ctx, LevelCritical, msg, fields)
209220
}
210221

222+
// Fatal logs the msg and fields at LevelFatal.
211223
func (l Logger) Fatal(ctx context.Context, msg string, fields ...Field) {
212224
l.testingHelper()
213225
l.log(ctx, LevelFatal, msg, fields)
214226
}
215227

228+
// With returns a Logger that prepends the given fields on every
229+
// logged entry.
230+
// It will append to any fields already in the Logger.
216231
func (l Logger) With(fields ...Field) Logger {
217232
l = l.clone()
218233
for i, s := range l.sinks {

sloggers/sloghuman/sloghuman.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (s *humanSink) writeString(str string) {
4444
io.WriteString(s.w, str)
4545
}
4646

47-
// Human creates a logger that writes logs in a human
47+
// Make creates a logger that writes logs in a human
4848
// readable YAML like format to the given writer.
4949
func Make(w io.Writer) slog.Logger {
5050
return slog.Make(&humanSink{

sloggers/slogtest/slogtest.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ import (
1212
)
1313

1414
// TestOptions represents the options for the logger returned
15-
// by Test.
15+
// by Make.
1616
type TestOptions struct {
1717
// IgnoreErrors causes the test logger to not fatal the test
1818
// on Fatal and not error the test on Error or Critical.
1919
IgnoreErrors bool
2020
}
2121

22-
// Test creates a Logger that writes logs to tb in a human readable format.
22+
// Make creates a Logger that writes logs to tb in a human readable format.
2323
func Make(tb testing.TB, opts *TestOptions) slog.Logger {
2424
if opts == nil {
2525
opts = &TestOptions{}

slogval/reflect.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,21 @@ import (
1212
"go.coder.com/slog"
1313
)
1414

15+
// Reflect uses reflection to convert the slice of fields into a ordered
16+
// map that uses only the primitive value types defined in this package.
1517
func Reflect(fs []slog.Field) Map {
1618
var m Map
1719
for _, f := range fs {
18-
m = m.Append(f.LogKey(), reflectValue(reflect.ValueOf(f.LogValue())))
20+
m = m.appendVal(f.LogKey(), reflectValue(reflect.ValueOf(f.LogValue())))
1921
}
2022
return m
2123
}
2224

23-
// TODO
24-
func ReflectMap() {
25+
// PureReflect is like reflect but only uses
26+
func PureReflect(v interface{}) {
2527
panic("TODO")
2628
}
2729

28-
// TODO remove later.
29-
func ReflectUnsafe(v interface{}) Value {
30-
return reflectValue(reflect.ValueOf(v))
31-
}
32-
3330
func reflectValue(rv reflect.Value) Value {
3431
if !rv.IsValid() {
3532
// reflect.ValueOf(nil).IsValid == false
@@ -99,7 +96,7 @@ func reflectValue(rv reflect.Value) Value {
9996
f := rv.Index(i)
10097
key := f.MethodByName("LogKey").Call(nil)[0].String()
10198
val := f.MethodByName("LogValue").Call(nil)[0]
102-
m = m.Append(key, reflectValue(val))
99+
m = m.appendVal(key, reflectValue(val))
103100
}
104101
return m
105102
}
@@ -112,9 +109,9 @@ func reflectValue(rv reflect.Value) Value {
112109
m := make(Map, 0, rv.Len())
113110
for _, k := range rv.MapKeys() {
114111
mv := rv.MapIndex(k)
115-
m = m.Append(fmt.Sprintf("%v", k), reflectValue(mv))
112+
m = m.appendVal(fmt.Sprintf("%v", k), reflectValue(mv))
116113
}
117-
m.Sort()
114+
m.sort()
118115
return m
119116
case reflect.Struct:
120117
typ := rv.Type()
@@ -138,7 +135,7 @@ func reflectValue(rv reflect.Value) Value {
138135
if name == "" {
139136
name = snakecase(ft.Name)
140137
}
141-
f = f.Append(name, v)
138+
f = f.appendVal(name, v)
142139

143140
}
144141

0 commit comments

Comments
 (0)