Skip to content

Commit 5e1b8ae

Browse files
authored
fix: panic on custom marshaller (#5)
* fix: panic on custom marshaller * review: rename parameters in test * review: add test bad case
1 parent ff0e607 commit 5e1b8ae

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

testdata/src/a/a.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ func bad() {
4646
Str("bar", "baz").
4747
Int("n", 1),
4848
)
49+
50+
// custom object marshaller
51+
f := &Foo{Bar: &Bar{}}
52+
53+
log.Info().Object("foo", f) // want "must be dispatched by Msg or Send method"
4954
}
5055

5156
func ok() {
@@ -94,4 +99,27 @@ func ok() {
9499
Str("bar", "baz").
95100
Int("n", 1),
96101
).Send()
102+
103+
// custom object marshaller
104+
f := &Foo{Bar: &Bar{}}
105+
106+
log.Info().Object("foo", f).Msg("")
107+
}
108+
109+
type Marshaller interface {
110+
MarshalZerologObject(event *zerolog.Event)
111+
}
112+
113+
type Foo struct {
114+
Bar Marshaller
115+
}
116+
117+
func (f *Foo) MarshalZerologObject(event *zerolog.Event) {
118+
f.Bar.MarshalZerologObject(event)
119+
}
120+
121+
type Bar struct{}
122+
123+
func (b *Bar) MarshalZerologObject(event *zerolog.Event) {
124+
event.Str("key", "value")
97125
}

zerologlint.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,12 @@ func isZerologEvent(v ssa.Value) bool {
117117
}
118118

119119
func isDispatchMethod(c ssa.CallCommon) bool {
120-
m := c.StaticCallee().Name()
120+
callee := c.StaticCallee()
121+
if callee == nil {
122+
return false
123+
}
124+
125+
m := callee.Name()
121126
if m == "Send" || m == "Msg" || m == "Msgf" || m == "MsgFunc" {
122127
return true
123128
}

0 commit comments

Comments
 (0)