protod 0.1.0

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.
data/Gemfile.lock ADDED
@@ -0,0 +1,165 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ protod (0.1.0)
5
+ activemodel (>= 5.2.0)
6
+ activesupport
7
+ google-protobuf
8
+ rbs (>= 3.2.0)
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ actionmailer (0.6.1)
14
+ actionpack (>= 0.9.5)
15
+ actionpack (1.4.0)
16
+ activemodel (7.0.7.2)
17
+ activesupport (= 7.0.7.2)
18
+ activerecord (7.0.7.2)
19
+ activemodel (= 7.0.7.2)
20
+ activesupport (= 7.0.7.2)
21
+ activesupport (7.0.7.2)
22
+ concurrent-ruby (~> 1.0, >= 1.0.2)
23
+ i18n (>= 1.6, < 2)
24
+ minitest (>= 5.1)
25
+ tzinfo (~> 2.0)
26
+ ast (2.4.2)
27
+ base64 (0.1.1)
28
+ binding_of_caller (1.0.0)
29
+ debug_inspector (>= 0.0.1)
30
+ byebug (11.1.3)
31
+ coderay (1.1.3)
32
+ concurrent-ruby (1.2.2)
33
+ debug_inspector (1.1.0)
34
+ diff-lcs (1.5.0)
35
+ factory_bot (6.3.0)
36
+ activesupport (>= 5.0.0)
37
+ faker (3.2.1)
38
+ i18n (>= 1.8.11, < 2)
39
+ google-protobuf (3.24.3-x86_64-linux)
40
+ i18n (1.14.1)
41
+ concurrent-ruby (~> 1.0)
42
+ json (2.6.3)
43
+ language_server-protocol (3.17.0.3)
44
+ method_source (1.0.0)
45
+ minitest (5.19.0)
46
+ parallel (1.23.0)
47
+ parser (3.2.2.3)
48
+ ast (~> 2.4.1)
49
+ racc
50
+ proc_to_ast (0.1.0)
51
+ coderay
52
+ parser
53
+ unparser
54
+ pry (0.14.2)
55
+ coderay (~> 1.1)
56
+ method_source (~> 1.0)
57
+ pry-byebug (3.10.1)
58
+ byebug (~> 11.0)
59
+ pry (>= 0.13, < 0.15)
60
+ pry-doc (1.4.0)
61
+ pry (~> 0.11)
62
+ yard (~> 0.9.11)
63
+ racc (1.7.1)
64
+ rack (3.0.8)
65
+ rails (0.9.5)
66
+ actionmailer (>= 0.6.1)
67
+ actionpack (>= 1.4.0)
68
+ activerecord (>= 1.6.0)
69
+ rake (>= 0.4.15)
70
+ rainbow (3.1.1)
71
+ rake (13.0.6)
72
+ rbs (3.2.1)
73
+ regexp_parser (2.8.1)
74
+ rexml (3.2.6)
75
+ rspec (3.12.0)
76
+ rspec-core (~> 3.12.0)
77
+ rspec-expectations (~> 3.12.0)
78
+ rspec-mocks (~> 3.12.0)
79
+ rspec-core (3.12.2)
80
+ rspec-support (~> 3.12.0)
81
+ rspec-expectations (3.12.3)
82
+ diff-lcs (>= 1.2.0, < 2.0)
83
+ rspec-support (~> 3.12.0)
84
+ rspec-mocks (3.12.6)
85
+ diff-lcs (>= 1.2.0, < 2.0)
86
+ rspec-support (~> 3.12.0)
87
+ rspec-parameterized (1.0.0)
88
+ rspec-parameterized-core (< 2)
89
+ rspec-parameterized-table_syntax (< 2)
90
+ rspec-parameterized-core (1.0.0)
91
+ parser
92
+ proc_to_ast
93
+ rspec (>= 2.13, < 4)
94
+ unparser
95
+ rspec-parameterized-table_syntax (1.0.0)
96
+ binding_of_caller
97
+ rspec-parameterized-core (< 2)
98
+ rspec-support (3.12.1)
99
+ rubocop (1.56.2)
100
+ base64 (~> 0.1.1)
101
+ json (~> 2.3)
102
+ language_server-protocol (>= 3.17.0)
103
+ parallel (~> 1.10)
104
+ parser (>= 3.2.2.3)
105
+ rainbow (>= 2.2.2, < 4.0)
106
+ regexp_parser (>= 1.8, < 3.0)
107
+ rexml (>= 3.2.5, < 4.0)
108
+ rubocop-ast (>= 1.28.1, < 2.0)
109
+ ruby-progressbar (~> 1.7)
110
+ unicode-display_width (>= 2.4.0, < 3.0)
111
+ rubocop-ast (1.29.0)
112
+ parser (>= 3.2.1.0)
113
+ rubocop-capybara (2.18.0)
114
+ rubocop (~> 1.41)
115
+ rubocop-factory_bot (2.23.1)
116
+ rubocop (~> 1.33)
117
+ rubocop-performance (1.19.0)
118
+ rubocop (>= 1.7.0, < 2.0)
119
+ rubocop-ast (>= 0.4.0)
120
+ rubocop-rails (2.20.2)
121
+ activesupport (>= 4.2.0)
122
+ rack (>= 1.1)
123
+ rubocop (>= 1.33.0, < 2.0)
124
+ rubocop-rake (0.6.0)
125
+ rubocop (~> 1.0)
126
+ rubocop-rspec (2.23.2)
127
+ rubocop (~> 1.33)
128
+ rubocop-capybara (~> 2.17)
129
+ rubocop-factory_bot (~> 2.22)
130
+ rubocop-rubycw (0.1.6)
131
+ rubocop (~> 1.0)
132
+ ruby-progressbar (1.13.0)
133
+ shoulda-matchers (5.3.0)
134
+ activesupport (>= 5.2.0)
135
+ tzinfo (2.0.6)
136
+ concurrent-ruby (~> 1.0)
137
+ unicode-display_width (2.4.2)
138
+ unparser (0.6.8)
139
+ diff-lcs (~> 1.3)
140
+ parser (>= 3.2.0)
141
+ yard (0.9.34)
142
+
143
+ PLATFORMS
144
+ x86_64-linux
145
+
146
+ DEPENDENCIES
147
+ factory_bot
148
+ faker
149
+ protod!
150
+ pry-byebug
151
+ pry-doc
152
+ rails
153
+ rake
154
+ rspec
155
+ rspec-parameterized
156
+ rubocop
157
+ rubocop-performance
158
+ rubocop-rails
159
+ rubocop-rake
160
+ rubocop-rspec
161
+ rubocop-rubycw
162
+ shoulda-matchers
163
+
164
+ BUNDLED WITH
165
+ 2.4.19
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # Protod
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/protod`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add protod
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install protod
16
+
17
+ ## Usage
18
+
19
+ TODO: Write usage instructions here
20
+
21
+ ## Development
22
+
23
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
24
+
25
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
26
+
27
+ ## Contributing
28
+
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/protod. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/protod/blob/master/CODE_OF_CONDUCT.md).
30
+
31
+ ## License
32
+
33
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
34
+
35
+ ## Code of Conduct
36
+
37
+ Everyone interacting in the Protod project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/protod/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,43 @@
1
+ require 'rails'
2
+
3
+ class Protod
4
+ class GrufGenerator < Rails::Generators::Base
5
+ def create_gruf_controller
6
+ create_file "app/rpc/protod/handler_controller.rb", <<~RUBY
7
+ # frozen_string_literal: true
8
+
9
+ class Protod
10
+ class HandlerController < ::Gruf::Controllers::Base
11
+ include ::Gruf::Loggable
12
+
13
+ def self.package
14
+ @package ||= Protod::Rpc::Handler.find_package
15
+ end
16
+
17
+ bind Protod::Rpc::Handler.find_service_in(package).pb_const
18
+
19
+ def handle
20
+ return enum_for(:handle) unless block_given?
21
+
22
+ logger.info("protod/handle start")
23
+ handler = Protod::Rpc::Handler.new(self.class.package)
24
+
25
+ request.messages.each do |m|
26
+ logger.debug("protod/handle receive : #\{m\}")
27
+ yield handler.handle(m).tap { logger.debug("protod/handle send : #\{_1\}") }
28
+ end
29
+ logger.info("protod/handle finished")
30
+ rescue Protod::Rpc::Handler::InvalidArgument => e
31
+ logger.debug("protod/handle failed : #\{e.message\}")
32
+ fail!(:invalid_argument, :invalid_argument, "ERROR: #\{e.message\}")
33
+ rescue Exception => e
34
+ logger.error("protod/handle failed : #\{e.message\}\\n#\{e.backtrace.join("\\n")\}")
35
+ set_debug_info(e.message, e.backtrace[0..4])
36
+ fail!(:internal, :internal, "ERROR: #\{e.message\}")
37
+ end
38
+ end
39
+ end
40
+ RUBY
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,23 @@
1
+ require 'rails'
2
+
3
+ class Protod
4
+ class TaskGenerator < Rails::Generators::Base
5
+ def create_raketask
6
+ create_file "lib/tasks/protod.rake", <<~RUBY
7
+ # frozen_string_literal: true
8
+
9
+ begin
10
+ require 'protod/rake_task'
11
+
12
+ Protod::RakeTask::Builder.new do |builder|
13
+ # If you want to change the rake task namespace, comment in it.
14
+ # default: :protod
15
+ # builder.name = :pd
16
+ end.build
17
+ rescue LoadError => e
18
+ $stderr.puts "#\{e\}\\nYou might should remove 'lib/tasks/protod.rake'."
19
+ end
20
+ RUBY
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Protod
4
+ class Configuration
5
+ attr_accessor :proto_root_dir, :pb_root_dir
6
+
7
+ def initialize
8
+ @proto_root_dir = './proto'
9
+ @pb_root_dir = './lib'
10
+ @builders = {}
11
+ end
12
+
13
+ def setup_rbs_environment_loader(&body)
14
+ @setup_rbs_environment_loader = body
15
+ end
16
+
17
+ def define_package(ident, for_ruby: nil, for_java: nil, &body)
18
+ bkup = @current_root_package
19
+
20
+ raise NotImplementedError, "Unsupported nested package not yet!" if bkup
21
+
22
+ @current_root_package = Protod.find_or_register_package(ident, for_ruby: for_ruby, for_java: for_java)
23
+
24
+ body.call
25
+ ensure
26
+ @current_root_package = bkup
27
+ end
28
+
29
+ def derive_from(const_name, abstruct: false, &body)
30
+ raise NotImplementedError, "You need to call #define_package before" unless @current_root_package
31
+
32
+ bkup = @current_const
33
+ @current_const = bkup ? "#{bkup}::#{const_name.to_s.delete_prefix('::')}" : const_name.to_s.delete_prefix('::')
34
+
35
+ Protod::Rpc::Request::Receiver.register_for(@current_const, force: false, ignore: true)
36
+ Protod::Rpc::Response::Receiver.register_for(@current_const, force: false, ignore: true)
37
+
38
+ unless abstruct
39
+ builder = @builders[@current_root_package.full_ident] ||= Protod::Proto::Builder.new(@current_root_package)
40
+ builder.push_receiver(@current_const)
41
+ end
42
+
43
+ body.call
44
+ ensure
45
+ @current_const = bkup
46
+ end
47
+
48
+ def define_rpc(*names, singleton: false)
49
+ raise NotImplementedError, "You need to call #define_package before" unless @current_root_package
50
+ raise NotImplementedError, "You need to call #derive_from before" unless @current_const
51
+
52
+ Protod::Rpc::Request.find_by(@current_const).push_procedure(*names, singleton: singleton)
53
+ Protod::Rpc::Response.find_by(@current_const).push_procedure(*names, singleton: singleton)
54
+ end
55
+
56
+ def register_interpreter_for(*const_names, with: nil, force: true, &body)
57
+ raise NotImplementedError, "You need to call #define_package before" unless @current_root_package
58
+
59
+ Protod::Interpreter.register_for(*const_names, with: with, force: force, &body)
60
+ end
61
+
62
+ def builders
63
+ @builders.values
64
+ end
65
+
66
+ def rbs_environment
67
+ @rbs_environment ||= RBS::EnvironmentLoader.new().tap do
68
+ @setup_rbs_environment_loader&.call(_1)
69
+ end.then do
70
+ RBS::Environment.from_loader(_1).resolve_type_names
71
+ end
72
+ end
73
+
74
+ def rbs_definition_builder
75
+ @rbs_definition_builder ||= RBS::DefinitionBuilder.new(env: rbs_environment)
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,161 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Protod
4
+ class Interpreter
5
+ class ActiveRecord
6
+ def self.setup!
7
+ setup_active_model! if defined?(::ActiveModel::Attributes)
8
+ setup_active_record! if defined?(::ActiveRecord::Attributes)
9
+ end
10
+
11
+ private
12
+
13
+ def self.setup_active_model!
14
+ Interpreter.register_for('ActiveModel::Attributes', force: false, ignore: true) do
15
+ def proto_message
16
+ i = const.new
17
+
18
+ Protod::Proto::Message.new(
19
+ ident: proto_ident,
20
+ fields: const.attribute_names.map do |name|
21
+ Protod::Proto::Field.build_from(
22
+ resolve_type_from(const.attribute_types.fetch(name)) || 'RBS::Types::Bases::Any',
23
+ ident: name,
24
+ required: false,
25
+ optional: true,
26
+ repeated: i.method(name).call.is_a?(::Array) ? true : false
27
+ )
28
+ end
29
+ )
30
+ end
31
+
32
+ def resolve_type_from(attribute_type)
33
+ case attribute_type
34
+ when ::ActiveModel::Type::Boolean
35
+ 'RBS::Types::Bases::Bool'
36
+ when ::ActiveModel::Type::String, ::ActiveModel::Type::ImmutableString
37
+ 'String'
38
+ when ::ActiveModel::Type::Decimal
39
+ 'BigDecimal'
40
+ when ::ActiveModel::Type::Integer, ::ActiveModel::Type::BigInteger
41
+ 'Integer'
42
+ when ::ActiveModel::Type::Float
43
+ 'Numeric'
44
+ when ::ActiveModel::Type::Date
45
+ 'Date'
46
+ when ::ActiveModel::Type::Time, ::ActiveModel::Type::DateTime
47
+ 'Time'
48
+ when ::ActiveModel::Type::Binary
49
+ 'Protod::Types::Binary'
50
+ end
51
+ end
52
+
53
+ def to_pb_from(rb)
54
+ acceptable_names = const.attribute_names.index_with(true)
55
+
56
+ attributes = proto_message.fields.filter_map do |f|
57
+ name = f.ident
58
+
59
+ next unless acceptable_names.key?(name)
60
+
61
+ [
62
+ name.to_sym,
63
+ if f.repeated
64
+ rb.attributes.fetch(name).map { f.interpreter.to_pb_from(_1) }
65
+ else
66
+ f.interpreter.to_pb_from(rb.attributes.fetch(name))
67
+ end
68
+ ]
69
+ end.to_h
70
+
71
+ pb_const.new(**attributes)
72
+ end
73
+
74
+ def to_rb_from(pb)
75
+ acceptable_names = const.attribute_names.index_with(true)
76
+
77
+ attributes = proto_message.fields.map do |f|
78
+ name = f.ident
79
+
80
+ next unless acceptable_names.key?(name)
81
+ next if Protod::Proto.omits_field?(pb, name)
82
+
83
+ [
84
+ name.to_sym,
85
+ if f.repeated
86
+ pb.public_send(name)&.map { f.interpreter.to_rb_from(_1) }
87
+ else
88
+ f.interpreter.to_rb_from(pb.public_send(name))
89
+ end
90
+ ]
91
+ end.compact.to_h
92
+
93
+ const.new(**attributes)
94
+ end
95
+ end
96
+ end
97
+
98
+ def self.setup_active_record!
99
+ Interpreter.register_for('ActiveRecord::Attributes', with: 'ActiveModel::Attributes', force: false, ignore: true) do
100
+ def resolve_type_from(attribute_type)
101
+ case attribute_type
102
+ when ::ActiveRecord::Type::Boolean
103
+ 'RBS::Types::Bases::Bool'
104
+ when ::ActiveRecord::Type::String, ::ActiveRecord::Type::ImmutableString, ::ActiveRecord::Type::Text
105
+ 'String'
106
+ when ::ActiveRecord::Type::Decimal, ::ActiveRecord::Type::DecimalWithoutScale
107
+ 'BigDecimal'
108
+ when ::ActiveRecord::Type::UnsignedInteger
109
+ 'Protod::Types::UnsignedInteger'
110
+ when ::ActiveRecord::Type::Integer, ::ActiveRecord::Type::BigInteger
111
+ 'Integer'
112
+ when ::ActiveRecord::Type::Float
113
+ 'Numeric'
114
+ when ::ActiveRecord::Type::Date
115
+ 'Date'
116
+ when ::ActiveRecord::Type::Time, ::ActiveRecord::Type::DateTime
117
+ 'Time'
118
+ when ::ActiveRecord::Type::Json
119
+ 'Protod::Types::Json'
120
+ when ::ActiveRecord::Type::Binary
121
+ 'Protod::Types::Binary'
122
+ else
123
+ case attribute_type.type
124
+ when :boolean
125
+ 'RBS::Types::Bases::Bool'
126
+ when :string
127
+ 'String'
128
+ when :integer
129
+ 'Integer'
130
+ when :datetime
131
+ 'Time'
132
+ else
133
+ super
134
+ end
135
+ end
136
+ end
137
+ end
138
+
139
+ Interpreter.register_for('ActiveRecord::Relation', force: false, ignore: true) do
140
+ def proto_ident
141
+ Protod::Proto::Ident.build_from('ActiveRecord::Relation')
142
+ end
143
+
144
+ def proto_message
145
+ Protod::Proto::Message.new(
146
+ ident: proto_ident
147
+ )
148
+ end
149
+
150
+ def to_pb_from(rb)
151
+ pb_const.new
152
+ end
153
+
154
+ def to_rb_from(pb)
155
+ pb
156
+ end
157
+ end
158
+ end
159
+ end
160
+ end
161
+ end