grpc-rest 0.1.2 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c3dc93105554c8c92a3229f693ab0c93ed5833796ef7b7f11e94cb8821f2c425
4
- data.tar.gz: ac3ec407c674d16fb33e6bddccfa10b9e66859af3d5ecbad58d1ff2603e7c098
3
+ metadata.gz: 3ed7db60b29738e635e7b776539fc8207a5ec264e43912cb83562b1a44efdc58
4
+ data.tar.gz: a1653a18dab17be5abad7968ae405254ccc23973f799f9c7def7cd318b485681
5
5
  SHA512:
6
- metadata.gz: b1c05d823f083722774025bf8772d535b211b4ef01f3022baa92003a179ab2f3c1fcd6646c6b7eb5acf9ced1ff5abd3e962eea541ddfa7c0d9766bcc2649cb44
7
- data.tar.gz: c4c40a55b73429c7716ed2e6164c546f8d1c2e53030d6b1182475b66ec430590c637c691bac7af21294a62b39782a4ecc2d332c4afa68879cef4b803a170beb1
6
+ metadata.gz: 04b70b84065aa66b4441a4987519a351e0cafca0b58d5392a4e3a5a3b64429ca6bc4d6be877dfcf28f3c34fd77b04f7592500d89989737eab513b7c40591580f
7
+ data.tar.gz: fd35771ff21bbd7dc98550633a0eb96cb44b518f3356f792ce92dc02e9655f2683d0eab08bc1d36a1f8abf36f23fbabdd68e8bdb7f0dd4423ad311a27e7f80e6
data/CHANGELOG CHANGED
@@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## UNRELEASED
9
9
 
10
+ # 0.1.5 - 2024-04-04
11
+ - Parse Google Well Known Types (WKT) in request.
12
+
13
+ # 0.1.4 - 2024-04-04
14
+ - Remove geo-admin hardcoded requirement.
15
+ - Use baked-in Protobuf initialization to support nested objects properly.
16
+
10
17
  # 0.1.0 - 2024-03-01
11
18
 
12
19
  * Initial release.
@@ -1,3 +1,3 @@
1
1
  module GrpcRest
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.5"
3
3
  end
data/lib/grpc_rest.rb CHANGED
@@ -33,6 +33,29 @@ module GrpcRest
33
33
  proto.public_send(:"#{tokens.last}=", value) if proto.respond_to?(:"#{tokens.last}=")
34
34
  end
35
35
 
36
+ def map_wkt(proto, params)
37
+ proto.to_a.each do |descriptor|
38
+ field = descriptor.name
39
+ case descriptor.subtype&.name
40
+ when 'google.protobuf.Struct'
41
+ params[field] = Google::Protobuf::Struct.from_hash(params[field])
42
+ when 'google.protobuf.Timestamp'
43
+ params[field] = Google::Protobuf::Timestamp.from_time(Time.new(params[field]))
44
+ when 'google.protobuf.Value'
45
+ params[field] = Google::Protobuf::Value.from_ruby(params[field])
46
+ when 'google.protobuf.ListValue'
47
+ params[field] = Google::Protobuf::ListValue.from_a(params[field])
48
+ else
49
+ map_wkt(descriptor.subtype, params[field]) if descriptor.subtype
50
+ end
51
+ end
52
+ end
53
+
54
+ def init_request(request_class, params)
55
+ map_wkt(request_class.descriptor, params)
56
+ request_class.new(params)
57
+ end
58
+
36
59
  def assign_params(request, param_hash, body_string, params)
37
60
  parameters = params.to_h.deep_dup
38
61
  # each instance of {variable} means that we set the corresponding param variable into the
@@ -56,11 +79,6 @@ module GrpcRest
56
79
  sub_record.public_send(:"#{key}=", parameters.delete(key))
57
80
  end
58
81
  end
59
-
60
- # assign remaining parameters
61
- parameters.each do |k, v|
62
- assign_value(request, k, v)
63
- end
64
82
  end
65
83
 
66
84
  def error_msg(error)
@@ -16,7 +16,6 @@ type FileResult struct {
16
16
 
17
17
  type controller struct {
18
18
  ControllerName string
19
- ServiceFilePath string
20
19
  Methods []method
21
20
  ServiceName string
22
21
  FullServiceName string
@@ -41,7 +40,6 @@ type Route struct {
41
40
 
42
41
  var controllerTemplate = `
43
42
  require 'grpc_rest'
44
- require 'services/geo_admin/v1/test_services_pb'
45
43
  class {{.ControllerName}}Controller < ActionController::Base
46
44
  protect_from_forgery with: :null_session
47
45
 
@@ -60,8 +58,9 @@ class {{.ControllerName}}Controller < ActionController::Base
60
58
  {{$fullServiceName := .FullServiceName -}}
61
59
  {{range .Methods }}
62
60
  def {{.Name}}
63
- grpc_request = {{.RequestType}}.new
64
- GrpcRest.assign_params(grpc_request, METHOD_PARAM_MAP["{{.Name}}"], "{{.Body}}", request.parameters)
61
+ fields = {{.RequestType}}.descriptor.to_a.map(&:name)
62
+ grpc_request = GrpcRest.init_request({{.RequestType}}, request.parameters.to_h.slice(*fields))
63
+ GrpcRest.assign_params(grpc_request, METHOD_PARAM_MAP["{{.Name}}"], "{{.Body}}", request.parameters)
65
64
  render json: GrpcRest.send_request("{{$fullServiceName}}", "{{.Name}}", grpc_request)
66
65
  end
67
66
  {{end}}
@@ -74,7 +73,6 @@ func ProcessService(service *descriptorpb.ServiceDescriptorProto, pkg string) (F
74
73
  Methods: []method{},
75
74
  ServiceName: Classify(service.GetName()),
76
75
  ControllerName: Demodulize(service.GetName()),
77
- ServiceFilePath: FilePathify(pkg + "." + service.GetName()),
78
76
  FullServiceName: Classify(pkg + "." + service.GetName()),
79
77
  }
80
78
  for _, m := range service.GetMethod() {
@@ -1,6 +1,5 @@
1
1
 
2
2
  require 'grpc_rest'
3
- require 'services/geo_admin/v1/test_services_pb'
4
3
  class MyServiceController < ActionController::Base
5
4
  protect_from_forgery with: :null_session
6
5
 
@@ -23,20 +22,23 @@ class MyServiceController < ActionController::Base
23
22
  }.freeze
24
23
 
25
24
  def test
26
- grpc_request = Testdata::TestRequest.new
27
- GrpcRest.assign_params(grpc_request, METHOD_PARAM_MAP["test"], "*", request.parameters)
25
+ fields = Testdata::TestRequest.descriptor.to_a.map(&:name)
26
+ grpc_request = GrpcRest.init_request(Testdata::TestRequest, request.parameters.to_h.slice(*fields))
27
+ GrpcRest.assign_params(grpc_request, METHOD_PARAM_MAP["test"], "*", request.parameters)
28
28
  render json: GrpcRest.send_request("Testdata::MyService", "test", grpc_request)
29
29
  end
30
30
 
31
31
  def test_2
32
- grpc_request = Testdata::TestRequest.new
33
- GrpcRest.assign_params(grpc_request, METHOD_PARAM_MAP["test_2"], "second_record", request.parameters)
32
+ fields = Testdata::TestRequest.descriptor.to_a.map(&:name)
33
+ grpc_request = GrpcRest.init_request(Testdata::TestRequest, request.parameters.to_h.slice(*fields))
34
+ GrpcRest.assign_params(grpc_request, METHOD_PARAM_MAP["test_2"], "second_record", request.parameters)
34
35
  render json: GrpcRest.send_request("Testdata::MyService", "test_2", grpc_request)
35
36
  end
36
37
 
37
38
  def test_3
38
- grpc_request = Testdata::TestRequest.new
39
- GrpcRest.assign_params(grpc_request, METHOD_PARAM_MAP["test_3"], "", request.parameters)
39
+ fields = Testdata::TestRequest.descriptor.to_a.map(&:name)
40
+ grpc_request = GrpcRest.init_request(Testdata::TestRequest, request.parameters.to_h.slice(*fields))
41
+ GrpcRest.assign_params(grpc_request, METHOD_PARAM_MAP["test_3"], "", request.parameters)
40
42
  render json: GrpcRest.send_request("Testdata::MyService", "test_3", grpc_request)
41
43
  end
42
44
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grpc-rest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Orner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-03-04 00:00:00.000000000 Z
11
+ date: 2024-04-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: grpc