Skip to content

Commit

Permalink
HTTP proto annotations & minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeroen Rinzema committed Feb 29, 2020
1 parent b357af8 commit 9aa7655
Show file tree
Hide file tree
Showing 18 changed files with 131 additions and 30 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
PROTODIR = annotations

proto-build: $(PROTODIR)/annotations.pb.go

%.pb.go: %.proto
protoc --proto_path=. --proto_path=$(PROTODIR) --go_out=paths=source_relative:. $^
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ flow "checkout" {
id = "<string>"
}
call "shipping" "warehouse.Send" {
call "shipping" "warehouse" "Send" {
request {
user = "{{ input:id }}"
}
Expand Down
54 changes: 54 additions & 0 deletions annotations/annotations.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions annotations/annotations.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
syntax = "proto3";

package maestro;

import "google/protobuf/descriptor.proto";

option go_package = "github.com/jexia/maestro/annotations";

extend google.protobuf.MethodOptions {
string http_endpoint = 50011;
}
16 changes: 8 additions & 8 deletions codec/proto/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func BenchmarkSimpleMarshal(b *testing.B) {
b.Fatal(err)
}

method := collection.GetService("proto.mock").GetEndpoint("simple")
method := collection.GetService("proto.mock").GetMethod("simple")
schema := method.GetInput().(protoc.Object)
flow := FindFlow(manifest, "simple")
specs := FindNode(flow, "first").Call.GetRequest()
Expand Down Expand Up @@ -128,7 +128,7 @@ func BenchmarkNestedMarshal(b *testing.B) {
b.Fatal(err)
}

method := collection.GetService("proto.mock").GetEndpoint("nested")
method := collection.GetService("proto.mock").GetMethod("nested")
schema := method.GetInput().(protoc.Object)
flow := FindFlow(manifest, "nested")
specs := FindNode(flow, "first").Call.GetRequest()
Expand Down Expand Up @@ -168,7 +168,7 @@ func BenchmarkRepeatedMarshal(b *testing.B) {
b.Fatal(err)
}

method := collection.GetService("proto.mock").GetEndpoint("repeated")
method := collection.GetService("proto.mock").GetMethod("repeated")
schema := method.GetInput().(protoc.Object)
flow := FindFlow(manifest, "repeated")
specs := FindNode(flow, "first").Call.GetRequest()
Expand Down Expand Up @@ -207,7 +207,7 @@ func BenchmarkSimpleUnmarshal(b *testing.B) {
b.Fatal(err)
}

method := collection.GetService("proto.mock").GetEndpoint("simple")
method := collection.GetService("proto.mock").GetMethod("simple")
schema := method.GetInput().(protoc.Object)
flow := FindFlow(manifest, "simple")
specs := FindNode(flow, "first").Call.GetRequest()
Expand Down Expand Up @@ -257,7 +257,7 @@ func BenchmarkNestedUnmarshal(b *testing.B) {
b.Fatal(err)
}

method := collection.GetService("proto.mock").GetEndpoint("nested")
method := collection.GetService("proto.mock").GetMethod("nested")
schema := method.GetInput().(protoc.Object)
flow := FindFlow(manifest, "nested")
specs := FindNode(flow, "first").Call.GetRequest()
Expand Down Expand Up @@ -309,7 +309,7 @@ func BenchmarkRepeatedUnmarshal(b *testing.B) {
b.Fatal(err)
}

method := collection.GetService("proto.mock").GetEndpoint("repeated")
method := collection.GetService("proto.mock").GetMethod("repeated")
schema := method.GetInput().(protoc.Object)
flow := FindFlow(manifest, "repeated")
specs := FindNode(flow, "first").Call.GetRequest()
Expand Down Expand Up @@ -347,7 +347,7 @@ func TestMarshal(t *testing.T) {
t.Fatal(err)
}

method := collection.GetService("proto.test").GetEndpoint("complete")
method := collection.GetService("proto.test").GetMethod("complete")
schema := method.GetInput().(protoc.Object)
flow := FindFlow(manifest, "complete")
specs := FindNode(flow, "first").Call.GetRequest()
Expand Down Expand Up @@ -436,7 +436,7 @@ func TestUnmarshal(t *testing.T) {
t.Fatal(err)
}

method := collection.GetService("proto.test").GetEndpoint("complete")
method := collection.GetService("proto.test").GetMethod("complete")
schema := method.GetInput().(protoc.Object)
flow := FindFlow(manifest, "complete")
specs := FindNode(flow, "first").Call.GetRequest()
Expand Down
6 changes: 5 additions & 1 deletion example/parsing/logger.proto
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
syntax = "proto3";

import "annotations/annotations.proto";

package proto;

service Logger {
rpc Call(Message) returns (Record);
rpc Append(Message) returns (Record);
rpc Append(Message) returns (Record) {
option (maestro.http_endpoint) = "typicode/demo/db";
};
}

message Response {
Expand Down
2 changes: 1 addition & 1 deletion example/parsing/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func main() {
collection, err := protoc.Collect(nil, ".")
collection, err := protoc.Collect([]string{"../../", "."}, ".")
if err != nil {
panic(err)
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/zclconf/go-cty v1.2.0
golang.org/x/net v0.0.0-20191109021931-daa7c04131f5
golang.org/x/sys v0.0.0-20200122134326-e047566fdf82 // indirect
google.golang.org/genproto v0.0.0-20191108220845-16a3f7862a1a
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v2 v2.2.8
)
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func (rw *rw) WriteHeader(int) {}
func ConstructCall(manifest *specs.Manifest, node *specs.Node, call *specs.Call, options Options) (flow.Call, error) {
service := GetService(manifest, call.GetService())
if service == nil {
return nil, trace.New(trace.WithMessage("the service for %s was not found", call.GetEndpoint()))
return nil, trace.New(trace.WithMessage("the service for %s was not found", call.GetMethod()))
}

constructor := GetCaller(options.Callers, service.Caller)
Expand All @@ -265,7 +265,7 @@ func ConstructCall(manifest *specs.Manifest, node *specs.Node, call *specs.Call,
return nil, err
}

caller, err := constructor.New(service.Host, service.Options)
caller, err := constructor.New(service.Host, options.Schema.GetService(service.Schema), service.Options)
if err != nil {
return nil, err
}
Expand All @@ -278,7 +278,7 @@ func ConstructCall(manifest *specs.Manifest, node *specs.Node, call *specs.Call,

reader, writer := io.Pipe()
req := &protocol.Request{
Endpoint: call.GetEndpoint(),
Endpoint: call.GetMethod(),
Context: ctx,
Body: body,
// Header: protocol.Header{},
Expand Down
5 changes: 3 additions & 2 deletions protocol/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/jexia/maestro/protocol"
"github.com/jexia/maestro/refs"
"github.com/jexia/maestro/schema"
"github.com/jexia/maestro/specs"
"github.com/julienschmidt/httprouter"
)
Expand All @@ -23,7 +24,7 @@ func (caller *Caller) Name() string {
}

// New constructs a new caller for the given host
func (caller *Caller) New(host string, opts specs.Options) (protocol.Call, error) {
func (caller *Caller) New(host string, schema schema.Service, opts specs.Options) (protocol.Call, error) {
options, err := ParseCallerOptions(opts)
if err != nil {
return nil, err
Expand Down Expand Up @@ -57,7 +58,7 @@ func (call *Call) Call(rw protocol.ResponseWriter, incoming *protocol.Request, r
return err
}

url.Path = "typicode/demo/db"
url.Path = incoming.Endpoint

req, err := http.NewRequestWithContext(incoming.Context, call.method, url.String(), incoming.Body)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion protocol/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestCaller(t *testing.T) {
}

constructor := &Caller{}
caller, err := constructor.New(server.URL, nil)
caller, err := constructor.New(server.URL, nil, nil)
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 2 additions & 1 deletion protocol/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/jexia/maestro/codec"
"github.com/jexia/maestro/flow"
"github.com/jexia/maestro/refs"
"github.com/jexia/maestro/schema"
"github.com/jexia/maestro/specs"
)

Expand Down Expand Up @@ -57,7 +58,7 @@ type Request struct {
// Caller constructs new calls which could be used to call services
type Caller interface {
Name() string
New(host string, options specs.Options) (Call, error)
New(host string, schema schema.Service, options specs.Options) (Call, error)
}

// Call is a preconfigured interface for a single service
Expand Down
19 changes: 16 additions & 3 deletions schema/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,32 @@ func (service *Service) GetName() string {
return service.Name
}

// GetEndpoint attempts to return the given service method
func (service *Service) GetEndpoint(name string) schema.Method {
// GetMethod attempts to return the given service method
func (service *Service) GetMethod(name string) schema.Method {
for key, method := range service.Methods {
if key != name {
continue
}

return NewMethod(name, method)
return NewMethod(key, method)
}

return nil
}

// GetMethods attempts to return the given service methods
func (service *Service) GetMethods() []schema.Method {
result := make([]schema.Method, len(service.Methods))

index := 0
for key, method := range service.Methods {
result[index] = NewMethod(key, method)
index++
}

return result
}

// Method represents a mock YAML service method
type Method struct {
Name string
Expand Down
2 changes: 1 addition & 1 deletion schema/protoc/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func UnmarshalFiles(imports []string, files []utils.FileInfo) ([]*desc.FileDescr
results := []*desc.FileDescriptor{}

for _, file := range files {
descs, err := parser.ParseFiles(filepath.Join(file.Path, file.Name()))
descs, err := parser.ParseFiles(file.Name())
if err != nil {
return nil, err
}
Expand Down
11 changes: 10 additions & 1 deletion schema/protoc/protoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (service *service) GetName() string {
return service.descriptor.GetName()
}

func (service *service) GetEndpoint(name string) schema.Method {
func (service *service) GetMethod(name string) schema.Method {
for _, method := range service.descriptor.GetMethods() {
if method.GetName() != name {
continue
Expand All @@ -73,6 +73,15 @@ func (service *service) GetEndpoint(name string) schema.Method {
return nil
}

func (service *service) GetMethods() []schema.Method {
result := make([]schema.Method, len(service.descriptor.GetMethods()))
for index, method := range service.descriptor.GetMethods() {
result[index] = NewMethod(method)
}

return result
}

func (service *service) GetDescriptor() *desc.ServiceDescriptor {
return service.descriptor
}
Expand Down
3 changes: 2 additions & 1 deletion schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ type Collection interface {
// Service represents a service which could be called in one of the flows
type Service interface {
GetName() string
GetEndpoint(name string) Method
GetMethod(name string) Method
GetMethods() []Method
}

// Method represents a service method
Expand Down
4 changes: 2 additions & 2 deletions specs/specs.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,8 @@ func (call *Call) GetService() string {
return call.Service
}

// GetEndpoint returns the call endpoint
func (call *Call) GetEndpoint() string {
// GetMethod returns the call endpoint
func (call *Call) GetMethod() string {
return call.Endpoint
}

Expand Down
8 changes: 4 additions & 4 deletions specs/strict/define.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func GetFlowSchema(schema schema.Collection, flow *specs.Flow) (schema.Method, e
return nil, trace.New(trace.WithMessage("undefined service alias '%s' in flow schema '%s'", GetService(flow.Schema), flow.Name))
}

method := service.GetEndpoint(GetMethod(flow.Schema))
method := service.GetMethod(GetMethod(flow.Schema))
if method == nil {
return nil, trace.New(trace.WithMessage("undefined method '%s' in flow schema '%s'", GetMethod(flow.Schema), flow.Name))
}
Expand All @@ -114,7 +114,7 @@ func GetFlowSchema(schema schema.Collection, flow *specs.Flow) (schema.Method, e

// DefineCall defineds the types for the given parameter map
func DefineCall(schema schema.Collection, manifest *specs.Manifest, node *specs.Node, call *specs.Call, flow specs.FlowManager) (err error) {
if call.GetEndpoint() == "" {
if call.GetMethod() == "" {
return nil
}

Expand All @@ -123,9 +123,9 @@ func DefineCall(schema schema.Collection, manifest *specs.Manifest, node *specs.
return trace.New(trace.WithMessage("undefined service alias '%s' in flow '%s'", call.GetService(), flow.GetName()))
}

method := service.GetEndpoint(call.GetEndpoint())
method := service.GetMethod(call.GetMethod())
if method == nil {
return trace.New(trace.WithMessage("undefined method '%s' in flow '%s'", call.GetEndpoint(), flow.GetName()))
return trace.New(trace.WithMessage("undefined method '%s' in flow '%s'", call.GetMethod(), flow.GetName()))
}

call.SetDescriptor(method)
Expand Down

0 comments on commit 9aa7655

Please sign in to comment.