twirp_rails 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/Gemfile.lock +1 -1
- data/lib/twirp_rails/generators/twirp/twirp_generator.rb +29 -11
- data/lib/twirp_rails/routes.rb +9 -3
- data/lib/twirp_rails/twirp.rb +1 -1
- data/lib/twirp_rails/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '06560208f92156b0a681508de14468457ca498294e139f6f949f14f037ca03d4'
|
4
|
+
data.tar.gz: '0989d1e84e085f7faaac0514c4140f96d3ffcf11f3bb157f1c775c38f06f83d3'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a56fe829dba492ce1df60576e045a254a2e116004065ce4d866d625d3717fefda40b48f5d31f5470c5b685f22ab2c3acf76232c6f91959823cf35c57906240ef
|
7
|
+
data.tar.gz: dec12ac4cb49e5d6dca4f8460fa2029648e747b5ddba3653a44f09754dbcf27040cd02473279a11bb2e9a09bedd7bdaa8509f50a8a0926a4101b804ea97b863d
|
data/CHANGELOG.md
CHANGED
@@ -4,6 +4,16 @@ All notable changes to this project will be documented in this file.
|
|
4
4
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
5
5
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
6
6
|
|
7
|
+
## 0.3.2 - 2020-03-12
|
8
|
+
|
9
|
+
### Added
|
10
|
+
- Correct code generation for proto files with packages.
|
11
|
+
|
12
|
+
## 0.3.1 - 2020-03-10
|
13
|
+
|
14
|
+
### Fix
|
15
|
+
- Fix default log subscriber to use string keys to avoid SemanticLogger use :exception as parameter
|
16
|
+
|
7
17
|
## 0.3.0 - 2020-02-28
|
8
18
|
|
9
19
|
### Added
|
data/Gemfile.lock
CHANGED
@@ -15,7 +15,7 @@ class TwirpGenerator < Rails::Generators::NamedBase
|
|
15
15
|
def check_requirements
|
16
16
|
in_root do
|
17
17
|
unless File.exist?(proto_file_name)
|
18
|
-
raise "#{proto_file_name} not found
|
18
|
+
raise "#{proto_file_name} not found"
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
@@ -40,7 +40,8 @@ class TwirpGenerator < Rails::Generators::NamedBase
|
|
40
40
|
|
41
41
|
def generate_twirp_files
|
42
42
|
in_root do
|
43
|
-
|
43
|
+
protos_mask = File.join *['app/protos', class_path, '**/*.proto'].flatten
|
44
|
+
proto_files = Dir.glob protos_mask
|
44
45
|
|
45
46
|
proto_files.each do |file|
|
46
47
|
gen_swagger = gen_swagger? && file =~ %r{/#{file_name}\.proto$}
|
@@ -56,33 +57,40 @@ class TwirpGenerator < Rails::Generators::NamedBase
|
|
56
57
|
|
57
58
|
PROTO_RPC_REGEXP = /\brpc\s+(\S+)\s*\(\s*(\S+)\s*\)\s*returns\s*\(\s*(\S+)\s*\)/m.freeze
|
58
59
|
|
60
|
+
def create_module_files
|
61
|
+
return if regular_class_path.empty?
|
62
|
+
|
63
|
+
class_path.length.times do |i|
|
64
|
+
current_path = class_path[0..i]
|
65
|
+
create_file File.join('app/rpc', "#{current_path.join('/')}.rb"),
|
66
|
+
module_hier(current_path.map(&:camelize), 0)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
59
70
|
def generate_handler
|
60
|
-
methods = proto_content.scan(PROTO_RPC_REGEXP).map do |method,
|
61
|
-
result_type = proto_type_to_ruby(result_type)
|
71
|
+
methods = proto_content.scan(PROTO_RPC_REGEXP).map do |method, _arg_type, _result_type|
|
62
72
|
optimize_indentation <<~RUBY, 2
|
63
73
|
def #{method.underscore}(req, _env)
|
64
|
-
#{result_type}.new
|
65
74
|
end
|
66
75
|
RUBY
|
67
76
|
end.join("\n")
|
68
77
|
|
69
78
|
# Let us assume that the service name is the same file name
|
70
|
-
create_file "app/rpc/#{
|
79
|
+
create_file "app/rpc/#{file_path}_handler.rb", <<~RUBY
|
71
80
|
class #{class_name}Handler
|
72
|
-
|
73
81
|
#{methods}end
|
74
82
|
RUBY
|
75
83
|
end
|
76
84
|
|
77
85
|
def generate_route
|
78
|
-
route "mount_twirp
|
86
|
+
route "mount_twirp '#{file_path}'"
|
79
87
|
end
|
80
88
|
|
81
89
|
def generate_rspec_files
|
82
90
|
in_root do
|
83
91
|
return unless File.exist?('spec')
|
84
92
|
|
85
|
-
methods = proto_content.scan(PROTO_RPC_REGEXP).map do |method,
|
93
|
+
methods = proto_content.scan(PROTO_RPC_REGEXP).map do |method, _arg_type, result_type|
|
86
94
|
result_type = proto_type_to_ruby(result_type)
|
87
95
|
optimize_indentation <<~RUBY, 2
|
88
96
|
context '##{method.underscore}' do
|
@@ -96,7 +104,7 @@ class TwirpGenerator < Rails::Generators::NamedBase
|
|
96
104
|
RUBY
|
97
105
|
end.join("\n")
|
98
106
|
|
99
|
-
create_file "spec/rpc/#{
|
107
|
+
create_file "spec/rpc/#{file_path}_handler_spec.rb", <<~RUBY
|
100
108
|
require 'rails_helper'
|
101
109
|
|
102
110
|
describe #{class_name}Handler do
|
@@ -108,6 +116,16 @@ class TwirpGenerator < Rails::Generators::NamedBase
|
|
108
116
|
|
109
117
|
private
|
110
118
|
|
119
|
+
def module_hier(modules, indent)
|
120
|
+
return '' if modules.size.zero?
|
121
|
+
|
122
|
+
cur, *tail = modules
|
123
|
+
optimize_indentation <<~RUBY, indent
|
124
|
+
module #{cur}
|
125
|
+
#{module_hier(tail, indent + 2)}end
|
126
|
+
RUBY
|
127
|
+
end
|
128
|
+
|
111
129
|
def proto_type_to_ruby(result_type)
|
112
130
|
result_type.split('.').map(&:camelize).join('::')
|
113
131
|
end
|
@@ -139,7 +157,7 @@ class TwirpGenerator < Rails::Generators::NamedBase
|
|
139
157
|
end
|
140
158
|
|
141
159
|
def proto_file_name
|
142
|
-
"app/protos/#{
|
160
|
+
"app/protos/#{file_path}.proto"
|
143
161
|
end
|
144
162
|
|
145
163
|
def gen_swagger?
|
data/lib/twirp_rails/routes.rb
CHANGED
@@ -13,10 +13,16 @@ module TwirpRails
|
|
13
13
|
when String, Symbol
|
14
14
|
service_class = Helper.constantize_first "#{name}_service", name
|
15
15
|
|
16
|
-
|
16
|
+
unless service_class
|
17
|
+
msg = "mount_twirp of #{name} error. #{name.camelize}Service or #{name.camelize} class is not found"
|
17
18
|
|
18
|
-
|
19
|
+
raise TwirpRails::Error, msg unless Rails.env.development?
|
20
|
+
|
21
|
+
warn msg
|
22
|
+
return
|
23
|
+
end
|
19
24
|
|
25
|
+
handler ||= "#{name}_handler".camelize.constantize
|
20
26
|
else
|
21
27
|
raise 'twirp service name required'
|
22
28
|
end
|
@@ -35,7 +41,7 @@ module TwirpRails
|
|
35
41
|
|
36
42
|
def self.constantize_first(*variants)
|
37
43
|
variants.each do |name|
|
38
|
-
clazz = name.to_s.camelize.
|
44
|
+
clazz = name.to_s.camelize.safe_constantize
|
39
45
|
|
40
46
|
return clazz if clazz
|
41
47
|
end
|
data/lib/twirp_rails/twirp.rb
CHANGED
@@ -5,7 +5,7 @@ module TwirpRails
|
|
5
5
|
twirp_path = Rails.root.join('lib/twirp').to_s
|
6
6
|
$LOAD_PATH.unshift(twirp_path) if !$LOAD_PATH.include?(twirp_path)
|
7
7
|
|
8
|
-
Dir.glob(Rails.root.join('lib/twirp
|
8
|
+
Dir.glob(Rails.root.join('lib/twirp/**/*_twirp.rb')).sort.each do |file|
|
9
9
|
require file
|
10
10
|
end
|
11
11
|
end
|
data/lib/twirp_rails/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: twirp_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexandr Zimin
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-03-
|
11
|
+
date: 2020-03-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: twirp
|