Skip to content

Commit

Permalink
Support requests in camel case (#10)
Browse files Browse the repository at this point in the history
* support requests in camel case

* update go tests

* update

* remove binary file
  • Loading branch information
nkadovic authored Jul 9, 2024
1 parent 1dc54ed commit b753a35
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 31 deletions.
56 changes: 29 additions & 27 deletions protoc-gen-rails/internal/output.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
package internal

import (
"bytes"
"fmt"
"github.com/iancoleman/strcase"
"google.golang.org/protobuf/types/descriptorpb"
"bytes"
"fmt"
"strings"
"text/template"

"github.com/iancoleman/strcase"
"google.golang.org/protobuf/types/descriptorpb"
)

type FileResult struct {
Name string
Name string
Content string
}

type controller struct {
ControllerName string
Methods []method
ServiceName string
ControllerName string
Methods []method
ServiceName string
FullServiceName string
MethodName string
MethodName string
}

type method struct {
Name string
Name string
RequestType string
Path string
PathInfo []PathInfo
Body string
HttpMethod string
Path string
PathInfo []PathInfo
Body string
HttpMethod string
}

type Route struct {
MethodName string
Path string
Path string
Controller string
HttpMethod string
}
Expand Down Expand Up @@ -67,7 +68,8 @@ class {{.ControllerName}}Controller < ActionController::Base
{{range .Methods }}
def {{.Name}}
fields = {{.RequestType}}.descriptor.to_a.map(&:name)
grpc_request = GrpcRest.init_request({{.RequestType}}, request.parameters.to_h.slice(*fields))
parameters = request.parameters.to_h.deep_transform_keys(&:underscore)
grpc_request = GrpcRest.init_request({{.RequestType}}, parameters.slice(*fields))
GrpcRest.assign_params(grpc_request, METHOD_PARAM_MAP["{{.Name}}"], "{{.Body}}", request.parameters)
render json: GrpcRest.send_request("{{$fullServiceName}}", "{{.Name}}", grpc_request)
end
Expand All @@ -76,11 +78,11 @@ end
`

func ProcessService(service *descriptorpb.ServiceDescriptorProto, pkg string) (FileResult, []Route, error) {
var routes []Route
var routes []Route
data := controller{
Methods: []method{},
ServiceName: Classify(service.GetName()),
ControllerName: Demodulize(service.GetName()),
Methods: []method{},
ServiceName: Classify(service.GetName()),
ControllerName: Demodulize(service.GetName()),
FullServiceName: Classify(pkg + "." + service.GetName()),
}
for _, m := range service.GetMethod() {
Expand All @@ -94,17 +96,17 @@ func ProcessService(service *descriptorpb.ServiceDescriptorProto, pkg string) (F
return FileResult{}, routes, err
}
controllerMethod := method{
Name: strcase.ToSnake(m.GetName()),
Name: strcase.ToSnake(m.GetName()),
RequestType: Classify(m.GetInputType()),
Path: path,
HttpMethod: httpMethod,
Body: opts.Body,
PathInfo: pathInfo,
Path: path,
HttpMethod: httpMethod,
Body: opts.Body,
PathInfo: pathInfo,
}
data.Methods = append(data.Methods, controllerMethod)
routes = append(routes, Route{
HttpMethod: strings.ToLower(httpMethod),
Path: SanitizePath(path),
Path: SanitizePath(path),
Controller: strcase.ToSnake(data.ControllerName),
MethodName: strcase.ToSnake(m.GetName()),
})
Expand All @@ -120,7 +122,7 @@ func ProcessService(service *descriptorpb.ServiceDescriptorProto, pkg string) (F
}
return FileResult{
Content: resultContent.String(),
Name: fmt.Sprintf("app/controllers/%s_controller.rb", strcase.ToSnake(data.ControllerName)),
Name: fmt.Sprintf("app/controllers/%s_controller.rb", strcase.ToSnake(data.ControllerName)),
}, routes, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,32 @@ class MyServiceController < ActionController::Base

def test
fields = Testdata::TestRequest.descriptor.to_a.map(&:name)
grpc_request = GrpcRest.init_request(Testdata::TestRequest, request.parameters.to_h.slice(*fields))
parameters = request.parameters.to_h.deep_transform_keys(&:underscore)
grpc_request = GrpcRest.init_request(Testdata::TestRequest, parameters.slice(*fields))
GrpcRest.assign_params(grpc_request, METHOD_PARAM_MAP["test"], "", request.parameters)
render json: GrpcRest.send_request("Testdata::MyService", "test", grpc_request)
end

def test_2
fields = Testdata::TestRequest.descriptor.to_a.map(&:name)
grpc_request = GrpcRest.init_request(Testdata::TestRequest, request.parameters.to_h.slice(*fields))
parameters = request.parameters.to_h.deep_transform_keys(&:underscore)
grpc_request = GrpcRest.init_request(Testdata::TestRequest, parameters.slice(*fields))
GrpcRest.assign_params(grpc_request, METHOD_PARAM_MAP["test_2"], "second_record", request.parameters)
render json: GrpcRest.send_request("Testdata::MyService", "test_2", grpc_request)
end

def test_3
fields = Testdata::TestRequest.descriptor.to_a.map(&:name)
grpc_request = GrpcRest.init_request(Testdata::TestRequest, request.parameters.to_h.slice(*fields))
parameters = request.parameters.to_h.deep_transform_keys(&:underscore)
grpc_request = GrpcRest.init_request(Testdata::TestRequest, parameters.slice(*fields))
GrpcRest.assign_params(grpc_request, METHOD_PARAM_MAP["test_3"], "", request.parameters)
render json: GrpcRest.send_request("Testdata::MyService", "test_3", grpc_request)
end

def test_4
fields = Testdata::TestRequest.descriptor.to_a.map(&:name)
grpc_request = GrpcRest.init_request(Testdata::TestRequest, request.parameters.to_h.slice(*fields))
parameters = request.parameters.to_h.deep_transform_keys(&:underscore)
grpc_request = GrpcRest.init_request(Testdata::TestRequest, parameters.slice(*fields))
GrpcRest.assign_params(grpc_request, METHOD_PARAM_MAP["test_4"], "*", request.parameters)
render json: GrpcRest.send_request("Testdata::MyService", "test_4", grpc_request)
end
Expand Down

0 comments on commit b753a35

Please sign in to comment.