gapic-common 0.18.0 → 0.19.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: 609c468f1e8a7e60f8b4ba911901dd99f8816cb367371dd5479d79dcd06bca82
4
- data.tar.gz: 23a01faa58af17577e262852d434a6746b33f090247b05ea9dc8ce2ae07772bb
3
+ metadata.gz: 9f42f8b965f312b70a2746fa345c8a546ecfe319c5189ed19654145b7206b66d
4
+ data.tar.gz: a97b42149a8fb5f6797cb608716b8b639bf1cd04d657e8fdf83ab455ae6ca147
5
5
  SHA512:
6
- metadata.gz: 7fc2a7f8c0fdcc79434966a1e6431c29d957dd1a646cae5a8cd023da1320b7f5253b69f4edd915c98656b12e681aac66bf11713c2bb47f0482798601635ba948
7
- data.tar.gz: 49a6c6aaa7fca24979d8707bc7a6ba2b8daed51951539d2235be32f932e73a02fa4cd8d7699f811b358bd216ca99ffbe63dab0469196b1815cdf37eb6ad4055c
6
+ metadata.gz: '02587d4f734c88f473265b1ef1d8fce02086be8c270fc679846697860bb37e4bf43a8cf8ed2126df64641040f0123741b965162dab2e889f0223955372c94b52'
7
+ data.tar.gz: 55105ce9ce9b47684ce10fe0bdeba37ec0caeba7949767d5829225fb0993266a52e0975db50fcac8a8f82df8cd04309709be45699e748b6f74175adf0f19aaeb
data/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Release History
2
2
 
3
+ ### 0.19.1 (2023-05-30)
4
+
5
+ #### Bug Fixes
6
+
7
+ * Fixed handling of optional fields in coerce ([#954](https://github.com/googleapis/gapic-generator-ruby/issues/954))
8
+
9
+ ### 0.19.0 (2023-05-26)
10
+
11
+ #### Features
12
+
13
+ * Compatibility with protobuf v23 generated map fields ([#948](https://github.com/googleapis/gapic-generator-ruby/issues/948))
14
+
3
15
  ### 0.18.0 (2023-02-27)
4
16
 
5
17
  #### Features
@@ -14,6 +14,6 @@
14
14
 
15
15
  module Gapic
16
16
  module Common
17
- VERSION = "0.18.0".freeze
17
+ VERSION = "0.19.1".freeze
18
18
  end
19
19
  end
@@ -16,7 +16,8 @@ require "google/protobuf/timestamp_pb"
16
16
 
17
17
  module Gapic
18
18
  ##
19
- # TODO: Describe Protobuf
19
+ # A set of internal utilities for coercing data to protobuf messages.
20
+ #
20
21
  module Protobuf
21
22
  ##
22
23
  # Creates an instance of a protobuf message from a hash that may include nested hashes. `google/protobuf` allows
@@ -30,11 +31,15 @@ module Gapic
30
31
  # @return [Object] An instance of the given message class.
31
32
  def self.coerce hash, to:
32
33
  return hash if hash.is_a? to
34
+ return nil if hash.nil?
35
+
36
+ # Special case handling of certain types
37
+ return time_to_timestamp hash if to == Google::Protobuf::Timestamp && hash.is_a?(Time)
33
38
 
34
39
  # Sanity check: input must be a Hash
35
40
  raise ArgumentError, "Value #{hash} must be a Hash or a #{to.name}" unless hash.is_a? Hash
36
41
 
37
- hash = coerce_submessages hash, to
42
+ hash = coerce_submessages hash, to.descriptor
38
43
  to.new hash
39
44
  end
40
45
 
@@ -44,89 +49,58 @@ module Gapic
44
49
  # @private
45
50
  #
46
51
  # @param hash [Hash] The hash whose nested hashes will be coerced.
47
- # @param message_class [Class] The corresponding protobuf message class of the given hash.
52
+ # @param message_descriptor [Google::Protobuf::Descriptor] The protobuf descriptor for the message.
48
53
  #
49
54
  # @return [Hash] A hash whose nested hashes have been coerced.
50
- def self.coerce_submessages hash, message_class
55
+ def self.coerce_submessages hash, message_descriptor
51
56
  return nil if hash.nil?
52
57
  coerced = {}
53
- message_descriptor = message_class.descriptor
54
58
  hash.each do |key, val|
55
59
  field_descriptor = message_descriptor.lookup key.to_s
56
- coerced[key] = if field_descriptor && field_descriptor.type == :message
57
- coerce_submessage val, field_descriptor
58
- elsif field_descriptor && field_descriptor.type == :bytes &&
59
- (val.is_a?(IO) || val.is_a?(StringIO))
60
- val.binmode.read
61
- else
62
- # `google/protobuf` should throw an error if no field descriptor is
63
- # found. Simply pass through.
64
- val
65
- end
60
+ coerced[key] =
61
+ if field_descriptor&.type == :message
62
+ coerce_submessage val, field_descriptor
63
+ elsif field_descriptor&.type == :bytes && (val.is_a?(IO) || val.is_a?(StringIO))
64
+ val.binmode.read
65
+ else
66
+ # For non-message fields, just pass the scalar value through.
67
+ # Note: if field_descriptor is not found, we just pass the value
68
+ # through and let protobuf raise an error.
69
+ val
70
+ end
66
71
  end
67
72
  coerced
68
73
  end
69
74
 
70
75
  ##
71
- # Coerces the value of a field to be acceptable by the instantiation method of the wrapping message.
76
+ # Coerces a message-typed field.
77
+ # The field can be a normal single message, a repeated message, or a map.
72
78
  #
73
79
  # @private
74
80
  #
75
- # @param val [Object] The value to be coerced.
76
- # @param field_descriptor [Google::Protobuf::FieldDescriptor] The field descriptor of the value.
81
+ # @param val [Object] The value to coerce
82
+ # @param field_descriptor [Google::Protobuf::FieldDescriptor] The field descriptor.
77
83
  #
78
- # @return [Object] The coerced version of the given value.
79
84
  def self.coerce_submessage val, field_descriptor
80
- if (field_descriptor.label == :repeated) && !(map_field? field_descriptor)
81
- coerce_array val, field_descriptor
82
- elsif field_descriptor.subtype.msgclass == Google::Protobuf::Timestamp && val.is_a?(Time)
83
- time_to_timestamp val
85
+ if val.is_a? Array
86
+ # Assume this is a repeated message field, iterate over it and coerce
87
+ # each to the message class.
88
+ # Protobuf will raise an error if this assumption is incorrect.
89
+ val.map do |elem|
90
+ coerce elem, to: field_descriptor.subtype.msgclass
91
+ end
92
+ elsif field_descriptor.label == :repeated
93
+ # Non-array passed to a repeated field: assume this is a map, and that
94
+ # a hash is being passed, and let protobuf handle the conversion.
95
+ # Protobuf will raise an error if this assumption is incorrect.
96
+ val
84
97
  else
85
- coerce_value val, field_descriptor
98
+ # Assume this is a normal single message, and coerce to the message
99
+ # class.
100
+ coerce val, to: field_descriptor.subtype.msgclass
86
101
  end
87
102
  end
88
103
 
89
- ##
90
- # Coerces the values of an array to be acceptable by the instantiation method the wrapping message.
91
- #
92
- # @private
93
- #
94
- # @param array [Array<Object>] The values to be coerced.
95
- # @param field_descriptor [Google::Protobuf::FieldDescriptor] The field descriptor of the values.
96
- #
97
- # @return [Array<Object>] The coerced version of the given values.
98
- def self.coerce_array array, field_descriptor
99
- raise ArgumentError, "Value #{array} must be an array" unless array.is_a? Array
100
- array.map do |val|
101
- coerce_value val, field_descriptor
102
- end
103
- end
104
-
105
- ##
106
- # Hack to determine if field_descriptor is for a map.
107
- #
108
- # TODO(geigerj): Remove this once protobuf Ruby supports an official way
109
- # to determine if a FieldDescriptor represents a map.
110
- # See: https://github.com/google/protobuf/issues/3425
111
- def self.map_field? field_descriptor
112
- (field_descriptor.label == :repeated) &&
113
- (field_descriptor.subtype.name.include? "_MapEntry_")
114
- end
115
-
116
- ##
117
- # Coerces the value of a field to be acceptable by the instantiation method of the wrapping message.
118
- #
119
- # @private
120
- #
121
- # @param val [Object] The value to be coerced.
122
- # @param field_descriptor [Google::Protobuf::FieldDescriptor] The field descriptor of the value.
123
- #
124
- # @return [Object] The coerced version of the given value.
125
- def self.coerce_value val, field_descriptor
126
- return val unless (val.is_a? Hash) && !(map_field? field_descriptor)
127
- coerce val, to: field_descriptor.subtype.msgclass
128
- end
129
-
130
104
  ##
131
105
  # Utility for converting a Google::Protobuf::Timestamp instance to a Ruby time.
132
106
  #
@@ -147,6 +121,6 @@ module Gapic
147
121
  Google::Protobuf::Timestamp.new seconds: time.to_i, nanos: time.nsec
148
122
  end
149
123
 
150
- private_class_method :coerce_submessages, :coerce_submessage, :coerce_array, :coerce_value, :map_field?
124
+ private_class_method :coerce_submessages, :coerce_submessage
151
125
  end
152
126
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gapic-common
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.18.0
4
+ version: 0.19.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Google API Authors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-02-27 00:00:00.000000000 Z
11
+ date: 2023-05-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday