pb-serializer 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d1f988a05d9e793ea7b9f8e7ce7424f3f0d2f7252c536322c881d7b7921f5c1d
4
- data.tar.gz: 9136b5e10daa4852db5ade8435d128851e4e1a636eb78fbe612ebca55d47de2a
3
+ metadata.gz: 0edb07851fb2e4ee393ca69ffbcb56ae012cf5e0dc4d55874b3a9364b9dff3d0
4
+ data.tar.gz: efdfa935a65fd014dc5e757d92f6e3d4162e45c4352aa4047e360bb0ca1b86c8
5
5
  SHA512:
6
- metadata.gz: 959064c2628dcc51320b6c1fc4e82dffce9f60529740853333198b6a0b9c66db8acfacb5476a9898362a7f7a8fae1e8aa1261ffb32d1d33bd383749645337470
7
- data.tar.gz: c8f0c9d172cebdbe9f40afca16ec79a62a2c4da32c12821e3420fe36747c6e7a6192d012a029e91427af6cdfb4da65e4f9e82212537e67beefdf8c17bbe04326
6
+ metadata.gz: 67a0ca4baa1bbf484a2c0b1b310a5299b0dd35864d87f9aef458f81d0c56e986be80ae2f4a15eb76be2c2f3befc74445674b0b9fc2b14a6b92feeb1438a4f954
7
+ data.tar.gz: 0043f9be4bf0a60631463179902b73493e7623a1f59b7d13f32e81f825767f6a0127b0ac4e2d7f3fc80f3b8b0f617c988bce778e3a28653b4e2bf98a8fdd508e
@@ -9,8 +9,10 @@ jobs:
9
9
  fail-fast: false
10
10
  matrix:
11
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 ]
12
+ # TODO: Wait for supporting Ruby 3.0 in simplecov-cobertura.
13
+ # See https://github.com/dashingrocket/simplecov-cobertura/pull/16
14
+ # ruby: [ 2.5, 2.6, 2.7, 3.0 ]
15
+ ruby: [ 2.5, 2.6, 2.7 ]
14
16
  runs-on: ${{ matrix.os }}
15
17
 
16
18
  steps:
@@ -15,11 +15,9 @@ jobs:
15
15
  sudo apt-get update
16
16
  sudo apt-get install libsqlite3-dev
17
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
18
+ - uses: reviewdog/action-setup@v1
19
+ with:
20
+ reviewdog_version: latest
23
21
 
24
22
  - run: gem install bundler -v 2.1.4
25
23
 
@@ -28,6 +26,8 @@ jobs:
28
26
  - run: bundle install --jobs 4 --retry 3
29
27
 
30
28
  - name: Run Rubocop with Reviewdog
29
+ env:
30
+ REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
31
31
  run: |
32
32
  bundle exec rubocop --fail-level E \
33
33
  | reviewdog -f=rubocop -reporter=github-pr-review
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  ## Unreleased
2
2
 
3
+ ## 0.4.0
4
+
5
+ - Make `#initialize` extensible used with `define_primary_loader` https://github.com/wantedly/pb-serializer/pull/31
6
+ - Supoprt `ignore` directive https://github.com/wantedly/pb-serializer/pull/36
7
+ - Support field mask https://github.com/wantedly/pb-serializer/pull/34
8
+
3
9
  ## 0.3.0
4
10
 
5
11
  - Support `if` option https://github.com/wantedly/pb-serializer/pull/24
@@ -6,8 +6,15 @@ module Pb
6
6
  base.singleton_class.prepend Hook
7
7
  end
8
8
 
9
+ # @param with [
10
+ # Google::Protobuf::FieldMask,
11
+ # Array<(Symbol, Hash)>,
12
+ # Hash{Symbol=>(Array,Symbol,Hash)},
13
+ # Pb::Serializer::NormalizedMask
14
+ # ]
9
15
  def to_pb(with: nil)
10
- # TODO: apply masks
16
+ with ||= ::Pb::Serializer.build_default_mask(self.class.message_class.descriptor)
17
+ with = ::Pb::Serializer::NormalizedMask.build(with)
11
18
 
12
19
  oneof_set = []
13
20
 
@@ -26,12 +33,13 @@ module Pb
26
33
  next
27
34
  end
28
35
 
36
+ next unless with.key?(attr.name)
29
37
  next unless attr.serializable?(self)
30
38
 
31
39
  raise "#{self.name}.#{attr.name} is not defined" unless respond_to?(attr.name)
32
40
 
33
41
  v = public_send(attr.name)
34
- v = attr.convert_to_pb(v)
42
+ v = attr.convert_to_pb(v, with: with[attr.name])
35
43
 
36
44
  if attr.oneof?
37
45
  if !v.nil?
@@ -128,7 +136,14 @@ module Pb
128
136
  end
129
137
  end
130
138
 
131
- # @param with [Array, Google::Protobuf::FieldMask, nil]
139
+ # @param names [Array<Symbol>] Attribute names to be ignored
140
+ def ignore(*names)
141
+ names.each do |name|
142
+ attribute name, ignore: true
143
+ end
144
+ end
145
+
146
+ # @param with [Array, Hash, Google::Protobuf::FieldMask, nil]
132
147
  # @return [Array]
133
148
  def bulk_load_and_serialize(with: nil, **args)
134
149
  bulk_load(with: with, **args).map { |s| s.to_pb(with: with) }
@@ -136,6 +151,7 @@ module Pb
136
151
 
137
152
  def bulk_load(with: nil, **args)
138
153
  with ||= ::Pb::Serializer.build_default_mask(message_class.descriptor)
154
+ with = ::Pb::Serializer::NormalizedMask.build(with)
139
155
  with = with.reject { |c| (__pb_serializer_attrs & (c.kind_of?(Hash) ? c.keys : [c])).empty? }
140
156
 
141
157
  bulk_load_and_compute(with, **args)
data/lib/pb/serializer.rb CHANGED
@@ -1,8 +1,10 @@
1
1
  require "pb/serializer/version"
2
2
  require "the_pb"
3
3
  require "computed_model"
4
+ require "google/protobuf/field_mask_pb"
4
5
 
5
6
  require "pb/serializable"
7
+ require "pb/serializer/normalized_mask"
6
8
  require "pb/serializer/base"
7
9
  require "pb/serializer/attribute"
8
10
  require "pb/serializer/oneof"
@@ -8,7 +8,7 @@ module Pb
8
8
  keyword_init: true,
9
9
  )
10
10
 
11
- ALLOWED_OPTIONS = Set[:allow_nil, :if, :serializer].freeze
11
+ ALLOWED_OPTIONS = Set[:allow_nil, :if, :serializer, :ignore].freeze
12
12
 
13
13
  def initialize(options:, **)
14
14
  super
@@ -36,6 +36,8 @@ module Pb
36
36
 
37
37
  # @return [Boolean]
38
38
  def serializable?(s)
39
+ return false if options[:ignore]
40
+
39
41
  cond = options[:if]
40
42
 
41
43
  return true unless cond
@@ -52,9 +54,10 @@ module Pb
52
54
  end
53
55
 
54
56
  # @param v [Object]
55
- def convert_to_pb(v, should_repeat: repeated?)
57
+ # @param with [Pb::Serializer::NormalizedMask]
58
+ def convert_to_pb(v, with: nil, should_repeat: repeated?)
56
59
  return nil if v.nil?
57
- return v.map { |i| convert_to_pb(i, should_repeat: false) } if should_repeat
60
+ return v.map { |i| convert_to_pb(i, should_repeat: false, with: with) } if should_repeat
58
61
 
59
62
  case field_descriptor.type
60
63
  when :message
@@ -74,8 +77,8 @@ module Pb
74
77
  when "google.protobuf.BoolValue" then Pb.to_boolval(v)
75
78
  when "google.protobuf.BytesValue" then Pb.to_bytesval(v)
76
79
  else
77
- return serializer_class.new(v).to_pb if serializer_class
78
- return v.to_pb if v.kind_of?(::Pb::Serializable)
80
+ return serializer_class.new(v).to_pb(with: with) if serializer_class
81
+ return v.to_pb(with: with) if v.kind_of?(::Pb::Serializable)
79
82
 
80
83
  raise "serializer was not found for #{field_descriptor.submsg_name}"
81
84
  end
@@ -15,10 +15,14 @@ module Pb
15
15
  module Hook
16
16
  def define_primary_loader(name, &block)
17
17
  class_eval <<~RUBY
18
- def initialize(*args)
19
- super
20
- @#{name} = object
18
+ module PbSerializerDefinePrimaryLoaderPrependMethods
19
+ def initialize(*args)
20
+ super
21
+ @#{name} = object
22
+ end
21
23
  end
24
+
25
+ prepend PbSerializerDefinePrimaryLoaderPrependMethods
22
26
  RUBY
23
27
 
24
28
  super
@@ -0,0 +1,57 @@
1
+ module Pb::Serializer
2
+ class NormalizedMask < ::Hash
3
+ class << self
4
+ # @param [Google::Protobuf::FieldMask, Symbol, Array<(Symbol,Hash)>, Hash{Symbol=>(Array,Symbol,Hash)}]
5
+ # @return [Hash{Symbol=>Hash}]
6
+ def build(input)
7
+ return input if input.kind_of? self
8
+
9
+ normalized = new
10
+
11
+ case input
12
+ when Google::Protobuf::FieldMask
13
+ normalized = normalize_mask_paths(input.paths)
14
+ when Array
15
+ input.each do |v|
16
+ deep_merge!(normalized, build(v))
17
+ end
18
+ when Hash
19
+ input.each do |k, v|
20
+ normalized[k] ||= new
21
+ deep_merge!(normalized[k], build(v))
22
+ end
23
+ when Symbol
24
+ normalized[input] ||= new
25
+ else
26
+ raise "not supported field mask type: #{input.class}"
27
+ end
28
+
29
+ normalized
30
+ end
31
+
32
+ private
33
+
34
+ # @param [Array<String>]
35
+ # @return [Hash{Symbol=>Hash}]
36
+ def normalize_mask_paths(paths)
37
+ paths_by_key = {}
38
+
39
+ paths.each do |path|
40
+ key, rest = path.split('.', 2)
41
+ paths_by_key[key.to_sym] ||= []
42
+ paths_by_key[key.to_sym].push(rest) if rest && !rest.empty?
43
+ end
44
+
45
+ paths_by_key.keys.each_with_object(new) do |key, normalized|
46
+ normalized[key] = normalize_mask_paths(paths_by_key[key])
47
+ end
48
+ end
49
+
50
+ def deep_merge!(h1, h2)
51
+ h1.merge!(h2) do |_k, v1, v2|
52
+ deep_merge!(v1, v2)
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -1,5 +1,5 @@
1
1
  module Pb
2
2
  module Serializer
3
- VERSION = "0.3.0".freeze
3
+ VERSION = "0.4.0".freeze
4
4
  end
5
5
  end
@@ -29,7 +29,7 @@ Gem::Specification.new do |spec|
29
29
  rails_versions = [">= 5.2", "< 6.1"]
30
30
  spec.add_runtime_dependency "google-protobuf", "~> 3.0"
31
31
  spec.add_runtime_dependency "the_pb", "~> 0.0.1"
32
- spec.add_runtime_dependency "computed_model", "~> 0.2.1"
32
+ spec.add_runtime_dependency "computed_model", "~> 0.2.2"
33
33
 
34
34
  spec.add_development_dependency "activerecord", rails_versions
35
35
  spec.add_development_dependency "bundler", "~> 2.0"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pb-serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - izumin5210
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-04-03 00:00:00.000000000 Z
11
+ date: 2021-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: google-protobuf
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 0.2.1
47
+ version: 0.2.2
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 0.2.1
54
+ version: 0.2.2
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: activerecord
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -210,6 +210,7 @@ files:
210
210
  - lib/pb/serializer.rb
211
211
  - lib/pb/serializer/attribute.rb
212
212
  - lib/pb/serializer/base.rb
213
+ - lib/pb/serializer/normalized_mask.rb
213
214
  - lib/pb/serializer/oneof.rb
214
215
  - lib/pb/serializer/version.rb
215
216
  - pb-serializer.gemspec
@@ -220,7 +221,7 @@ metadata:
220
221
  homepage_uri: https://github.com/izumin5210/pb-serializer
221
222
  source_code_uri: https://github.com/izumin5210/pb-serializer
222
223
  changelog_uri: https://github.com/izumin5210/pb-serializer
223
- post_install_message:
224
+ post_install_message:
224
225
  rdoc_options: []
225
226
  require_paths:
226
227
  - lib
@@ -235,8 +236,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
235
236
  - !ruby/object:Gem::Version
236
237
  version: '0'
237
238
  requirements: []
238
- rubygems_version: 3.1.2
239
- signing_key:
239
+ rubygems_version: 3.2.3
240
+ signing_key:
240
241
  specification_version: 4
241
242
  summary: Serialize objects into Protocol Buffers messages
242
243
  test_files: []