Skip to content

Commit 440e0ea

Browse files
Use easyjson to unmarshal the ProjectionNotification
1 parent 95a1d41 commit 440e0ea

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

driver/sql/projection.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"database/sql"
66

77
"github.com/hellofresh/goengine"
8+
"github.com/mailru/easyjson/jlexer"
89
)
910

1011
type (
@@ -57,3 +58,45 @@ type (
5758
// EventStreamLoader loads a event stream based on the provided notification and state
5859
EventStreamLoader func(ctx context.Context, conn *sql.Conn, notification *ProjectionNotification, position int64) (goengine.EventStream, error)
5960
)
61+
62+
// UnmarshalJSON supports json.Unmarshaler interface
63+
func (p *ProjectionNotification) UnmarshalJSON(data []byte) error {
64+
r := jlexer.Lexer{Data: data}
65+
p.UnmarshalEasyJSON(&r)
66+
return r.Error()
67+
}
68+
69+
// UnmarshalEasyJSON supports easyjson.Unmarshaler interface
70+
func (p *ProjectionNotification) UnmarshalEasyJSON(in *jlexer.Lexer) {
71+
isTopLevel := in.IsStart()
72+
if in.IsNull() {
73+
if isTopLevel {
74+
in.Consumed()
75+
}
76+
in.Skip()
77+
return
78+
}
79+
in.Delim('{')
80+
for !in.IsDelim('}') {
81+
key := in.UnsafeString()
82+
in.WantColon()
83+
if in.IsNull() {
84+
in.Skip()
85+
in.WantComma()
86+
continue
87+
}
88+
switch key {
89+
case "no":
90+
p.No = int64(in.Int64())
91+
case "aggregate_id":
92+
p.AggregateID = string(in.String())
93+
default:
94+
in.SkipRecursive()
95+
}
96+
in.WantComma()
97+
}
98+
in.Delim('}')
99+
if isTopLevel {
100+
in.Consumed()
101+
}
102+
}

extension/pq/listener.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package pq
22

33
import (
44
"context"
5-
"encoding/json"
65
"strings"
76
"time"
87

98
"github.com/hellofresh/goengine"
109
"github.com/hellofresh/goengine/driver/sql"
1110
"github.com/lib/pq"
11+
"github.com/mailru/easyjson"
1212
)
1313

1414
// Ensure Listener implements sql.Listener
@@ -138,7 +138,7 @@ func (s *Listener) unmarshalNotification(n *pq.Notification) *sql.ProjectionNoti
138138
}
139139

140140
notification := &sql.ProjectionNotification{}
141-
if err := json.Unmarshal([]byte(n.Extra), notification); err != nil {
141+
if err := easyjson.Unmarshal([]byte(n.Extra), notification); err != nil {
142142
logger.WithError(err).Error("received invalid notification data")
143143
return nil
144144
}

0 commit comments

Comments
 (0)