-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor unknown message decoding (#30)
* Initial unknown resolver refactor * Finish moving all decoders * Add test coverage for proto file parsing
- Loading branch information
1 parent
94c7a06
commit 1b97c5b
Showing
8 changed files
with
147 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{"service":"bradleyjkemp.github.io.TestService","method":"TestUnaryClientRequest","messages":[{"message_origin":"client","raw_message":"ChEaDUNsaWVudFJlcXVlc3QgARAB","message":{"1":{"3":"ClientRequest","4":1},"2":1},"timestamp":"2019-06-24T19:19:46.644943+01:00"},{"message_origin":"server","raw_message":"ChIaDlNlcnZlclJlc3BvbnNlIAIQAg==","message":{"1":{"3":"ServerResponse","4":2},"2":2},"timestamp":"2019-06-24T19:19:46.644943+01:00"}],"metadata":{":authority":["bradleyjkemp.github.io:444"],"content-type":["application/grpc"],"user-agent":["grpc-go/1.21.1"],"via":["HTTP/2.0 127.0.0.1:16354"]}} | ||
{"service":"bradleyjkemp.github.io.TestService","method":"TestUnaryClientRequest","messages":[{"message_origin":"client","raw_message":"ChEaDUNsaWVudFJlcXVlc3QgARAB","message":{"outerValue":{"innerValue":"ClientRequest","innerNum":1},"outerNum":1},"timestamp":"2019-06-24T19:19:46.644943+01:00"},{"message_origin":"server","raw_message":"ChIaDlNlcnZlclJlc3BvbnNlIAIQAg==","message":{"outerValue":{"innerValue":"ServerResponse","innerNum":2},"outerNum":2},"timestamp":"2019-06-24T19:19:46.644943+01:00"}],"metadata":{":authority":["bradleyjkemp.github.io:444"],"content-type":["application/grpc"],"user-agent":["grpc-go/1.21.1"],"via":["HTTP/2.0 127.0.0.1:16354"]}} | ||
{"service":"bradleyjkemp.github.io.TestService","method":"TestStreamingServerMessages","messages":[{"message_origin":"server","raw_message":"ChIaDlNlcnZlck1lc3NhZ2UxIAMQAw==","message":{"1":{"3":"ServerMessage1","4":3},"2":3},"timestamp":"2019-06-24T19:19:46.644943+01:00"},{"message_origin":"server","raw_message":"ChIaDlNlcnZlck1lc3NhZ2UyIAQQBA==","message":{"1":{"3":"ServerMessage2","4":4},"2":4},"timestamp":"2019-06-24T19:19:46.644943+01:00"}],"metadata":{":authority":["a-different-domain.github.io:444"],"content-type":["application/grpc"],"forwarded":["proto=https"],"user-agent":["grpc-go/1.21.1"],"via":["HTTP/2.0 127.0.0.1:16354"]}} | ||
{"service":"grpc.gateway.testing.EchoService","method":"Echo","messages":[{"message_origin":"server","raw_message":"ChIaDlNlcnZlck1lc3NhZ2UxIAMQAw==","message":{"1":{"3":"ServerMessage1","4":3},"2":3},"timestamp":"2019-06-24T19:19:46.644943+01:00"},{"message_origin":"server","raw_message":"ChIaDlNlcnZlck1lc3NhZ2UxIAMQAw==","message":{"1":{"3":"ServerMessage1","4":3},"2":3},"timestamp":"2019-06-24T19:19:46.644943+01:00"}],"metadata":{":authority":["grpc-web.github.io"],"accept":["*/*"],"accept-encoding":["gzip, deflate, br"],"accept-language":["en-US,en;q=0.9"],"cache-control":["no-cache"],"content-type":["application/grpc+proto"],"custom-header-1":["value1"],"origin":["http://localhost:8081"],"pragma":["no-cache"],"referer":["http://localhost:8081/echotest.html"],"user-agent":["Mozilla/5.0"],"via":["HTTP/2.0 127.0.0.1:16354"],"x-grpc-web":["1"],"x-user-agent":["grpc-web-javascript/0.1"]}} | ||
{"service":"grpc.gateway.testing.EchoService","method":"Echo","messages":[{"message_origin":"server","raw_message":"ChIaDlNlcnZlck1lc3NhZ2UxIAMQAw==","message":{"1":{"3":"ServerMessage1","4":3},"2":3},"timestamp":"2019-06-24T19:19:46.644943+01:00"},{"message_origin":"server","raw_message":"ChIaDlNlcnZlck1lc3NhZ2UxIAMQAw==","message":{"1":{"3":"ServerMessage1","4":3},"2":3},"timestamp":"2019-06-24T19:19:46.644943+01:00"}],"metadata":{":authority":["grpc-web.github.io:1234"],"accept":["*/*"],"accept-encoding":["gzip, deflate, br"],"accept-language":["en-US,en;q=0.9"],"cache-control":["no-cache"],"content-type":["application/grpc+proto"],"custom-header-1":["value1"],"forwarded":["proto=https"],"origin":["http://localhost:8081"],"pragma":["no-cache"],"referer":["http://localhost:8081/echotest.html"],"user-agent":["Mozilla/5.0"],"via":["HTTP/2.0 127.0.0.1:16354"],"x-grpc-web":["1"],"x-user-agent":["grpc-web-javascript/0.1"]}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package proto_decoder | ||
|
||
import ( | ||
"github.com/bradleyjkemp/grpc-tools/internal" | ||
"github.com/golang/protobuf/proto" | ||
"github.com/jhump/protoreflect/desc" | ||
"github.com/jhump/protoreflect/dynamic" | ||
) | ||
|
||
type MessageResolver interface { | ||
resolve(fullMethod string, direction internal.MessageOrigin, raw []byte) (*desc.MessageDescriptor, error) | ||
} | ||
|
||
type MessageDecoder interface { | ||
Decode(fullMethod string, direction internal.MessageOrigin, raw []byte) (*dynamic.Message, error) | ||
} | ||
|
||
type messageDecoder struct { | ||
resolvers []MessageResolver | ||
} | ||
|
||
// Chain together a number of resolvers to decode incoming messages. | ||
// Resolvers are in priority order, the first to return a nil error | ||
// is used to decode the message. | ||
func NewDecoder(resolvers ...MessageResolver) *messageDecoder { | ||
return &messageDecoder{ | ||
resolvers: resolvers, | ||
} | ||
} | ||
|
||
func (d *messageDecoder) Decode(fullMethod string, direction internal.MessageOrigin, raw []byte) (*dynamic.Message, error) { | ||
var err error | ||
for _, resolver := range d.resolvers { | ||
var descriptor *desc.MessageDescriptor | ||
descriptor, err = resolver.resolve(fullMethod, direction, raw) | ||
if err != nil { | ||
continue | ||
} | ||
dyn := dynamic.NewMessage(descriptor) | ||
// now unmarshal again using the new generated message type | ||
err = proto.Unmarshal(raw, dyn) | ||
if err == nil { | ||
return dyn, nil | ||
} | ||
} | ||
return nil, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package proto_decoder | ||
|
||
import ( | ||
"fmt" | ||
"github.com/bradleyjkemp/grpc-tools/internal" | ||
"github.com/bradleyjkemp/grpc-tools/internal/proto_descriptor" | ||
"github.com/jhump/protoreflect/desc" | ||
) | ||
|
||
type descriptorResolver struct { | ||
methodDescriptors map[string]*desc.MethodDescriptor | ||
} | ||
|
||
func (d *descriptorResolver) resolve(fullMethod string, direction internal.MessageOrigin, raw []byte) (*desc.MessageDescriptor, error) { | ||
if descriptor, ok := d.methodDescriptors[fullMethod]; ok { | ||
switch direction { | ||
case internal.ClientMessage: | ||
return descriptor.GetInputType(), nil | ||
case internal.ServerMessage: | ||
return descriptor.GetOutputType(), nil | ||
} | ||
} | ||
return nil, fmt.Errorf("method not known") | ||
} | ||
|
||
func NewFileResolver(protoFileRoots ...string) (*descriptorResolver, error) { | ||
descs, err := proto_descriptor.LoadProtoDirectories(protoFileRoots...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &descriptorResolver{ | ||
descs, | ||
}, nil | ||
} | ||
|
||
func NewDescriptorResolver(protoFileDescriptors ...string) (*descriptorResolver, error) { | ||
descs, err := proto_descriptor.LoadProtoDescriptors(protoFileDescriptors...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &descriptorResolver{ | ||
descs, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters