Skip to content

Commit

Permalink
Merge pull request #8 from k1LoW/convert-type
Browse files Browse the repository at this point in the history
Fix keys convert: use OrigName option
  • Loading branch information
k1LoW authored Jul 4, 2022
2 parents d8f4044 + 0ac83c7 commit 76647a1
Showing 1 changed file with 27 additions and 26 deletions.
53 changes: 27 additions & 26 deletions grpcstub.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package grpcstub

import (
"bytes"
"context"
"encoding/json"
"errors"
Expand All @@ -13,7 +14,8 @@ import (
"testing"
"time"

"github.com/golang/protobuf/proto" //nolint
"github.com/golang/protobuf/jsonpb" //nolint
"github.com/golang/protobuf/proto" //nolint
"github.com/jhump/protoreflect/desc"
"github.com/jhump/protoreflect/desc/protoparse"
"github.com/jhump/protoreflect/dynamic"
Expand Down Expand Up @@ -42,15 +44,6 @@ func (m Message) Set(pointer string, value interface{}) error {
return jsonpointer.Set(m, pointer, value)
}

func (m Message) format(mdd *desc.MessageDescriptor) {
for _, f := range mdd.GetFields() {
if f.GetName() != f.GetJSONName() {
m[f.GetName()] = m[f.GetJSONName()]
delete(m, f.GetJSONName())
}
}
}

type Request struct {
Service string
Method string
Expand Down Expand Up @@ -408,15 +401,17 @@ func (s *Server) createUnaryHandler(md *desc.MethodDescriptor) func(srv interfac
if err := dec(in); err != nil {
return nil, err
}
b, err := json.Marshal(in)
if err != nil {
b := new(bytes.Buffer)
marshaler := jsonpb.Marshaler{
OrigName: true,
}
if err := marshaler.Marshal(b, in); err != nil {
return nil, err
}
m := Message{}
if err := json.Unmarshal(b, &m); err != nil {
if err := json.Unmarshal(b.Bytes(), &m); err != nil {
return nil, err
}
m.format(md.GetInputType())
r := newRequest(md.GetService().GetFullyQualifiedName(), md.GetName(), m)
h, ok := metadata.FromIncomingContext(ctx)
if ok {
Expand Down Expand Up @@ -496,15 +491,17 @@ func (s *Server) createServerStreamingHandler(md *desc.MethodDescriptor) func(sr
if err := stream.RecvMsg(in); err != nil {
return err
}
b, err := json.Marshal(in)
if err != nil {
b := new(bytes.Buffer)
marshaler := jsonpb.Marshaler{
OrigName: true,
}
if err := marshaler.Marshal(b, in); err != nil {
return err
}
m := Message{}
if err := json.Unmarshal(b, &m); err != nil {
if err := json.Unmarshal(b.Bytes(), &m); err != nil {
return err
}
m.format(md.GetInputType())
r := newRequest(md.GetService().GetFullyQualifiedName(), md.GetName(), m)
h, ok := metadata.FromIncomingContext(stream.Context())
if ok {
Expand Down Expand Up @@ -569,15 +566,17 @@ func (s *Server) createClientStreamingHandler(md *desc.MethodDescriptor) func(sr
in := msgFactory.NewMessage(md.GetInputType())
err := stream.RecvMsg(in)
if err == nil {
b, err := json.Marshal(in)
if err != nil {
b := new(bytes.Buffer)
marshaler := jsonpb.Marshaler{
OrigName: true,
}
if err := marshaler.Marshal(b, in); err != nil {
return err
}
m := Message{}
if err := json.Unmarshal(b, &m); err != nil {
if err := json.Unmarshal(b.Bytes(), &m); err != nil {
return err
}
m.format(md.GetInputType())
r := newRequest(md.GetService().GetFullyQualifiedName(), md.GetName(), m)
h, ok := metadata.FromIncomingContext(stream.Context())
if ok {
Expand Down Expand Up @@ -654,15 +653,17 @@ func (s *Server) createBiStreamingHandler(md *desc.MethodDescriptor) func(srv in
if err != nil {
return err
}
b, err := json.Marshal(in)
if err != nil {
b := new(bytes.Buffer)
marshaler := jsonpb.Marshaler{
OrigName: true,
}
if err := marshaler.Marshal(b, in); err != nil {
return err
}
m := Message{}
if err := json.Unmarshal(b, &m); err != nil {
if err := json.Unmarshal(b.Bytes(), &m); err != nil {
return err
}
m.format(md.GetInputType())
r := newRequest(md.GetService().GetFullyQualifiedName(), md.GetName(), m)
h, ok := metadata.FromIncomingContext(stream.Context())
if ok {
Expand Down

0 comments on commit 76647a1

Please sign in to comment.