pb-serializer 0.2.0 → 0.2.1

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: 3d65ab2cd7830751ed61e5cd72eca1d688fac910b544167994780d065e55fa3b
4
- data.tar.gz: e991534beffa6fb92b4c23f05bf59b5ba0ef6872cd34dc801148e08ec3f2c075
3
+ metadata.gz: ed65391a95c2c1d8ebdcd7977c8c8992428c062df0f78c1ba7fbf047737e98e1
4
+ data.tar.gz: 1c6db98b94bde534ebbe2999509791b0e2354b15d5ee5bdde17035081e4bf3e2
5
5
  SHA512:
6
- metadata.gz: 0a138f78df718b226cbf65cef851380a941c34306ed80bdc2fb65093f0dec804515d0187d3f0b11e4fcd36bbc339dfca08f2beef49989d6417ccf157ddf9da97
7
- data.tar.gz: bf343a1d3f7f055370f652616fc6914dc682f2fdc6840c6975614537d5ecf02241974b8df986505f96a517401b676e0756aa2040d181898e8dc866ee76ab305c
6
+ metadata.gz: 7989d54a7f8f699f97387b9dee18ae93e93eb80c745af81802454d34c7b3c262c8b849248ec979a7a1e15e30a1a0a5beabfad816dbd78852198a3d09ce8a0652
7
+ data.tar.gz: b9f1e003472b813316869a643ab0214f59eedf1431a998204a57622dffad4287161ff8c57f746d4106b6263ff5cc366c181e948c7ce787de48c98d390ff4c7df
data/CHANGELOG.md ADDED
@@ -0,0 +1,19 @@
1
+ ## Unreleased
2
+
3
+ ## 0.2.1
4
+
5
+ - **BREAKING CHANGE** `required` -> `allow_nil` https://github.com/wantedly/pb-serializer/pull/21
6
+ - Make Serializer's constructors extensible https://github.com/wantedly/pb-serializer/pull/22
7
+
8
+ ## 0.2.0
9
+
10
+ - **BREAKING CHANGE** https://github.com/wantedly/pb-serializer/pull/17
11
+ - Support loading and serializing arrays
12
+ - Bump `computed_model` from 0.1.0 to 0.2.1
13
+ - Change API
14
+ - Add example specs https://github.com/wantedly/pb-serializer/pull/18
15
+
16
+
17
+ ## 0.1.0
18
+
19
+ Initial release.
data/README.md CHANGED
@@ -4,9 +4,9 @@
4
4
  class UserSerializer < Pb::Serializer::Base
5
5
  message YourApp::User
6
6
 
7
- attribute :id, required: true
8
- attribute :name, required: true
9
- attribute :posts, required: true
7
+ attribute :id
8
+ attribute :name
9
+ attribute :posts
10
10
 
11
11
  define_primary_loader :user do |subdeps, ids:, **|
12
12
  User.where(id: ids).preload(subdeps).map { |u| new(u) }
@@ -29,9 +29,9 @@ class PostSerializer < Pb::Serializer::Base
29
29
  Post.where(user_id: user_ids).preload(subdeps).map { |p| new(p) }
30
30
  end
31
31
 
32
- attribute :id, required: true
33
- attribute :title, required: true
34
- attribute :body, required: true
32
+ attribute :id
33
+ attribute :title
34
+ attribute :body
35
35
  end
36
36
 
37
37
  class UserGrpcService < YourApp::UserService::Service
@@ -22,19 +22,19 @@ module Pb
22
22
  v = public_send(attr.name)
23
23
  v = attr.convert_to_pb(v)
24
24
 
25
- if attr.required && attr.field_descriptor.default == v
25
+ if attr.oneof?
26
+ if !v.nil?
27
+ if oneof_set.include?(attr.oneof)
28
+ raise ::Pb::Serializer::ConflictOneofError, "#{primary_object.class.name}##{attr.name} is oneof attribute"
29
+ end
30
+ oneof_set << attr.oneof
31
+ end
32
+ elsif !attr.allow_nil? && v.nil?
26
33
  raise ::Pb::Serializer::ValidationError, "#{primary_object.class.name}##{attr.name} is required"
27
34
  end
28
35
 
29
36
  next if v.nil?
30
37
 
31
- if attr.oneof?
32
- if oneof_set.include?(attr.oneof)
33
- raise ::Pb::Serializer::ConflictOneofError, "#{primary_object.class.name}##{attr.name} is oneof attribute"
34
- end
35
- oneof_set << attr.oneof
36
- end
37
-
38
38
  if attr.repeated?
39
39
  o.public_send(attr.name).push(*v)
40
40
  else
@@ -44,7 +44,7 @@ module Pb
44
44
 
45
45
  self.class.oneofs.each do |oneof|
46
46
  next if oneof_set.include?(oneof.name)
47
- next unless oneof.required?
47
+ next if oneof.allow_nil?
48
48
  raise ::Pb::Serializer::ValidationError, "#{primary_object.class.name}##{oneof.name} is required"
49
49
  end
50
50
 
@@ -91,15 +91,15 @@ module Pb
91
91
  end
92
92
 
93
93
  # @param name [Symbol] An attribute name
94
- # @param required [Boolean] Set true if this attribute should not zero-value
94
+ # @param allow_nil [Boolean] Set true if this attribute allow to be nil
95
95
  # @param serializer [Class] A serializer class for this attribute
96
- def attribute(name, required: false, serializer: nil)
96
+ def attribute(name, allow_nil: false, serializer: nil)
97
97
  fd = message_class.descriptor.find { |fd| fd.name.to_sym == name }
98
98
  raise ::Pb::Serializer::UnknownFieldError, "#{name} is not defined in #{message_class.name}" unless fd
99
99
 
100
100
  attr = ::Pb::Serializer::Attribute.new(
101
101
  name: name,
102
- required: required,
102
+ allow_nil: allow_nil,
103
103
  serializer_class: serializer,
104
104
  field_descriptor: fd,
105
105
  oneof: @current_oneof&.name,
@@ -126,11 +126,11 @@ module Pb
126
126
  bulk_load_and_compute(with, **args)
127
127
  end
128
128
 
129
- def oneof(name, required: true)
129
+ def oneof(name, allow_nil: false)
130
130
  @oneof_by_name ||= {}
131
131
  @current_oneof = ::Pb::Serializer::Oneof.new(
132
132
  name: name,
133
- required: required,
133
+ allow_nil: allow_nil,
134
134
  attributes: [],
135
135
  )
136
136
  yield
@@ -2,7 +2,7 @@ module Pb
2
2
  module Serializer
3
3
  class Attribute < Struct.new(
4
4
  :name,
5
- :required,
5
+ :allow_nil,
6
6
  :serializer_class,
7
7
  :field_descriptor,
8
8
  :oneof,
@@ -10,8 +10,8 @@ module Pb
10
10
  )
11
11
 
12
12
  # @return [Boolean]
13
- def required?
14
- required
13
+ def allow_nil?
14
+ allow_nil
15
15
  end
16
16
 
17
17
  # @return [Boolean]
@@ -25,6 +25,7 @@ module Pb
25
25
 
26
26
  # @param v [Object]
27
27
  def convert_to_pb(v, should_repeat: repeated?)
28
+ return nil if v.nil?
28
29
  return v.map { |i| convert_to_pb(i, should_repeat: false) } if should_repeat
29
30
 
30
31
  case field_descriptor.type
@@ -41,7 +42,6 @@ module Pb
41
42
  when "google.protobuf.BoolValue" then Pb.to_boolval(v)
42
43
  when "google.protobuf.BytesValue" then Pb.to_bytesval(v)
43
44
  else
44
- return nil if v.nil?
45
45
  return serializer_class.new(v).to_pb if serializer_class
46
46
  return v.to_pb if v.kind_of?(::Pb::Serializable)
47
47
 
@@ -8,14 +8,15 @@ module Pb
8
8
 
9
9
  attr_reader :object
10
10
 
11
- def initialize(object)
11
+ def initialize(object, *)
12
12
  @object = object
13
13
  end
14
14
 
15
15
  module Hook
16
16
  def define_primary_loader(name, &block)
17
17
  class_eval <<~RUBY
18
- def initialize(object)
18
+ def initialize(*args)
19
+ super
19
20
  @#{name} = object
20
21
  end
21
22
  RUBY
@@ -2,13 +2,13 @@ module Pb
2
2
  module Serializer
3
3
  class Oneof < Struct.new(
4
4
  :name,
5
- :required,
5
+ :allow_nil,
6
6
  :attributes,
7
7
  keyword_init: true,
8
8
  )
9
9
  # @return [Boolean]
10
- def required?
11
- required
10
+ def allow_nil?
11
+ allow_nil
12
12
  end
13
13
  end
14
14
  end
@@ -1,5 +1,5 @@
1
1
  module Pb
2
2
  module Serializer
3
- VERSION = "0.2.0".freeze
3
+ VERSION = "0.2.1".freeze
4
4
  end
5
5
  end
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.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - izumin5210
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-03-29 00:00:00.000000000 Z
11
+ date: 2020-03-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: google-protobuf
@@ -169,6 +169,7 @@ files:
169
169
  - ".rspec"
170
170
  - ".rubocop.yml"
171
171
  - ".rubocop_todo.yml"
172
+ - CHANGELOG.md
172
173
  - CODE_OF_CONDUCT.md
173
174
  - Gemfile
174
175
  - LICENSE.txt