rbs_protobuf 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/test.yml +42 -0
- data/.gitignore +10 -0
- data/.gitmodules +0 -0
- data/.protoc-version +1 -0
- data/.rakeTasks +7 -0
- data/.travis.yml +6 -0
- data/Gemfile +10 -0
- data/LICENSE.txt +21 -0
- data/README.md +138 -0
- data/Rakefile +43 -0
- data/bin/console +14 -0
- data/bin/protoc-gen-dumper +11 -0
- data/bin/protoc-gen-ruby +29 -0
- data/bin/rpc_server +29 -0
- data/bin/setup +19 -0
- data/example/Steepfile +9 -0
- data/example/a.proto +54 -0
- data/example/b.proto +10 -0
- data/example/protobuf_gem_example.rb +32 -0
- data/exe/protoc-gen-rbs +35 -0
- data/lib/rbs_protobuf.rb +19 -0
- data/lib/rbs_protobuf/rbs_factory.rb +149 -0
- data/lib/rbs_protobuf/translator/base.rb +100 -0
- data/lib/rbs_protobuf/translator/protobuf_gem.rb +583 -0
- data/lib/rbs_protobuf/version.rb +3 -0
- data/rbs_protobuf.gemspec +30 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6d8369a09563679a8a6c334efe056f6ef462bc1fc9bbd14af108fea7d4996cdb
|
4
|
+
data.tar.gz: 7f0e46181747c4f0ee8209496c16a29a81264ccbca39faf1803de6e2a6e3b535
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e70f6e59b9f9d3de0676dc1d6d6bf2ed7715649fe428e5edb8740247df0b6e12e21b26e2e9361443c80343563160da00841d1e90c35ae906db786aca676645d9
|
7
|
+
data.tar.gz: 901ca653bdd3c03f9f09086c194c2f60180b693d2b43830335a2d34f39053dfd1e9b974d7f598377493e8121d27cadecdc9ef959b26ca14a186633a6b4dd05b4
|
@@ -0,0 +1,42 @@
|
|
1
|
+
name: Test
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches:
|
6
|
+
- master
|
7
|
+
pull_request: {}
|
8
|
+
|
9
|
+
jobs:
|
10
|
+
test:
|
11
|
+
runs-on: "ubuntu-latest"
|
12
|
+
strategy:
|
13
|
+
matrix:
|
14
|
+
container_tag:
|
15
|
+
- 2.6-bionic
|
16
|
+
- 2.7-bionic
|
17
|
+
- 3.0-focal
|
18
|
+
protoc_version:
|
19
|
+
- 3.13.0
|
20
|
+
- 3.14.0
|
21
|
+
container:
|
22
|
+
image: rubylang/ruby:${{ matrix.container_tag }}
|
23
|
+
steps:
|
24
|
+
- name: Install protoc
|
25
|
+
run: |
|
26
|
+
cd /tmp
|
27
|
+
apt-get update
|
28
|
+
apt-get -y install curl unzip
|
29
|
+
curl -OL https://github.com/protocolbuffers/protobuf/releases/download/v${{ matrix.protoc_version }}/protoc-${{ matrix.protoc_version }}-linux-x86_64.zip
|
30
|
+
unzip protoc-${{ matrix.protoc_version }}-linux-x86_64.zip
|
31
|
+
mv bin/protoc /usr/local/bin/protoc
|
32
|
+
protoc --version
|
33
|
+
- uses: actions/checkout@v1
|
34
|
+
- name: Run test
|
35
|
+
run: |
|
36
|
+
ruby -v
|
37
|
+
gem install bundler
|
38
|
+
bin/setup
|
39
|
+
bundle exec rake
|
40
|
+
bundle exec rake install
|
41
|
+
mkdir -p tmp/out
|
42
|
+
RBS_PROTOBUF_BACKEND=protobuf protoc --rbs_out=tmp/out -Iexample example/*.proto
|
data/.gitignore
ADDED
data/.gitmodules
ADDED
File without changes
|
data/.protoc-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.9.0-square-1.0
|
data/.rakeTasks
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<Settings><!--This file was automatically generated by Ruby plugin.
|
3
|
+
You are allowed to:
|
4
|
+
1. Remove rake task
|
5
|
+
2. Add existing rake tasks
|
6
|
+
To add existing rake tasks automatically delete this file and reload the project.
|
7
|
+
--><RakeGroup description="" fullCmd="" taksId="rake"><RakeTask description="Build rbs_protobuf-0.1.0.gem into the pkg directory" fullCmd="build" taksId="build" /><RakeTask description="Remove any temporary products" fullCmd="clean" taksId="clean" /><RakeTask description="Remove any generated files" fullCmd="clobber" taksId="clobber" /><RakeTask description="Build and install rbs_protobuf-0.1.0.gem into system gems" fullCmd="install" taksId="install" /><RakeGroup description="" fullCmd="" taksId="install"><RakeTask description="Build and install rbs_protobuf-0.1.0.gem into system gems without network access" fullCmd="install:local" taksId="local" /></RakeGroup><RakeTask description="Create tag v0.1.0 and build and push rbs_protobuf-0.1.0.gem to rubygems.org" fullCmd="release[remote]" taksId="release[remote]" /><RakeTask description="Run tests" fullCmd="test" taksId="test" /><RakeTask description="" fullCmd="default" taksId="default" /><RakeTask description="" fullCmd="release" taksId="release" /><RakeGroup description="" fullCmd="" taksId="release"><RakeTask description="" fullCmd="release:guard_clean" taksId="guard_clean" /><RakeTask description="" fullCmd="release:rubygem_push" taksId="rubygem_push" /><RakeTask description="" fullCmd="release:source_control_push" taksId="source_control_push" /></RakeGroup></RakeGroup></Settings>
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Soutaro Matsumoto
|
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,138 @@
|
|
1
|
+
# rbs_protobuf
|
2
|
+
|
3
|
+
rbs_protobuf is a [RBS](https://github.com/ruby/rbs) generator for Protocol Buffer messages. It parses `.proto` files and generates RBS type signature.
|
4
|
+
|
5
|
+
It works as a `protoc` plugin and generates RBSs for `protobuf` gem. (We plan to support `google-protobuf` gem too.)
|
6
|
+
|
7
|
+
## Example
|
8
|
+
|
9
|
+
This is an example .proto file.
|
10
|
+
|
11
|
+
```proto
|
12
|
+
syntax = "proto2";
|
13
|
+
|
14
|
+
package protobuf.example;
|
15
|
+
|
16
|
+
message SearchRequest {
|
17
|
+
required string query = 1;
|
18
|
+
optional int32 page_number = 2;
|
19
|
+
optional int32 result_per_page = 3;
|
20
|
+
}
|
21
|
+
```
|
22
|
+
|
23
|
+
rbs_protobuf will generate the following RBS file including method definitions for each attribute with correct types.
|
24
|
+
|
25
|
+
```rbs
|
26
|
+
module Protobuf
|
27
|
+
module Example
|
28
|
+
class SearchRequest < ::Protobuf::Message
|
29
|
+
attr_reader query(): ::String
|
30
|
+
|
31
|
+
attr_writer query(): ::String?
|
32
|
+
|
33
|
+
attr_reader page_number(): ::Integer
|
34
|
+
|
35
|
+
attr_writer page_number(): ::Integer?
|
36
|
+
|
37
|
+
attr_reader result_per_page(): ::Integer
|
38
|
+
|
39
|
+
attr_writer result_per_page(): ::Integer?
|
40
|
+
|
41
|
+
def initialize: (?query: ::String?, ?page_number: ::Integer?, ?result_per_page: ::Integer?) -> void
|
42
|
+
|
43
|
+
def []: (:query) -> ::String
|
44
|
+
| (:page_number) -> ::Integer
|
45
|
+
| (:result_per_page) -> ::Integer
|
46
|
+
| (::Symbol) -> untyped
|
47
|
+
|
48
|
+
def []=: (:query, ::String?) -> ::String?
|
49
|
+
| (:page_number, ::Integer?) -> ::Integer?
|
50
|
+
| (:result_per_page, ::Integer?) -> ::Integer?
|
51
|
+
| (::Symbol, untyped) -> untyped
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
And you can type check your Ruby program using the classes with RBS above. 💪
|
58
|
+
|
59
|
+
## Installation
|
60
|
+
|
61
|
+
Add this line to your application's Gemfile:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
group :development do
|
65
|
+
gem 'rbs_protobuf', require: false
|
66
|
+
end
|
67
|
+
```
|
68
|
+
|
69
|
+
And then execute:
|
70
|
+
|
71
|
+
$ bundle install
|
72
|
+
|
73
|
+
Or install it yourself as:
|
74
|
+
|
75
|
+
$ gem install rbs_protobuf
|
76
|
+
|
77
|
+
## Usage
|
78
|
+
|
79
|
+
Run `protoc` with `--rbs_out` option.
|
80
|
+
|
81
|
+
$ RBS_PROTOBUF_BACKEND=protobuf protoc --rbs_out=sig/protos protos/a.proto
|
82
|
+
|
83
|
+
You may need `bundle exec protoc ...` to let bundler set up PATH.
|
84
|
+
|
85
|
+
## Type checking
|
86
|
+
|
87
|
+
To type check the output, you need to configure your tools to import [gem_rbs_collection](https://github.com/ruby/gem_rbs_collection).
|
88
|
+
|
89
|
+
$ git submodule add https://github.com/ruby/gem_rbs_collection.git vendor/rbs/gem_rbs_collection
|
90
|
+
|
91
|
+
If you want to validate your RBS files with `rbs validate`, specify `--repo` option.
|
92
|
+
|
93
|
+
$ rbs --repo=vendor/rbs/gem_rbs_collection/gems -rprotobuf validate
|
94
|
+
|
95
|
+
If you want to type check your Ruby program using Steep, add `repo_path` and `library` calls in your `Steepfile`.
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
target :app do
|
99
|
+
repo_path "vendor/rbs/gem_rbs_collection/gems"
|
100
|
+
library "protobuf"
|
101
|
+
end
|
102
|
+
```
|
103
|
+
|
104
|
+
### Options
|
105
|
+
|
106
|
+
* `RBS_PROTOBUF_BACKEND` specifies the Ruby code generator gem. Supported value is `protobuf`. (We will add `google-protobuf` for `google-protobuf` gem.)
|
107
|
+
* `PB_UPCASE_ENUMS` is for `protobuf` gem support. Specify the environment variable to make enum value constants upper case.
|
108
|
+
* `RBS_PROTOBUF_NO_NESTED_NAMESPACE` is to make the RBS declarations flat.
|
109
|
+
|
110
|
+
## Supported features
|
111
|
+
|
112
|
+
| Protocol Buffer Feature | Support for `protobuf` gem |
|
113
|
+
|-------------------------|----------------------------|
|
114
|
+
| Messages | ✓ |
|
115
|
+
| Enums | ✓ |
|
116
|
+
| Packages | ✓ |
|
117
|
+
| Nested messages | ✓ |
|
118
|
+
| Maps | ✓ |
|
119
|
+
| Extensions | ✓ |
|
120
|
+
| Services | Only generates classes |
|
121
|
+
| Oneof | No support in `protobuf` gem |
|
122
|
+
|
123
|
+
## Development
|
124
|
+
|
125
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test example:typecheck` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
126
|
+
|
127
|
+
The gem works as a plugin of `protoc` command, so `protoc` command should be available for development.
|
128
|
+
|
129
|
+
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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
130
|
+
|
131
|
+
## Contributing
|
132
|
+
|
133
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/square/rbs_protobuf.
|
134
|
+
|
135
|
+
|
136
|
+
## License
|
137
|
+
|
138
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rake/testtask"
|
3
|
+
|
4
|
+
Rake::TestTask.new(:test) do |t|
|
5
|
+
t.warning = false
|
6
|
+
t.libs << "test"
|
7
|
+
t.libs << "lib"
|
8
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
9
|
+
end
|
10
|
+
|
11
|
+
task :default => [:test, "example:typecheck"]
|
12
|
+
|
13
|
+
namespace :example do
|
14
|
+
desc "Generate Ruby code with protobuf-gem"
|
15
|
+
task :protobufgem do
|
16
|
+
sh("rm -rf example/protobuf-gem")
|
17
|
+
sh("mkdir", "-p", "example/protobuf-gem")
|
18
|
+
sh(
|
19
|
+
{ "PB_NO_TAG_WARNINGS" => "1" },
|
20
|
+
"protoc",
|
21
|
+
"--plugin=protoc-gen-ruby-protobuf=#{__dir__}/bin/protoc-gen-ruby",
|
22
|
+
"--ruby-protobuf_out=example/protobuf-gem",
|
23
|
+
"-Iexample",
|
24
|
+
"example/a.proto",
|
25
|
+
"example/b.proto"
|
26
|
+
)
|
27
|
+
sh(
|
28
|
+
{ "RBS_PROTOBUF_BACKEND" => "protobuf" },
|
29
|
+
"protoc",
|
30
|
+
"--rbs_out=example/protobuf-gem",
|
31
|
+
"-Iexample",
|
32
|
+
"example/a.proto",
|
33
|
+
"example/b.proto"
|
34
|
+
)
|
35
|
+
end
|
36
|
+
|
37
|
+
desc "Type check generated code"
|
38
|
+
task :typecheck => ["example:protobufgem"] do
|
39
|
+
Dir.chdir "example" do
|
40
|
+
sh(*%w(steep check))
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rbs_protobuf"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
@@ -0,0 +1,11 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
$LOAD_PATH << File.join(__dir__, "../lib")
|
4
|
+
|
5
|
+
require "rbs_protobuf"
|
6
|
+
|
7
|
+
input = STDIN.read()
|
8
|
+
File.write(ENV["PROTOC_DUMPER_OUT"] || "a.pb.out", input)
|
9
|
+
|
10
|
+
response = Google::Protobuf::Compiler::CodeGeneratorResponse.new
|
11
|
+
print Google::Protobuf::Compiler::CodeGeneratorResponse.encode(response)
|
data/bin/protoc-gen-ruby
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
#
|
5
|
+
# This file was generated by Bundler.
|
6
|
+
#
|
7
|
+
# The application 'protoc-gen-ruby' is installed as part of a gem, and
|
8
|
+
# this file is here to facilitate running it.
|
9
|
+
#
|
10
|
+
|
11
|
+
require "pathname"
|
12
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
13
|
+
Pathname.new(__FILE__).realpath)
|
14
|
+
|
15
|
+
bundle_binstub = File.expand_path("../bundle", __FILE__)
|
16
|
+
|
17
|
+
if File.file?(bundle_binstub)
|
18
|
+
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
|
19
|
+
load(bundle_binstub)
|
20
|
+
else
|
21
|
+
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
|
22
|
+
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
require "rubygems"
|
27
|
+
require "bundler/setup"
|
28
|
+
|
29
|
+
load Gem.bin_path("protobuf", "protoc-gen-ruby")
|
data/bin/rpc_server
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
#
|
5
|
+
# This file was generated by Bundler.
|
6
|
+
#
|
7
|
+
# The application 'rpc_server' is installed as part of a gem, and
|
8
|
+
# this file is here to facilitate running it.
|
9
|
+
#
|
10
|
+
|
11
|
+
require "pathname"
|
12
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
13
|
+
Pathname.new(__FILE__).realpath)
|
14
|
+
|
15
|
+
bundle_binstub = File.expand_path("../bundle", __FILE__)
|
16
|
+
|
17
|
+
if File.file?(bundle_binstub)
|
18
|
+
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
|
19
|
+
load(bundle_binstub)
|
20
|
+
else
|
21
|
+
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
|
22
|
+
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
require "rubygems"
|
27
|
+
require "bundler/setup"
|
28
|
+
|
29
|
+
load Gem.bin_path("protobuf", "rpc_server")
|
data/bin/setup
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
set -euo pipefail
|
3
|
+
IFS=$'\n\t'
|
4
|
+
|
5
|
+
echo ">>> Installing dependencies... 🏋️♂️"
|
6
|
+
bundle install
|
7
|
+
|
8
|
+
echo ">>> Checking if protoc is available... 👀"
|
9
|
+
protoc --version
|
10
|
+
|
11
|
+
echo ">>> Setting up gem_rbs_collection... 💎"
|
12
|
+
REPODIR=$(cd $(dirname $0)/..; pwd)/tmp/rbs/gem_rbs_collection
|
13
|
+
if [ -e $REPODIR ]; then
|
14
|
+
echo " ${REPODIR} exists"
|
15
|
+
else
|
16
|
+
echo " Cloning git repo into ${REPODIR}..."
|
17
|
+
mkdir -p $REPODIR/..
|
18
|
+
git clone https://github.com/ruby/gem_rbs_collection.git ${REPODIR}
|
19
|
+
fi
|
data/example/Steepfile
ADDED
data/example/a.proto
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
syntax = "proto2";
|
2
|
+
|
3
|
+
package rbs_protobuf.example;
|
4
|
+
|
5
|
+
message SearchRequest {
|
6
|
+
required string query = 1;
|
7
|
+
optional int32 page_number = 2;
|
8
|
+
optional int32 result_per_page = 3;
|
9
|
+
enum Corpus {
|
10
|
+
UNIVERSAL = 0;
|
11
|
+
WEB = 1;
|
12
|
+
IMAGES = 2;
|
13
|
+
LOCAL = 3;
|
14
|
+
NEWS = 4;
|
15
|
+
PRODUCTS = 5;
|
16
|
+
VIDEO = 6;
|
17
|
+
}
|
18
|
+
optional Corpus corpus = 4 [default = UNIVERSAL];
|
19
|
+
}
|
20
|
+
|
21
|
+
enum EnumAllowingAlias {
|
22
|
+
option allow_alias = true;
|
23
|
+
Unknown = 0;
|
24
|
+
Started = 1;
|
25
|
+
Running = 1;
|
26
|
+
}
|
27
|
+
|
28
|
+
message SearchResponse {
|
29
|
+
message Result {
|
30
|
+
required string url = 1;
|
31
|
+
optional string title = 2;
|
32
|
+
repeated string snippets = 3;
|
33
|
+
}
|
34
|
+
repeated Result result = 1;
|
35
|
+
}
|
36
|
+
|
37
|
+
message SampleMessage {
|
38
|
+
oneof test_oneof {
|
39
|
+
string name = 4;
|
40
|
+
SubMessage sub_message = 9;
|
41
|
+
}
|
42
|
+
}
|
43
|
+
|
44
|
+
message SubMessage {
|
45
|
+
}
|
46
|
+
|
47
|
+
message Project {
|
48
|
+
map<string, Project> projects = 3;
|
49
|
+
extensions 100 to max;
|
50
|
+
}
|
51
|
+
|
52
|
+
service SearchService {
|
53
|
+
rpc SearchUser(SearchRequest) returns (SearchResponse);
|
54
|
+
}
|