pb-serializer 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/ci.yml +32 -0
- data/.github/workflows/review.yml +33 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.rubocop.yml +15 -0
- data/.rubocop_todo.yml +46 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +90 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/pb/serializable.rb +122 -0
- data/lib/pb/serializer.rb +46 -0
- data/lib/pb/serializer/attribute.rb +56 -0
- data/lib/pb/serializer/base.rb +15 -0
- data/lib/pb/serializer/oneof.rb +15 -0
- data/lib/pb/serializer/version.rb +5 -0
- data/pb-serializer.gemspec +41 -0
- metadata +212 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f67d4a17784718471b0092c70c74d8ad19c3c7dcd7d0003268d0b52ee1cff8b5
|
4
|
+
data.tar.gz: 970526e14d3bacab347a57fb1a556ea041529cca3e2d9ad0e5aaf4482f4808bb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 922d74b4ca48c70e6fc37358a994630fceb3244acb8af39523dc60b8288a25c8f4f44f45c67e663743b01800fb8ec7a60b5f6804598d698ea42531947d5cb81d
|
7
|
+
data.tar.gz: 87fb4dec4bb9e073d6b461fd1b0d80803e8f1763a021e33746b9e789dfe71566dc9ccdf45ae9ca3834c4aeeb152b53660ee4bb4fedbd1525d4d81304b7f6c233
|
@@ -0,0 +1,32 @@
|
|
1
|
+
name: CI
|
2
|
+
|
3
|
+
on: [push]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
test:
|
7
|
+
|
8
|
+
strategy:
|
9
|
+
fail-fast: false
|
10
|
+
matrix:
|
11
|
+
os: [ ubuntu-latest, macos-latest ]
|
12
|
+
# ruby: [ 2.5, 2.6, 2.7 ] # TODO: Wait for supporting Ruby 2.7 in google-protobuf
|
13
|
+
ruby: [ 2.5, 2.6 ]
|
14
|
+
runs-on: ${{ matrix.os }}
|
15
|
+
|
16
|
+
steps:
|
17
|
+
- uses: ruby/setup-ruby@v1
|
18
|
+
with:
|
19
|
+
ruby-version: ${{ matrix.ruby }}
|
20
|
+
|
21
|
+
- run: |
|
22
|
+
sudo apt-get update
|
23
|
+
sudo apt-get install libsqlite3-dev
|
24
|
+
if: matrix.os == 'ubuntu-latest'
|
25
|
+
|
26
|
+
- run: gem install bundler -v 2.1.4
|
27
|
+
|
28
|
+
- uses: actions/checkout@v2
|
29
|
+
|
30
|
+
- run: bundle install --jobs 4 --retry 3
|
31
|
+
|
32
|
+
- run: bundle exec rspec
|
@@ -0,0 +1,33 @@
|
|
1
|
+
name: review
|
2
|
+
|
3
|
+
on: [pull_request]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
rubocop:
|
7
|
+
runs-on: ubuntu-latest
|
8
|
+
|
9
|
+
steps:
|
10
|
+
- uses: ruby/setup-ruby@v1
|
11
|
+
with:
|
12
|
+
ruby-version: 2.6
|
13
|
+
|
14
|
+
- run: |
|
15
|
+
sudo apt-get update
|
16
|
+
sudo apt-get install libsqlite3-dev
|
17
|
+
|
18
|
+
- name: Setup reviewdog
|
19
|
+
run: |
|
20
|
+
mkdir -p $HOME/bin
|
21
|
+
curl -sfL https://raw.githubusercontent.com/reviewdog/reviewdog/master/install.sh | sh -s -- -b $HOME/bin
|
22
|
+
echo ::add-path::$HOME/bin
|
23
|
+
|
24
|
+
- run: gem install bundler -v 2.1.4
|
25
|
+
|
26
|
+
- uses: actions/checkout@v2
|
27
|
+
|
28
|
+
- run: bundle install --jobs 4 --retry 3
|
29
|
+
|
30
|
+
- name: Run Rubocop with Reviewdog
|
31
|
+
run: |
|
32
|
+
bundle exec rubocop --fail-level E \
|
33
|
+
| reviewdog -f=rubocop -reporter=github-pr-review
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# This configuration was generated by
|
2
|
+
# `rubocop --auto-gen-config`
|
3
|
+
# on 2020-03-14 13:19:43 +0900 using RuboCop version 0.67.2.
|
4
|
+
# The point is for the user to remove these configuration records
|
5
|
+
# one by one as the offenses are removed from the code base.
|
6
|
+
# Note that changes in the inspected code, or installation of new
|
7
|
+
# versions of RuboCop, may require this file to be generated again.
|
8
|
+
|
9
|
+
# Offense count: 1
|
10
|
+
Metrics/AbcSize:
|
11
|
+
Max: 48
|
12
|
+
|
13
|
+
# Offense count: 1
|
14
|
+
# Configuration parameters: CountComments, ExcludedMethods.
|
15
|
+
# ExcludedMethods: refine
|
16
|
+
Metrics/BlockLength:
|
17
|
+
Max: 38
|
18
|
+
|
19
|
+
# Offense count: 1
|
20
|
+
Metrics/CyclomaticComplexity:
|
21
|
+
Max: 18
|
22
|
+
|
23
|
+
# Offense count: 1
|
24
|
+
# Configuration parameters: CountComments, ExcludedMethods.
|
25
|
+
Metrics/MethodLength:
|
26
|
+
Max: 42
|
27
|
+
|
28
|
+
# Offense count: 1
|
29
|
+
Metrics/PerceivedComplexity:
|
30
|
+
Max: 13
|
31
|
+
|
32
|
+
# Offense count: 1
|
33
|
+
# Configuration parameters: Max.
|
34
|
+
RSpec/ExampleLength:
|
35
|
+
Exclude:
|
36
|
+
- 'spec/pb/serializer_spec.rb'
|
37
|
+
|
38
|
+
# Offense count: 8
|
39
|
+
RSpec/LeakyConstantDeclaration:
|
40
|
+
Exclude:
|
41
|
+
- 'spec/pb/serializer_spec.rb'
|
42
|
+
|
43
|
+
# Offense count: 1
|
44
|
+
# Configuration parameters: AggregateFailuresByDefault.
|
45
|
+
RSpec/MultipleExpectations:
|
46
|
+
Max: 10
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at m@izum.in. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 izumin5210
|
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,90 @@
|
|
1
|
+
# Pb::Serializer
|
2
|
+
|
3
|
+
```rb
|
4
|
+
class UserSerializer < Pb::Serializer::Base
|
5
|
+
message YourApp::User
|
6
|
+
|
7
|
+
attribute :id, required: true
|
8
|
+
attribute :name, required: true
|
9
|
+
attribute :posts, required: true, serializer: PostSerializer
|
10
|
+
|
11
|
+
define_loader :posts do |users, subdeps, **|
|
12
|
+
posts = Post.where(user_id: users.map(&:id)).index_by(&:user_id)
|
13
|
+
users.each do |user|
|
14
|
+
user.posts = posts[user.id]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
dependency :posts
|
19
|
+
computed def post_count
|
20
|
+
object.posts.size
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class PostSerializer < Pb::Serializer::Base
|
25
|
+
message YourApp::Post
|
26
|
+
|
27
|
+
attribute :id, required: true
|
28
|
+
attribute :title, required: true
|
29
|
+
attribute :body, required: true
|
30
|
+
end
|
31
|
+
|
32
|
+
class UserGrpcService < YourApp::UserService::Service
|
33
|
+
# @param req [YourApp::GetUserRequest]
|
34
|
+
# @param call [GRPC::ActiveCall::SingleReqView]
|
35
|
+
# @return [YourApp::User]
|
36
|
+
def get_users(req, call)
|
37
|
+
user = User.find(id: req.user_id)
|
38
|
+
UserSerializer.serialize(user, with: req.field_mask)
|
39
|
+
end
|
40
|
+
|
41
|
+
# @param req [YourApp::ListFriendUsersRequest]
|
42
|
+
# @param call [GRPC::ActiveCall::SingleReqView]
|
43
|
+
# @return [YourApp::ListFriendUsersResponse]
|
44
|
+
def list_friend_users(req, call)
|
45
|
+
friends = User.find(current_user_id).friends
|
46
|
+
YourApp::ListFriendUsersResponse.new(
|
47
|
+
users: UserSerializer.serialize_repeated(friends, with: req.field_mask),
|
48
|
+
)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
```
|
52
|
+
|
53
|
+
|
54
|
+
## Installation
|
55
|
+
|
56
|
+
Add this line to your application's Gemfile:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
gem 'pb-serializer'
|
60
|
+
```
|
61
|
+
|
62
|
+
And then execute:
|
63
|
+
|
64
|
+
$ bundle
|
65
|
+
|
66
|
+
Or install it yourself as:
|
67
|
+
|
68
|
+
$ gem install pb-serializer
|
69
|
+
|
70
|
+
## Usage
|
71
|
+
|
72
|
+
TODO: Write usage instructions here
|
73
|
+
|
74
|
+
## Development
|
75
|
+
|
76
|
+
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.
|
77
|
+
|
78
|
+
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).
|
79
|
+
|
80
|
+
## Contributing
|
81
|
+
|
82
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/izumin5210/pb-serializer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
83
|
+
|
84
|
+
## License
|
85
|
+
|
86
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
87
|
+
|
88
|
+
## Code of Conduct
|
89
|
+
|
90
|
+
Everyone interacting in the Pb::Serializer project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/izumin5210/pb-serializer/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "pb/serializer"
|
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__)
|
data/bin/setup
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
module Pb
|
2
|
+
module Serializable
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
base.include ComputedModel
|
6
|
+
end
|
7
|
+
|
8
|
+
def to_pb(with: nil)
|
9
|
+
if with.nil?
|
10
|
+
with = ::Pb::Serializer.build_default_mask(self.class.message_class.descriptor)
|
11
|
+
end
|
12
|
+
|
13
|
+
self.class.bulk_load_and_compute(Array(self), with)
|
14
|
+
|
15
|
+
oneof_set = []
|
16
|
+
|
17
|
+
o = self.class.message_class.new
|
18
|
+
self.class.message_class.descriptor.each do |fd|
|
19
|
+
attr = self.class.find_attribute_by_field_descriptor(fd)
|
20
|
+
|
21
|
+
next unless attr # TODO
|
22
|
+
|
23
|
+
raise "#{self.name}.#{attr.name} is not defined" unless respond_to?(attr.name)
|
24
|
+
|
25
|
+
v = public_send(attr.name)
|
26
|
+
v = attr.convert_to_pb(v)
|
27
|
+
|
28
|
+
if attr.required && attr.field_descriptor.default == v
|
29
|
+
raise ::Pb::Serializer::ValidationError, "#{object.class.name}##{attr.name} is required"
|
30
|
+
end
|
31
|
+
|
32
|
+
next if v.nil?
|
33
|
+
|
34
|
+
if attr.oneof?
|
35
|
+
if oneof_set.include?(attr.oneof)
|
36
|
+
raise ::Pb::Serializer::ConflictOneofError, "#{object.class.name}##{attr.name} is oneof attribute"
|
37
|
+
end
|
38
|
+
oneof_set << attr.oneof
|
39
|
+
end
|
40
|
+
|
41
|
+
if attr.repeated?
|
42
|
+
o.public_send(attr.name).push(*v)
|
43
|
+
else
|
44
|
+
o.public_send("#{attr.name}=", v)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
self.class.oneofs.each do |oneof|
|
49
|
+
next if oneof_set.include?(oneof.name)
|
50
|
+
next unless oneof.required?
|
51
|
+
raise ::Pb::Serializer::ValidationError, "#{object.class.name}##{oneof.name} is required"
|
52
|
+
end
|
53
|
+
|
54
|
+
o
|
55
|
+
end
|
56
|
+
|
57
|
+
module ClassMethods
|
58
|
+
attr_reader :message_class
|
59
|
+
def message(klass)
|
60
|
+
@message_class = klass
|
61
|
+
end
|
62
|
+
|
63
|
+
# @param name [Symbol] An attribute name
|
64
|
+
# @param required [Boolean] Set true if this attribute should not zero-value
|
65
|
+
# @param serializer [Class] A serializer class for this attribute
|
66
|
+
def attribute(name, required: false, serializer: nil)
|
67
|
+
fd = message_class.descriptor.find { |fd| fd.name.to_sym == name }
|
68
|
+
raise ::Pb::Serializer::UnknownFieldError, "#{name} is not defined in #{message_class.name}" unless fd
|
69
|
+
|
70
|
+
attr = ::Pb::Serializer::Attribute.new(
|
71
|
+
name: name,
|
72
|
+
required: required,
|
73
|
+
serializer_class: serializer,
|
74
|
+
field_descriptor: fd,
|
75
|
+
oneof: @current_oneof&.name,
|
76
|
+
)
|
77
|
+
|
78
|
+
@attr_by_name ||= {}
|
79
|
+
@attr_by_name[name] = attr
|
80
|
+
|
81
|
+
define_method attr.name do
|
82
|
+
object.public_send(attr.name) # FIXME: This does not work without ::Pb::Serializer::Base
|
83
|
+
end
|
84
|
+
|
85
|
+
dependency # FIXME
|
86
|
+
computed attr.name
|
87
|
+
end
|
88
|
+
|
89
|
+
# @param object [Object, Array]
|
90
|
+
# @param message_class [Class]
|
91
|
+
def serialize(object, with: nil)
|
92
|
+
if self < ::Pb::Serializer::Base
|
93
|
+
new(object).to_pb
|
94
|
+
else
|
95
|
+
object.to_pb
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def oneof(name, required: true)
|
100
|
+
@oneof_by_name ||= {}
|
101
|
+
@current_oneof = ::Pb::Serializer::Oneof.new(
|
102
|
+
name: name,
|
103
|
+
required: required,
|
104
|
+
attributes: [],
|
105
|
+
)
|
106
|
+
yield
|
107
|
+
@oneof_by_name[name] = @current_oneof
|
108
|
+
@current_oneof = nil
|
109
|
+
end
|
110
|
+
|
111
|
+
# @param fd [Google::Protobuf::FieldDescriptor] a field descriptor
|
112
|
+
# @return [Pb::Serializer::Attribute, nil]
|
113
|
+
def find_attribute_by_field_descriptor(fd)
|
114
|
+
@attr_by_name[fd.name.to_sym]
|
115
|
+
end
|
116
|
+
|
117
|
+
def oneofs
|
118
|
+
@oneof_by_name&.values || []
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require "pb/serializer/version"
|
2
|
+
require "the_pb"
|
3
|
+
require "computed_model"
|
4
|
+
|
5
|
+
require "pb/serializable"
|
6
|
+
require "pb/serializer/base"
|
7
|
+
require "pb/serializer/attribute"
|
8
|
+
require "pb/serializer/oneof"
|
9
|
+
|
10
|
+
module Pb
|
11
|
+
module Serializer
|
12
|
+
class Error < StandardError; end
|
13
|
+
class UnknownFieldError < Error; end
|
14
|
+
class ValidationError < Error; end
|
15
|
+
class ConflictOneofError < Error; end
|
16
|
+
|
17
|
+
class << self
|
18
|
+
# @param [Google::Protobuf::Descriptor]
|
19
|
+
def build_default_mask(descriptor)
|
20
|
+
set =
|
21
|
+
descriptor.each_with_object(Set[]) do |fd, m|
|
22
|
+
case fd.type
|
23
|
+
when :message
|
24
|
+
case fd.submsg_name
|
25
|
+
when "google.protobuf.Timestamp",
|
26
|
+
"google.protobuf.StringValue",
|
27
|
+
"google.protobuf.Int32Value" ,
|
28
|
+
"google.protobuf.Int64Value" ,
|
29
|
+
"google.protobuf.UInt32Value",
|
30
|
+
"google.protobuf.UInt64Value",
|
31
|
+
"google.protobuf.FloatValue" ,
|
32
|
+
"google.protobuf.DoubleValue",
|
33
|
+
"google.protobuf.BoolValue" ,
|
34
|
+
"google.protobuf.BytesValue" then m << fd.name.to_sym
|
35
|
+
else
|
36
|
+
m << { fd.name.to_sym => build_default_mask(fd.subtype) }
|
37
|
+
end
|
38
|
+
else
|
39
|
+
m << fd.name.to_sym
|
40
|
+
end
|
41
|
+
end
|
42
|
+
set.to_a
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module Pb
|
2
|
+
module Serializer
|
3
|
+
class Attribute < Struct.new(
|
4
|
+
:name,
|
5
|
+
:required,
|
6
|
+
:serializer_class,
|
7
|
+
:field_descriptor,
|
8
|
+
:oneof,
|
9
|
+
keyword_init: true,
|
10
|
+
)
|
11
|
+
|
12
|
+
# @return [Boolean]
|
13
|
+
def required?
|
14
|
+
required
|
15
|
+
end
|
16
|
+
|
17
|
+
# @return [Boolean]
|
18
|
+
def repeated?
|
19
|
+
field_descriptor.label == :repeated
|
20
|
+
end
|
21
|
+
|
22
|
+
def oneof?
|
23
|
+
!oneof.nil?
|
24
|
+
end
|
25
|
+
|
26
|
+
# @param v [Object]
|
27
|
+
def convert_to_pb(v, should_repeat: repeated?)
|
28
|
+
return v.map { |i| convert_to_pb(i, should_repeat: false) } if should_repeat
|
29
|
+
|
30
|
+
case field_descriptor.type
|
31
|
+
when :message
|
32
|
+
case field_descriptor.submsg_name
|
33
|
+
when "google.protobuf.Timestamp" then Pb.to_timestamp(v)
|
34
|
+
when "google.protobuf.StringValue" then Pb.to_strval(v)
|
35
|
+
when "google.protobuf.Int32Value" then Pb.to_int32val(v)
|
36
|
+
when "google.protobuf.Int64Value" then Pb.to_int64val(v)
|
37
|
+
when "google.protobuf.UInt32Value" then Pb.to_uint32val(v)
|
38
|
+
when "google.protobuf.UInt64Value" then Pb.to_uint64val(v)
|
39
|
+
when "google.protobuf.FloatValue" then Pb.to_floatval(v)
|
40
|
+
when "google.protobuf.DoubleValue" then Pb.to_doubleval(v)
|
41
|
+
when "google.protobuf.BoolValue" then Pb.to_boolval(v)
|
42
|
+
when "google.protobuf.BytesValue" then Pb.to_bytesval(v)
|
43
|
+
else
|
44
|
+
return nil if v.nil?
|
45
|
+
return serializer_class.serialize(v) if serializer_class
|
46
|
+
return v.to_pb if v.kind_of?(::Pb::Serializable)
|
47
|
+
|
48
|
+
raise "serializer was not found for #{field_descriptor.submsg_name}"
|
49
|
+
end
|
50
|
+
else
|
51
|
+
v.nil? ? field_descriptor.default : v
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
lib = File.expand_path("lib", __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "pb/serializer/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "pb-serializer"
|
7
|
+
spec.version = Pb::Serializer::VERSION
|
8
|
+
spec.authors = ["izumin5210"]
|
9
|
+
spec.email = ["m@izum.in"]
|
10
|
+
|
11
|
+
spec.summary = "Serialize objects into Protocol Buffers messages"
|
12
|
+
spec.description = spec.summary
|
13
|
+
spec.homepage = "https://github.com/izumin5210/pb-serializer"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
17
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
18
|
+
spec.metadata["changelog_uri"] = spec.homepage
|
19
|
+
|
20
|
+
# Specify which files should be added to the gem when it is released.
|
21
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
23
|
+
`git ls-files -z`.split("\x0").reject {|f| f.match(%r{^(test|spec|features)/}) }
|
24
|
+
end
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) {|f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
|
29
|
+
rails_versions = [">= 5.2", "< 6.1"]
|
30
|
+
spec.add_runtime_dependency "google-protobuf", "~> 3.0"
|
31
|
+
spec.add_runtime_dependency "the_pb", "~> 0.0.1"
|
32
|
+
spec.add_runtime_dependency "computed_model", "~> 0.1.0"
|
33
|
+
|
34
|
+
spec.add_development_dependency "activerecord", rails_versions
|
35
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
36
|
+
spec.add_development_dependency "onkcop", "~> 0.53"
|
37
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
38
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
39
|
+
spec.add_development_dependency "rubocop", "0.67.2" # for onkcop
|
40
|
+
spec.add_development_dependency "sqlite3", "~> 1.4"
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,212 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pb-serializer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- izumin5210
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-03-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: google-protobuf
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: the_pb
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.0.1
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.0.1
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: computed_model
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.1.0
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.1.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activerecord
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.2'
|
62
|
+
- - "<"
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '6.1'
|
65
|
+
type: :development
|
66
|
+
prerelease: false
|
67
|
+
version_requirements: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '5.2'
|
72
|
+
- - "<"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '6.1'
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: bundler
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - "~>"
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '2.0'
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - "~>"
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '2.0'
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: onkcop
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - "~>"
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0.53'
|
96
|
+
type: :development
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - "~>"
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0.53'
|
103
|
+
- !ruby/object:Gem::Dependency
|
104
|
+
name: rake
|
105
|
+
requirement: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - "~>"
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '10.0'
|
110
|
+
type: :development
|
111
|
+
prerelease: false
|
112
|
+
version_requirements: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - "~>"
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '10.0'
|
117
|
+
- !ruby/object:Gem::Dependency
|
118
|
+
name: rspec
|
119
|
+
requirement: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - "~>"
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '3.0'
|
124
|
+
type: :development
|
125
|
+
prerelease: false
|
126
|
+
version_requirements: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - "~>"
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '3.0'
|
131
|
+
- !ruby/object:Gem::Dependency
|
132
|
+
name: rubocop
|
133
|
+
requirement: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - '='
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: 0.67.2
|
138
|
+
type: :development
|
139
|
+
prerelease: false
|
140
|
+
version_requirements: !ruby/object:Gem::Requirement
|
141
|
+
requirements:
|
142
|
+
- - '='
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: 0.67.2
|
145
|
+
- !ruby/object:Gem::Dependency
|
146
|
+
name: sqlite3
|
147
|
+
requirement: !ruby/object:Gem::Requirement
|
148
|
+
requirements:
|
149
|
+
- - "~>"
|
150
|
+
- !ruby/object:Gem::Version
|
151
|
+
version: '1.4'
|
152
|
+
type: :development
|
153
|
+
prerelease: false
|
154
|
+
version_requirements: !ruby/object:Gem::Requirement
|
155
|
+
requirements:
|
156
|
+
- - "~>"
|
157
|
+
- !ruby/object:Gem::Version
|
158
|
+
version: '1.4'
|
159
|
+
description: Serialize objects into Protocol Buffers messages
|
160
|
+
email:
|
161
|
+
- m@izum.in
|
162
|
+
executables: []
|
163
|
+
extensions: []
|
164
|
+
extra_rdoc_files: []
|
165
|
+
files:
|
166
|
+
- ".github/workflows/ci.yml"
|
167
|
+
- ".github/workflows/review.yml"
|
168
|
+
- ".gitignore"
|
169
|
+
- ".rspec"
|
170
|
+
- ".rubocop.yml"
|
171
|
+
- ".rubocop_todo.yml"
|
172
|
+
- CODE_OF_CONDUCT.md
|
173
|
+
- Gemfile
|
174
|
+
- LICENSE.txt
|
175
|
+
- README.md
|
176
|
+
- Rakefile
|
177
|
+
- bin/console
|
178
|
+
- bin/setup
|
179
|
+
- lib/pb/serializable.rb
|
180
|
+
- lib/pb/serializer.rb
|
181
|
+
- lib/pb/serializer/attribute.rb
|
182
|
+
- lib/pb/serializer/base.rb
|
183
|
+
- lib/pb/serializer/oneof.rb
|
184
|
+
- lib/pb/serializer/version.rb
|
185
|
+
- pb-serializer.gemspec
|
186
|
+
homepage: https://github.com/izumin5210/pb-serializer
|
187
|
+
licenses:
|
188
|
+
- MIT
|
189
|
+
metadata:
|
190
|
+
homepage_uri: https://github.com/izumin5210/pb-serializer
|
191
|
+
source_code_uri: https://github.com/izumin5210/pb-serializer
|
192
|
+
changelog_uri: https://github.com/izumin5210/pb-serializer
|
193
|
+
post_install_message:
|
194
|
+
rdoc_options: []
|
195
|
+
require_paths:
|
196
|
+
- lib
|
197
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
198
|
+
requirements:
|
199
|
+
- - ">="
|
200
|
+
- !ruby/object:Gem::Version
|
201
|
+
version: '0'
|
202
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
203
|
+
requirements:
|
204
|
+
- - ">="
|
205
|
+
- !ruby/object:Gem::Version
|
206
|
+
version: '0'
|
207
|
+
requirements: []
|
208
|
+
rubygems_version: 3.0.3
|
209
|
+
signing_key:
|
210
|
+
specification_version: 4
|
211
|
+
summary: Serialize objects into Protocol Buffers messages
|
212
|
+
test_files: []
|