proto_dsl 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/.rspec +3 -0
- data/.rubocop.yml +22 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +58 -0
- data/README.md +90 -0
- data/Rakefile +12 -0
- data/lib/proto_dsl/dsl.rb +103 -0
- data/lib/proto_dsl/enum.rb +49 -0
- data/lib/proto_dsl/message.rb +90 -0
- data/lib/proto_dsl/oneof.rb +52 -0
- data/lib/proto_dsl/rpc.rb +44 -0
- data/lib/proto_dsl/service.rb +30 -0
- data/lib/proto_dsl/version.rb +5 -0
- data/lib/proto_dsl.rb +7 -0
- data/proto_dsl.gemspec +38 -0
- metadata +66 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 65e4ad31fcc61b8b2d14157ef69c9e66580d5bcfbee0772f6c2c629fc3c8ba03
|
4
|
+
data.tar.gz: bba38d481018d03f0d51a7bddce5ac542e14c8f81deaff3b7754214824e62b2e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0ec9c704050355c71734a7496ff6843442906c7d4e13cf3e1dbc8ce8b7af31e4d8b4a21a17419e52d05d4e6bde6bd30a8f35970ec639fc850299c0379244f00d
|
7
|
+
data.tar.gz: ad6281d9fa7036f23665b74d68a4825b4b35f7b9d27ab167eed9695588943e067b5f7dfdfb4b7565d17e59129b806ca979d54243156c1e6275bb26a1e04acb8a
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.6
|
3
|
+
|
4
|
+
Style/StringLiterals:
|
5
|
+
Enabled: true
|
6
|
+
EnforcedStyle: double_quotes
|
7
|
+
|
8
|
+
Style/StringLiteralsInInterpolation:
|
9
|
+
Enabled: true
|
10
|
+
EnforcedStyle: double_quotes
|
11
|
+
|
12
|
+
Layout/LineLength:
|
13
|
+
Max: 400
|
14
|
+
|
15
|
+
Lint/DuplicateMethods:
|
16
|
+
Enabled: false
|
17
|
+
|
18
|
+
Metrics/AbcSize:
|
19
|
+
Enabled: false
|
20
|
+
|
21
|
+
Metrics/MethodLength:
|
22
|
+
Max: 20
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at kerandisylvance@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
proto_dsl (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.2)
|
10
|
+
diff-lcs (1.5.0)
|
11
|
+
json (2.6.3)
|
12
|
+
parallel (1.22.1)
|
13
|
+
parser (3.1.3.0)
|
14
|
+
ast (~> 2.4.1)
|
15
|
+
rainbow (3.1.1)
|
16
|
+
rake (13.0.6)
|
17
|
+
regexp_parser (2.6.1)
|
18
|
+
rexml (3.2.5)
|
19
|
+
rspec (3.12.0)
|
20
|
+
rspec-core (~> 3.12.0)
|
21
|
+
rspec-expectations (~> 3.12.0)
|
22
|
+
rspec-mocks (~> 3.12.0)
|
23
|
+
rspec-core (3.12.0)
|
24
|
+
rspec-support (~> 3.12.0)
|
25
|
+
rspec-expectations (3.12.0)
|
26
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
27
|
+
rspec-support (~> 3.12.0)
|
28
|
+
rspec-mocks (3.12.0)
|
29
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
30
|
+
rspec-support (~> 3.12.0)
|
31
|
+
rspec-support (3.12.0)
|
32
|
+
rubocop (1.39.0)
|
33
|
+
json (~> 2.3)
|
34
|
+
parallel (~> 1.10)
|
35
|
+
parser (>= 3.1.2.1)
|
36
|
+
rainbow (>= 2.2.2, < 4.0)
|
37
|
+
regexp_parser (>= 1.8, < 3.0)
|
38
|
+
rexml (>= 3.2.5, < 4.0)
|
39
|
+
rubocop-ast (>= 1.23.0, < 2.0)
|
40
|
+
ruby-progressbar (~> 1.7)
|
41
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
42
|
+
rubocop-ast (1.24.0)
|
43
|
+
parser (>= 3.1.1.0)
|
44
|
+
ruby-progressbar (1.11.0)
|
45
|
+
unicode-display_width (2.3.0)
|
46
|
+
|
47
|
+
PLATFORMS
|
48
|
+
arm64-darwin-20
|
49
|
+
x86_64-linux
|
50
|
+
|
51
|
+
DEPENDENCIES
|
52
|
+
proto_dsl!
|
53
|
+
rake (~> 13.0)
|
54
|
+
rspec (~> 3.0)
|
55
|
+
rubocop (~> 1.21)
|
56
|
+
|
57
|
+
BUNDLED WITH
|
58
|
+
2.3.15
|
data/README.md
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# ProtoDsl
|
2
|
+
|
3
|
+
The `ProtoDsl::DSL` is a Domain Specific Language (DSL) for defining Protocol Buffer message types. It provides a simple, readable syntax for defining Protocol Buffer message types using Ruby code. The `to_proto` method can be used to convert the defined message types into the Protocol Buffer syntax.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Install the gem and add to the application's Gemfile by executing:
|
8
|
+
|
9
|
+
$ bundle add proto_dsl
|
10
|
+
|
11
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
12
|
+
|
13
|
+
$ gem install proto_dsl
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
This gem defines a `Protocol Buffer` message type using the `ProtoDsl::DSL` class. It specifies the syntax version, package name, and import paths. It also sets some options and defines a service with two RPCs. The code also defines several message types, an enum, and a oneof field. The to_proto method at the end of the code converts the defined message types into the Protocol Buffer syntax.
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
# frozen_string_literal: true
|
21
|
+
|
22
|
+
require 'proto_dsl'
|
23
|
+
|
24
|
+
ProtoDsl::Dsl.new.tap do |dsl|
|
25
|
+
dsl.syntax "proto2"
|
26
|
+
dsl.package "my_package"
|
27
|
+
dsl.import "google/protobuf/wrappers.proto"
|
28
|
+
|
29
|
+
dsl.option "java_package", "com.example.app"
|
30
|
+
dsl.option "java_outer_classname", "MyProto"
|
31
|
+
|
32
|
+
dsl.service "MyService" do
|
33
|
+
rpc "GetData", "GetDataRequest", "GetDataResponse"
|
34
|
+
rpc "UpdateData", "UpdateDataRequest", "UpdateDataResponse" do
|
35
|
+
option "method", "POST"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
dsl.message "GetDataRequest" do
|
40
|
+
field "string", "id", 1
|
41
|
+
field "int32", "version", 2
|
42
|
+
|
43
|
+
message "Filter" do
|
44
|
+
field "string", "field", 1
|
45
|
+
field "string", "value", 2
|
46
|
+
end
|
47
|
+
|
48
|
+
field "repeated", "filters", 3, type: "Filter"
|
49
|
+
|
50
|
+
oneof "params" do
|
51
|
+
option "deprecated", true
|
52
|
+
field "string", "id", 1
|
53
|
+
field "int32", "version", 2
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
dsl.message "GetDataResponse" do
|
58
|
+
field "string", "data", 1
|
59
|
+
end
|
60
|
+
|
61
|
+
dsl.message "UpdateDataRequest" do
|
62
|
+
field "string", "id", 1
|
63
|
+
field "int32", "version", 2
|
64
|
+
field "string", "data", 3
|
65
|
+
end
|
66
|
+
|
67
|
+
dsl.message "UpdateDataResponse" do
|
68
|
+
field "bool", "success", 1
|
69
|
+
end
|
70
|
+
|
71
|
+
dsl.enum "Status" do
|
72
|
+
value "OK", 0
|
73
|
+
value "ERROR", 1
|
74
|
+
end
|
75
|
+
end.to_proto
|
76
|
+
```
|
77
|
+
|
78
|
+
## Development
|
79
|
+
|
80
|
+
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.
|
81
|
+
|
82
|
+
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).
|
83
|
+
|
84
|
+
## Contributing
|
85
|
+
|
86
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Sylvance/proto_dsl. 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/Sylvance/proto_dsl/blob/main/CODE_OF_CONDUCT.md).
|
87
|
+
|
88
|
+
## Code of Conduct
|
89
|
+
|
90
|
+
Everyone interacting in the ProtoDsl project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/Sylvance/proto_dsl/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "./message"
|
4
|
+
require_relative "./enum"
|
5
|
+
require_relative "./service"
|
6
|
+
|
7
|
+
module ProtoDsl
|
8
|
+
# Dsl class
|
9
|
+
class Dsl
|
10
|
+
attr_reader :services, :messages, :enums, :options, :imports, :syntax, :package
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
@services = []
|
14
|
+
@messages = []
|
15
|
+
@enums = []
|
16
|
+
@options = []
|
17
|
+
@imports = []
|
18
|
+
@syntax = "proto2"
|
19
|
+
@package = ""
|
20
|
+
end
|
21
|
+
|
22
|
+
def service(name, &block)
|
23
|
+
service = Service.new(name)
|
24
|
+
service.instance_eval(&block)
|
25
|
+
@services << service
|
26
|
+
end
|
27
|
+
|
28
|
+
def message(name, &block)
|
29
|
+
message = Message.new(name)
|
30
|
+
message.instance_eval(&block)
|
31
|
+
@messages << message
|
32
|
+
end
|
33
|
+
|
34
|
+
def enum(name, &block)
|
35
|
+
enum = Enum.new(name)
|
36
|
+
enum.instance_eval(&block)
|
37
|
+
@enums << enum
|
38
|
+
end
|
39
|
+
|
40
|
+
def syntax(version)
|
41
|
+
@syntax = version
|
42
|
+
end
|
43
|
+
|
44
|
+
def package(name)
|
45
|
+
@package = name
|
46
|
+
end
|
47
|
+
|
48
|
+
def option(name, value)
|
49
|
+
@options << [name, value]
|
50
|
+
end
|
51
|
+
|
52
|
+
def import(path)
|
53
|
+
@imports << path
|
54
|
+
end
|
55
|
+
|
56
|
+
def to_proto
|
57
|
+
output = +""
|
58
|
+
output << "syntax = \"#{@syntax}\";\n"
|
59
|
+
output << "package #{@package};\n" unless @package.empty?
|
60
|
+
@imports.each do |path|
|
61
|
+
output << "import \"#{path}\";\n"
|
62
|
+
end
|
63
|
+
@options.each do |option|
|
64
|
+
output << "option #{option[0]} = #{option[1]};\n"
|
65
|
+
end
|
66
|
+
@services.each do |service|
|
67
|
+
output << service.to_proto
|
68
|
+
end
|
69
|
+
@messages.each do |message|
|
70
|
+
output << message.to_proto
|
71
|
+
end
|
72
|
+
@enums.each do |enum|
|
73
|
+
output << enum.to_proto
|
74
|
+
end
|
75
|
+
output
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# This is the main ProtoDsl::Dsl class, which defines a domain-specific language (DSL) for defining Protocol Buffer (protobuf) messages, services, and enums. Protocol Buffers are a language-neutral, platform-neutral, extensible way of serializing structured data for use in communications protocols, data storage, and more.
|
81
|
+
|
82
|
+
# The ProtoDsl::Dsl class has several instance attributes that store the protobuf definitions that are defined using the DSL:
|
83
|
+
|
84
|
+
# -> services: an array of ProtoDsl::Service objects representing the protobuf services that have been defined
|
85
|
+
# -> messages: an array of ProtoDsl::Message objects representing the protobuf messages that have been defined
|
86
|
+
# -> enums: an array of ProtoDsl::Enum objects representing the protobuf enums that have been defined
|
87
|
+
# -> options: an array of [name, value] pairs representing the protobuf options that have been defined
|
88
|
+
# -> imports: an array of strings representing the paths of the protobuf files that have been imported
|
89
|
+
# -> syntax: a string representing the protobuf syntax version (either "proto2" or "proto3")
|
90
|
+
# -> package: a string representing the protobuf package name
|
91
|
+
|
92
|
+
# The ProtoDsl::Dsl class has several instance methods that can be used to define protobuf services, messages, enums, options, and imports, as well as to set the protobuf syntax and package. These methods are:
|
93
|
+
|
94
|
+
# -> service: defines a new protobuf service with the given name, and evaluates the given block in the context of a new ProtoDsl::Service instance
|
95
|
+
# -> message: defines a new protobuf message with the given name, and evaluates the given block in the context of a new ProtoDsl::Message instance
|
96
|
+
# -> enum: defines a new protobuf enum with the given name, and evaluates the given block in the context of a new ProtoDsl::Enum instance
|
97
|
+
# -> syntax: sets the protobuf syntax version to the given value (either "proto2" or "proto3")
|
98
|
+
# -> package: sets the protobuf package name to the given value
|
99
|
+
# -> option: defines a new protobuf option with the given name and value
|
100
|
+
# -> import: imports the protobuf file at the given path
|
101
|
+
# -> to_proto: generates the protobuf code for all of the protobuf definitions that have been defined using the DSL, and returns the generated code as a string.
|
102
|
+
|
103
|
+
# The ProtoDsl::Dsl class depends on the ProtoDsl::Message, ProtoDsl::Enum, and ProtoDsl::Service classes, which define the individual protobuf definitions that can be created using the DSL. These classes are defined in separate files that are required by the ProtoDsl::Dsl class.
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ProtoDsl
|
4
|
+
# Enum class
|
5
|
+
class Enum
|
6
|
+
attr_reader :name, :values
|
7
|
+
|
8
|
+
def initialize(name)
|
9
|
+
@name = name
|
10
|
+
@values = []
|
11
|
+
end
|
12
|
+
|
13
|
+
def value(name, tag)
|
14
|
+
@values << [name, tag]
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_proto
|
18
|
+
output = "enum #{@name} {\n"
|
19
|
+
@values.each do |value|
|
20
|
+
output << " #{value[0]} = #{value[1]};\n"
|
21
|
+
end
|
22
|
+
output << "}\n"
|
23
|
+
output
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# This is the ProtoDsl::Enum class, which defines a protobuf enum. A protobuf enum is a type that consists of a set of named constants, and is often used to define a set of possible values for a field in a protobuf message.
|
29
|
+
|
30
|
+
# The ProtoDsl::Enum class is typically used by the ProtoDsl::Dsl class to define protobuf enums.
|
31
|
+
|
32
|
+
# The ProtoDsl::Enum class has two instance attributes:
|
33
|
+
|
34
|
+
# -> name: a string representing the name of the enum
|
35
|
+
# -> values: an array of [name, tag] pairs representing the values in the enum, where name is the name of the value, and tag is its numeric tag
|
36
|
+
|
37
|
+
# The ProtoDsl::Enum class has two instance methods:
|
38
|
+
|
39
|
+
# -> value: defines a new value in the enum with the given name and tag
|
40
|
+
# -> to_proto: generates the protobuf code for the enum, and returns the generated code as a string.
|
41
|
+
|
42
|
+
# The to_proto method generates the protobuf code for the enum by concatenating the name of the enum, the individual values in the values array as protobuf code, and a closing } character. For example, given an enum named MyEnum with values MY_VALUE (tag 1) and MY_OTHER_VALUE (tag 2), the to_proto method would generate the following protobuf code:
|
43
|
+
|
44
|
+
# ```proto
|
45
|
+
# enum MyEnum {
|
46
|
+
# MY_VALUE = 1;
|
47
|
+
# MY_OTHER_VALUE = 2;
|
48
|
+
# }
|
49
|
+
# ```
|
@@ -0,0 +1,90 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "./enum"
|
4
|
+
require_relative "./oneof"
|
5
|
+
|
6
|
+
module ProtoDsl
|
7
|
+
# Message class
|
8
|
+
class Message
|
9
|
+
attr_reader :name, :fields, :messages, :enums, :oneofs
|
10
|
+
|
11
|
+
def initialize(name)
|
12
|
+
@name = name
|
13
|
+
@fields = []
|
14
|
+
@messages = []
|
15
|
+
@enums = []
|
16
|
+
@oneofs = []
|
17
|
+
end
|
18
|
+
|
19
|
+
def field(type, name, tag)
|
20
|
+
@fields << [type, name, tag]
|
21
|
+
end
|
22
|
+
|
23
|
+
def message(name, &block)
|
24
|
+
message = Message.new(name)
|
25
|
+
message.instance_eval(&block)
|
26
|
+
@messages << message
|
27
|
+
end
|
28
|
+
|
29
|
+
def enum(name, &block)
|
30
|
+
enum = Enum.new(name)
|
31
|
+
enum.instance_eval(&block)
|
32
|
+
@enums << enum
|
33
|
+
end
|
34
|
+
|
35
|
+
def oneof(name, &block)
|
36
|
+
oneof = Oneof.new(name)
|
37
|
+
oneof.instance_eval(&block)
|
38
|
+
@oneofs << oneof
|
39
|
+
end
|
40
|
+
|
41
|
+
def to_proto
|
42
|
+
output = "message #{@name} {\n"
|
43
|
+
@fields.each do |field|
|
44
|
+
output << " #{field[0]} #{field[1]} = #{field[2]};\n"
|
45
|
+
end
|
46
|
+
@messages.each do |message|
|
47
|
+
output << message.to_proto
|
48
|
+
end
|
49
|
+
@enums.each do |enum|
|
50
|
+
output << enum.to_proto
|
51
|
+
end
|
52
|
+
@oneofs.each do |oneof|
|
53
|
+
output << oneof.to_proto
|
54
|
+
end
|
55
|
+
output << "}\n"
|
56
|
+
output
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# This is the ProtoDsl::Message class, which defines a protobuf message. A protobuf message is a type that consists of a set of named fields, and is used to define the structure of data in a protobuf service.
|
62
|
+
|
63
|
+
# The ProtoDsl::Message class has five instance attributes:
|
64
|
+
|
65
|
+
# -> name: a string representing the name of the message
|
66
|
+
# -> fields: an array of [type, name, tag] tuples representing the fields in the message, where type is the type of the field, name is the name of the field, and tag is its numeric tag
|
67
|
+
# -> messages: an array of nested ProtoDsl::Message instances
|
68
|
+
# -> enums: an array of nested ProtoDsl::Enum instances
|
69
|
+
# -> oneofs: an array of nested ProtoDsl::Oneof instances
|
70
|
+
|
71
|
+
# The ProtoDsl::Message class has four instance methods:
|
72
|
+
|
73
|
+
# -> field: defines a new field in the message with the given type, name, and tag
|
74
|
+
# -> message: defines a nested message within the current message
|
75
|
+
# -> enum: defines a nested enum within the current message
|
76
|
+
# -> oneof: defines a oneof group within the current message
|
77
|
+
# -> to_proto: generates the protobuf code for the message, and returns the generated code as a string.
|
78
|
+
|
79
|
+
# The to_proto method generates the protobuf code for the message by concatenating the name of the message, the individual fields in the fields array as protobuf code, the nested messages, enums, and oneofs as protobuf code, and a closing } character.
|
80
|
+
# For example, given a message named MyMessage with fields my_field (type string, tag 1) and my_other_field (type int32, tag 2), the to_proto method would generate the following protobuf code:
|
81
|
+
|
82
|
+
# ```proto
|
83
|
+
# message MyMessage {
|
84
|
+
# string my_field = 1;
|
85
|
+
# int32 my_other_field = 2;
|
86
|
+
# }
|
87
|
+
# ```
|
88
|
+
|
89
|
+
# The ProtoDsl::Message class is typically used by the ProtoDsl::Dsl class to define protobuf messages.
|
90
|
+
# The ProtoDsl::Dsl class uses the message method to define a new ProtoDsl::Message instance, and then uses the methods in the ProtoDsl::Message class to define the fields, messages, enums, and oneofs
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ProtoDsl
|
4
|
+
# Oneof class
|
5
|
+
class Oneof
|
6
|
+
attr_reader :name, :fields
|
7
|
+
|
8
|
+
def initialize(name)
|
9
|
+
@name = name
|
10
|
+
@fields = []
|
11
|
+
end
|
12
|
+
|
13
|
+
def field(type, name, tag)
|
14
|
+
@fields << [type, name, tag]
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_proto
|
18
|
+
output = " oneof #{@name} {\n"
|
19
|
+
@fields.each do |field|
|
20
|
+
output << " #{field[0]} #{field[1]} = #{field[2]};\n"
|
21
|
+
end
|
22
|
+
output << " }\n"
|
23
|
+
output
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# This is the ProtoDsl::Oneof class, which defines a protobuf oneof group. A protobuf oneof group is a set of fields within a message, where only one field can be set at a time.
|
29
|
+
|
30
|
+
# The ProtoDsl::Oneof class has two instance attributes:
|
31
|
+
|
32
|
+
# -> name: a string representing the name of the oneof group
|
33
|
+
# -> fields: an array of [type, name, tag] tuples representing the fields in the oneof group, where type is the type of the field, name is the name of the field, and tag is its numeric tag
|
34
|
+
|
35
|
+
# The ProtoDsl::Oneof class has two instance methods:
|
36
|
+
|
37
|
+
# -> field: defines a new field in the oneof group with the given type, name, and tag
|
38
|
+
# -> to_proto: generates the protobuf code for the oneof group, and returns the generated code as a string.
|
39
|
+
|
40
|
+
# The to_proto method generates the protobuf code for the oneof group by concatenating the name of the oneof group, the individual fields in the fields array as protobuf code, and a closing }
|
41
|
+
# character. For example, given a oneof group named MyOneof with fields my_field (type string, tag 1) and my_other_field (type int32, tag 2), the to_proto method would generate the following protobuf code:
|
42
|
+
|
43
|
+
# ```proto
|
44
|
+
# oneof MyOneof {
|
45
|
+
# string my_field = 1;
|
46
|
+
# int32 my_other_field = 2;
|
47
|
+
# }
|
48
|
+
# ```
|
49
|
+
|
50
|
+
# The ProtoDsl::Oneof class is typically used by the ProtoDsl::Message class to define oneof groups within messages.
|
51
|
+
# The ProtoDsl::Message class uses the oneof method to define a new ProtoDsl::Oneof instance, and then uses the field method in the ProtoDsl::Oneof class to define the fields within the oneof group.
|
52
|
+
# The ProtoDsl::Message class then uses the to_proto method in the ProtoDsl::Oneof class to generate the protobuf code for the oneof group and include it in the generated code for the message.
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ProtoDsl
|
4
|
+
# Rpc class
|
5
|
+
class Rpc
|
6
|
+
attr_reader :name, :request_type, :response_type, :options
|
7
|
+
|
8
|
+
def initialize(name, request_type, response_type)
|
9
|
+
@name = name
|
10
|
+
@request_type = request_type
|
11
|
+
@response_type = response_type
|
12
|
+
@options = []
|
13
|
+
end
|
14
|
+
|
15
|
+
def option(name, value)
|
16
|
+
@options << [name, value]
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_proto
|
20
|
+
output = " rpc #{@name} (#{@request_type}) returns (#{@response_type}) {\n"
|
21
|
+
@options.each do |option|
|
22
|
+
output << " option #{option[0]} = #{option[1]};\n"
|
23
|
+
end
|
24
|
+
output << " }\n"
|
25
|
+
output
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# This is the ProtoDsl::Rpc class, which defines a protobuf RPC (remote procedure call) service. A protobuf RPC service allows clients to call methods on the server by sending a request message and receiving a response message.
|
31
|
+
|
32
|
+
# The ProtoDsl::Rpc class has four instance attributes:
|
33
|
+
|
34
|
+
# -> name: a string representing the name of the RPC service
|
35
|
+
# -> request_type: a string representing the name of the request message type
|
36
|
+
# -> response_type: a string representing the name of the response message type
|
37
|
+
# -> options: an array of [name, value] tuples representing the options for the RPC service, where name is the name of the option and value is the option's value
|
38
|
+
|
39
|
+
# The ProtoDsl::Rpc class has two instance methods:
|
40
|
+
|
41
|
+
# -> option: defines a new option for the RPC service with the given name and value
|
42
|
+
# -> to_proto: generates the protobuf code for the RPC service, and returns the generated code as a string.
|
43
|
+
|
44
|
+
# The to_proto method generates the protobuf code for the RPC service by concatenating the name, request_type, and response_type of the RPC service, the options in the options array as protobuf code, and a closing } character.
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "./rpc"
|
4
|
+
|
5
|
+
module ProtoDsl
|
6
|
+
# Service class
|
7
|
+
class Service
|
8
|
+
attr_reader :name, :rpcs
|
9
|
+
|
10
|
+
def initialize(name)
|
11
|
+
@name = name
|
12
|
+
@rpcs = []
|
13
|
+
end
|
14
|
+
|
15
|
+
def rpc(name, request_type, response_type, &block)
|
16
|
+
rpc = Rpc.new(name, request_type, response_type)
|
17
|
+
rpc.instance_eval(&block) if block_given?
|
18
|
+
@rpcs << rpc
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_proto
|
22
|
+
output = "service #{@name} {\n"
|
23
|
+
@rpcs.each do |rpc|
|
24
|
+
output << rpc.to_proto
|
25
|
+
end
|
26
|
+
output << "}\n"
|
27
|
+
output
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/lib/proto_dsl.rb
ADDED
data/proto_dsl.gemspec
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/proto_dsl/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "proto_dsl"
|
7
|
+
spec.version = ProtoDsl::VERSION
|
8
|
+
spec.authors = ["Sylvance"]
|
9
|
+
spec.email = ["kerandisylvance@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "The `ProtoDsl::DSL` is a Domain Specific Language (DSL) for defining Protocol Buffer message types"
|
12
|
+
spec.description = "It provides a simple, readable syntax for defining Protocol Buffer message types using Ruby code. The `to_proto` method can be used to convert the defined message types into the Protocol Buffer syntax."
|
13
|
+
spec.homepage = "https://github.com/Sylvance/proto_dsl"
|
14
|
+
spec.required_ruby_version = ">= 2.6.0"
|
15
|
+
|
16
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
17
|
+
|
18
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
19
|
+
spec.metadata["source_code_uri"] = "https://github.com/Sylvance/proto_dsl"
|
20
|
+
spec.metadata["changelog_uri"] = "https://github.com/Sylvance/proto_dsl/blob/main/CHANGELOG.md"
|
21
|
+
|
22
|
+
# Specify which files should be added to the gem when it is released.
|
23
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
24
|
+
spec.files = Dir.chdir(__dir__) do
|
25
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
26
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
27
|
+
end
|
28
|
+
end
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
# Uncomment to register a new dependency of your gem
|
34
|
+
# spec.add_dependency "example-gem", "~> 1.0"
|
35
|
+
|
36
|
+
# For more information and examples about making a new gem, check out our
|
37
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: proto_dsl
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sylvance
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-12-06 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: It provides a simple, readable syntax for defining Protocol Buffer message
|
14
|
+
types using Ruby code. The `to_proto` method can be used to convert the defined
|
15
|
+
message types into the Protocol Buffer syntax.
|
16
|
+
email:
|
17
|
+
- kerandisylvance@gmail.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- ".rspec"
|
23
|
+
- ".rubocop.yml"
|
24
|
+
- CHANGELOG.md
|
25
|
+
- CODE_OF_CONDUCT.md
|
26
|
+
- Gemfile
|
27
|
+
- Gemfile.lock
|
28
|
+
- README.md
|
29
|
+
- Rakefile
|
30
|
+
- lib/proto_dsl.rb
|
31
|
+
- lib/proto_dsl/dsl.rb
|
32
|
+
- lib/proto_dsl/enum.rb
|
33
|
+
- lib/proto_dsl/message.rb
|
34
|
+
- lib/proto_dsl/oneof.rb
|
35
|
+
- lib/proto_dsl/rpc.rb
|
36
|
+
- lib/proto_dsl/service.rb
|
37
|
+
- lib/proto_dsl/version.rb
|
38
|
+
- proto_dsl.gemspec
|
39
|
+
homepage: https://github.com/Sylvance/proto_dsl
|
40
|
+
licenses: []
|
41
|
+
metadata:
|
42
|
+
allowed_push_host: https://rubygems.org
|
43
|
+
homepage_uri: https://github.com/Sylvance/proto_dsl
|
44
|
+
source_code_uri: https://github.com/Sylvance/proto_dsl
|
45
|
+
changelog_uri: https://github.com/Sylvance/proto_dsl/blob/main/CHANGELOG.md
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 2.6.0
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
requirements: []
|
61
|
+
rubygems_version: 3.3.7
|
62
|
+
signing_key:
|
63
|
+
specification_version: 4
|
64
|
+
summary: The `ProtoDsl::DSL` is a Domain Specific Language (DSL) for defining Protocol
|
65
|
+
Buffer message types
|
66
|
+
test_files: []
|