Skip to content

Commit

Permalink
Map Google Well Known Types from plain JSON/Ruby types
Browse files Browse the repository at this point in the history
  • Loading branch information
dorner committed Apr 4, 2024
1 parent 24e2835 commit af76199
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
23 changes: 23 additions & 0 deletions lib/grpc_rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,29 @@ def assign_value(proto, path, value)
proto.public_send(:"#{tokens.last}=", value) if proto.respond_to?(:"#{tokens.last}=")
end

def map_wkt(proto, params)
proto.to_a.each do |descriptor|
field = descriptor.name
case descriptor.subtype&.name
when 'google.protobuf.Struct'
params[field] = Google::Protobuf::Struct.from_hash(params[field])
when 'google.protobuf.Timestamp'
params[field] = Google::Protobuf::Timestamp.from_time(Time.new(params[field]))
when 'google.protobuf.Value'
params[field] = Google::Protobuf::Value.from_ruby(params[field])
when 'google.protobuf.ListValue'
params[field] = Google::Protobuf::ListValue.from_a(params[field])
else
map_wkt(descriptor.subtype, params[field]) if descriptor.subtype
end
end
end

def init_request(request_class, params)
map_wkt(request_class.descriptor, params)
request_class.new(params)
end

def assign_params(request, param_hash, body_string, params)
parameters = params.to_h.deep_dup
# each instance of {variable} means that we set the corresponding param variable into the
Expand Down
2 changes: 1 addition & 1 deletion protoc-gen-rails/internal/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class {{.ControllerName}}Controller < ActionController::Base
{{range .Methods }}
def {{.Name}}
fields = {{.RequestType}}.descriptor.to_a.map(&:name)
grpc_request = {{.RequestType}}.new(request.parameters.to_h.slice(*fields))
grpc_request = GrpcRest.init_request({{.RequestType}}, request.parameters.to_h.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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ class MyServiceController < ActionController::Base

def test
fields = Testdata::TestRequest.descriptor.to_a.map(&:name)
grpc_request = Testdata::TestRequest.new(request.parameters.to_h.slice(*fields))
grpc_request = GrpcRest.init_request(Testdata::TestRequest, request.parameters.to_h.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 = Testdata::TestRequest.new(request.parameters.to_h.slice(*fields))
grpc_request = GrpcRest.init_request(Testdata::TestRequest, request.parameters.to_h.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 = Testdata::TestRequest.new(request.parameters.to_h.slice(*fields))
grpc_request = GrpcRest.init_request(Testdata::TestRequest, request.parameters.to_h.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
Expand Down

0 comments on commit af76199

Please sign in to comment.