Skip to content

Commit 433f774

Browse files
committed
add UnmarshalJSON support
1 parent ace3b0a commit 433f774

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

event.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,21 @@ func (e Event) MarshalJSON() ([]byte, error) {
120120
return json.Marshal(h)
121121
}
122122

123+
// UnmarshalJSON is a Go function that unmarshal the Event from JSON.
124+
func (e *Event) UnmarshalJSON(data []byte) error {
125+
e.headers = make(map[string]string)
126+
if err := json.Unmarshal(data, &e.headers); err != nil {
127+
return err
128+
}
129+
130+
if e.headers["_body"] != "" {
131+
e.body = []byte(e.headers["_body"])
132+
delete(e.headers, "_body")
133+
}
134+
135+
return nil
136+
}
137+
123138
// LogValue returns the log value of the Event.
124139
//
125140
// It returns a slog.Value that contains the name and sequence of the Event.

0 commit comments

Comments
 (0)