Skip to content

Commit

Permalink
use of NoEscape version of json marshaler/unmarshaler
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsannm committed Jul 29, 2022
1 parent 0426244 commit 971df06
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 15 deletions.
2 changes: 1 addition & 1 deletion envelope_carrier.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func envelopeCarrierFromContext(ctx *Context) envelopeCarrier {

func envelopeCarrierFromData(data []byte) envelopeCarrier {
ec := envelopeCarrier{}
_ = json.Unmarshal(data, &ec)
_ = json.UnmarshalNoEscape(data, &ec)

return ec
}
5 changes: 3 additions & 2 deletions exmples/simple-rest-server/api/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ var SampleDesc desc.ServiceDescFunc = func() *desc.Service {
desc.NewContract().
SetInput(&dto.EchoRequest{}).
SetOutput(&dto.EchoResponse{}).
NamedSelector("Echo", fasthttp.REST(http.MethodGet, "/echo/:randomID")).
NamedSelector("Echo", fasthttp.RPC("echoRequest")).
NamedSelector("EchoGET", fasthttp.REST(http.MethodGet, "/echo/:randomID")).
NamedSelector("EchoPOST", fasthttp.REST(http.MethodPost, "/echo-post")).
NamedSelector("EchoRPC", fasthttp.RPC("echoRequest")).
AddModifier(func(envelope ronykit.Envelope) {
envelope.SetHdr("X-Custom-Header", "justForTestingModifier")
}).
Expand Down
18 changes: 13 additions & 5 deletions exmples/simple-rest-server/bench.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
counter = 0

request = function()
path = "/echo/" .. counter
wrk.headers["X-Counter"] = counter
counter = counter + 1
return wrk.format(nil, path)
end
path = "/echo-post"
counter = counter + 1
wrk.method = "POST"
wrk.body = '{"x": "something", "y": "anotherThing", "randomId": ' .. counter .. '}'
wrk.headers["X-Counter"] = counter
return wrk.format(nil, path)
end

response = function(status, headers, body)
if status ~= 200 then
io.write(body)
end
end
2 changes: 1 addition & 1 deletion exmples/simple-rest-server/cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func main() {
SetResponseHandler(
http.StatusOK,
func(ctx context.Context, r stub.RESTResponse) *stub.Error {
return stub.WrapError(json.Unmarshal(r.GetBody(), &res1))
return stub.WrapError(json.UnmarshalNoEscape(r.GetBody(), &res1))
},
).
Run(context.Background())
Expand Down
4 changes: 2 additions & 2 deletions internal/common/rpc_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ var SimpleIncomingJSONRPC = func() ronykit.IncomingRPCContainer {
}

func (e *simpleIncomingJSONRPC) Unmarshal(data []byte) error {
return json.Unmarshal(data, e)
return json.UnmarshalNoEscape(data, e)
}

func (e *simpleIncomingJSONRPC) Fill(m ronykit.Message) error {
return json.Unmarshal(e.Payload, m)
return json.UnmarshalNoEscape(e.Payload, m)
}

func (e *simpleIncomingJSONRPC) GetHdr(key string) string {
Expand Down
4 changes: 2 additions & 2 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func UnmarshalMessage(data []byte, m Message) error {
case encoding.TextUnmarshaler:
err = v.UnmarshalText(data)
default:
err = json.Unmarshal(data, m)
err = json.UnmarshalNoEscape(data, m)
}

return err
Expand All @@ -84,7 +84,7 @@ func MarshalMessage(m Message) ([]byte, error) {
case encoding.TextMarshaler:
return v.MarshalText()
default:
return json.Marshal(m)
return json.MarshalNoEscape(m)
}
}

Expand Down
2 changes: 1 addition & 1 deletion stub/stub_rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func (hc *restClientCtx) AutoRun(
var reqBody []byte
switch enc {
case ronykit.JSON:
reqBody, _ = json.Marshal(m)
reqBody, _ = json.MarshalNoEscape(m)
default:
reqBody, _ = ronykit.MarshalMessage(m)
}
Expand Down
2 changes: 2 additions & 0 deletions stub/stub_rest_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package stub

type RESTOption func(ctx *restClientCtx)

// WithPreflightREST register one or many handlers to run in sequence before
// actually making requests.
func WithPreflightREST(h ...RESTPreflightHandler) RESTOption {
return func(ctx *restClientCtx) {
ctx.preflights = append(ctx.preflights[:0], h...)
Expand Down
2 changes: 1 addition & 1 deletion stub/stub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var _ = Describe("Stub Basic Functionality", func() {
switch r.StatusCode() {
case http.StatusOK:
v := &ipInfoResponse{}
Expect(json.Unmarshal(r.GetBody(), v)).To(Succeed())
Expect(json.UnmarshalNoEscape(r.GetBody(), v)).To(Succeed())
Expect(v.Readme).To(Not(BeEmpty()))
Expect(v.IP).To(Not(BeEmpty()))
default:
Expand Down

0 comments on commit 971df06

Please sign in to comment.