twirp_rails 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: de31905ff271c17c94f4d6032ce473cfbd4dba1cdad97266d13fe5977172fd48
4
- data.tar.gz: 8c9b485008788d9ad48867e73831bf5f5ade8d01516ea394c0d25808b67bce52
3
+ metadata.gz: '06560208f92156b0a681508de14468457ca498294e139f6f949f14f037ca03d4'
4
+ data.tar.gz: '0989d1e84e085f7faaac0514c4140f96d3ffcf11f3bb157f1c775c38f06f83d3'
5
5
  SHA512:
6
- metadata.gz: f1b5a97a142433adbbf69896c3ce51c9315bba93020dd0b6f780c33462f0fe0b65cea3c920347e84dcb9113ac146aeb670744851ea54ae94e02681961f172095
7
- data.tar.gz: 45f8c665c1e546c9646d3a164d0024147940aa18bf685907063d39c9a043e12c4a7cf2bd80c84ae22851446ee14b0e124cec3f9d28c89f4070de0756d5352ccd
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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- twirp_rails (0.3.1)
4
+ twirp_rails (0.3.2)
5
5
  railties (~> 6.0)
6
6
  twirp (~> 1)
7
7
 
@@ -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 #{`pwd`} #{`ls`}"
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
- proto_files = Dir.glob 'app/protos/**/*.proto'
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, arg_type, result_type|
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/#{file_name}_handler.rb", <<~RUBY
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 :#{file_name}"
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, arg_type, result_type|
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/#{file_name}_handler_spec.rb", <<~RUBY
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/#{file_name}.proto"
160
+ "app/protos/#{file_path}.proto"
143
161
  end
144
162
 
145
163
  def gen_swagger?
@@ -13,10 +13,16 @@ module TwirpRails
13
13
  when String, Symbol
14
14
  service_class = Helper.constantize_first "#{name}_service", name
15
15
 
16
- raise "#{name.camelize}Service or #{name.camelize} is not found" unless service_class
16
+ unless service_class
17
+ msg = "mount_twirp of #{name} error. #{name.camelize}Service or #{name.camelize} class is not found"
17
18
 
18
- handler ||= "#{name}_handler".camelize.constantize
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.constantize
44
+ clazz = name.to_s.camelize.safe_constantize
39
45
 
40
46
  return clazz if clazz
41
47
  end
@@ -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/*_twirp.rb')).sort.each do |file|
8
+ Dir.glob(Rails.root.join('lib/twirp/**/*_twirp.rb')).sort.each do |file|
9
9
  require file
10
10
  end
11
11
  end
@@ -1,3 +1,3 @@
1
1
  module TwirpRails
2
- VERSION = '0.3.1'.freeze
2
+ VERSION = '0.3.2'.freeze
3
3
  end
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.1
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-10 00:00:00.000000000 Z
11
+ date: 2020-03-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: twirp