Skip to content

Commit 6057699

Browse files
authored
feat: adding HandleInlineHandler and InlineHandler (#25)
* feat: adding HandleInlineHandler and InlineHandler * doc: adding HandleInlineHandler and InlineHandler
1 parent 6b959c1 commit 6057699

File tree

4 files changed

+167
-2
lines changed

4 files changed

+167
-2
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,35 @@ The example above uses:
363363

364364
Note: `WithAttrs` and `WithGroup` methods of custom middleware must return a new instance, instead of `this`.
365365

366+
#### Inline handler
367+
368+
An "inline handler" (aka. lambda), is a shortcut to handler implement, that hooks a single method and proxies others.
369+
370+
```go
371+
mdw := slogmulti.NewHandleInlineHandler(
372+
// simulate "Handle()"
373+
func(ctx context.Context, groups []string, attrs []slog.Attr, record slog.Record) error {
374+
// [...]
375+
return nil
376+
},
377+
)
378+
```
379+
380+
```go
381+
mdw := slogmulti.NewInlineHandler(
382+
// simulate "Enabled()"
383+
func(ctx context.Context, groups []string, attrs []slog.Attr, level slog.Level) bool {
384+
// [...]
385+
return true
386+
},
387+
// simulate "Handle()"
388+
func(ctx context.Context, groups []string, attrs []slog.Attr, record slog.Record) error {
389+
// [...]
390+
return nil
391+
},
392+
)
393+
```
394+
366395
#### Inline middleware
367396

368397
An "inline middleware" (aka. lambda), is a shortcut to middleware implementation, that hooks a single method and proxies others.

go.work.sum

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
55
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
66
github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE=
77
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
8+
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
89
golang.org/x/mod v0.6.0/go.mod h1:4mET923SAdbXp2ki8ey+zGs1SLqsuM2Y0uvdZR/fUNI=
910
golang.org/x/mod v0.11.0 h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU=
1011
golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
@@ -18,8 +19,6 @@ golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
1819
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1920
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
2021
golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE=
21-
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
22-
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
2322
golang.org/x/tools v0.2.0 h1:G6AHpWxTMGY1KyEYoAQ5WTtIekUUvDNjan3ugu60JvE=
2423
golang.org/x/tools v0.2.0/go.mod h1:y4OqIKeOV/fWJetJ8bXPU1sEVniLMIyDAZWeHdV+NTA=
2524
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=

handler_inline.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package slogmulti
2+
3+
import (
4+
"context"
5+
6+
"log/slog"
7+
)
8+
9+
// NewInlineHandler is a shortcut to a handler that implements all methods.
10+
func NewInlineHandler(
11+
enabledFunc func(ctx context.Context, groups []string, attrs []slog.Attr, level slog.Level) bool,
12+
handleFunc func(ctx context.Context, groups []string, attrs []slog.Attr, record slog.Record) error,
13+
) slog.Handler {
14+
return &InlineHandler{
15+
groups: []string{},
16+
attrs: []slog.Attr{},
17+
enabledFunc: enabledFunc,
18+
handleFunc: handleFunc,
19+
}
20+
}
21+
22+
var _ slog.Handler = (*InlineHandler)(nil)
23+
24+
type InlineHandler struct {
25+
groups []string
26+
attrs []slog.Attr
27+
enabledFunc func(ctx context.Context, groups []string, attrs []slog.Attr, level slog.Level) bool
28+
handleFunc func(ctx context.Context, groups []string, attrs []slog.Attr, record slog.Record) error
29+
}
30+
31+
// Implements slog.Handler
32+
func (h *InlineHandler) Enabled(ctx context.Context, level slog.Level) bool {
33+
return h.enabledFunc(ctx, h.groups, h.attrs, level)
34+
}
35+
36+
// Implements slog.Handler
37+
func (h *InlineHandler) Handle(ctx context.Context, record slog.Record) error {
38+
return h.handleFunc(ctx, h.groups, h.attrs, record)
39+
}
40+
41+
// Implements slog.Handler
42+
func (h *InlineHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
43+
newAttrs := []slog.Attr{}
44+
newAttrs = append(newAttrs, h.attrs...)
45+
newAttrs = append(newAttrs, attrs...)
46+
47+
return &InlineHandler{
48+
groups: h.groups,
49+
attrs: newAttrs,
50+
enabledFunc: h.enabledFunc,
51+
handleFunc: h.handleFunc,
52+
}
53+
}
54+
55+
// Implements slog.Handler
56+
func (h *InlineHandler) WithGroup(name string) slog.Handler {
57+
// https://cs.opensource.google/go/x/exp/+/46b07846:slog/handler.go;l=247
58+
if name == "" {
59+
return h
60+
}
61+
62+
newGroups := []string{}
63+
newGroups = append(newGroups, h.groups...)
64+
newGroups = append(newGroups, name)
65+
return &InlineHandler{
66+
groups: newGroups,
67+
attrs: h.attrs,
68+
enabledFunc: h.enabledFunc,
69+
handleFunc: h.handleFunc,
70+
}
71+
}

handler_inline_handle.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package slogmulti
2+
3+
import (
4+
"context"
5+
6+
"log/slog"
7+
)
8+
9+
// NewHandleInlineHandler is a shortcut to a middleware that implements only the `Handle` method.
10+
func NewHandleInlineHandler(handleFunc func(ctx context.Context, groups []string, attrs []slog.Attr, record slog.Record) error) Middleware {
11+
return func(next slog.Handler) slog.Handler {
12+
return &HandleInlineHandler{
13+
groups: []string{},
14+
attrs: []slog.Attr{},
15+
handleFunc: handleFunc,
16+
}
17+
}
18+
}
19+
20+
var _ slog.Handler = (*HandleInlineHandler)(nil)
21+
22+
type HandleInlineHandler struct {
23+
groups []string
24+
attrs []slog.Attr
25+
handleFunc func(ctx context.Context, groups []string, attrs []slog.Attr, record slog.Record) error
26+
}
27+
28+
// Implements slog.Handler
29+
func (h *HandleInlineHandler) Enabled(ctx context.Context, level slog.Level) bool {
30+
return true
31+
}
32+
33+
// Implements slog.Handler
34+
func (h *HandleInlineHandler) Handle(ctx context.Context, record slog.Record) error {
35+
return h.handleFunc(ctx, h.groups, h.attrs, record)
36+
}
37+
38+
// Implements slog.Handler
39+
func (h *HandleInlineHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
40+
newAttrs := []slog.Attr{}
41+
newAttrs = append(newAttrs, h.attrs...)
42+
newAttrs = append(newAttrs, attrs...)
43+
44+
return &InlineHandler{
45+
groups: h.groups,
46+
attrs: newAttrs,
47+
handleFunc: h.handleFunc,
48+
}
49+
}
50+
51+
// Implements slog.Handler
52+
func (h *HandleInlineHandler) WithGroup(name string) slog.Handler {
53+
// https://cs.opensource.google/go/x/exp/+/46b07846:slog/handler.go;l=247
54+
if name == "" {
55+
return h
56+
}
57+
58+
newGroups := []string{}
59+
newGroups = append(newGroups, h.groups...)
60+
newGroups = append(newGroups, name)
61+
return &InlineHandler{
62+
groups: newGroups,
63+
attrs: h.attrs,
64+
handleFunc: h.handleFunc,
65+
}
66+
}

0 commit comments

Comments
 (0)