gapic-common 0.18.0 → 0.19.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/lib/gapic/common/version.rb +1 -1
- data/lib/gapic/protobuf.rb +39 -66
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8f4c41c2fffb829cfadc7792a8e1e2a15162ae960ae810f216dd4cb677dc9098
|
4
|
+
data.tar.gz: 3214f4236eb38446cf46b89d1957c659abbbb8265eecafe6275d8448dfdfabb8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d18733e8a412ed7c366e14a253a62f460b1685503ee71bc435b35bd152263a48ace22f7d3be1da50c39da7b67bdcd720e70054154cee029c68cb670a0b79e25c
|
7
|
+
data.tar.gz: f409bbbe7036ab93a7952b5085b1bd11af4ee902afe36625fe2872bc142ced1c27f22b88d967ad4fb64d35d141af7cba678ab57bcae8f5709132db1e3e3b9c84
|
data/CHANGELOG.md
CHANGED
data/lib/gapic/common/version.rb
CHANGED
data/lib/gapic/protobuf.rb
CHANGED
@@ -16,7 +16,8 @@ require "google/protobuf/timestamp_pb"
|
|
16
16
|
|
17
17
|
module Gapic
|
18
18
|
##
|
19
|
-
#
|
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
|
@@ -31,10 +32,13 @@ module Gapic
|
|
31
32
|
def self.coerce hash, to:
|
32
33
|
return hash if hash.is_a? to
|
33
34
|
|
35
|
+
# Special case handling of certain types
|
36
|
+
return time_to_timestamp hash if to == Google::Protobuf::Timestamp && hash.is_a?(Time)
|
37
|
+
|
34
38
|
# Sanity check: input must be a Hash
|
35
39
|
raise ArgumentError, "Value #{hash} must be a Hash or a #{to.name}" unless hash.is_a? Hash
|
36
40
|
|
37
|
-
hash = coerce_submessages hash, to
|
41
|
+
hash = coerce_submessages hash, to.descriptor
|
38
42
|
to.new hash
|
39
43
|
end
|
40
44
|
|
@@ -44,89 +48,58 @@ module Gapic
|
|
44
48
|
# @private
|
45
49
|
#
|
46
50
|
# @param hash [Hash] The hash whose nested hashes will be coerced.
|
47
|
-
# @param
|
51
|
+
# @param message_descriptor [Google::Protobuf::Descriptor] The protobuf descriptor for the message.
|
48
52
|
#
|
49
53
|
# @return [Hash] A hash whose nested hashes have been coerced.
|
50
|
-
def self.coerce_submessages hash,
|
54
|
+
def self.coerce_submessages hash, message_descriptor
|
51
55
|
return nil if hash.nil?
|
52
56
|
coerced = {}
|
53
|
-
message_descriptor = message_class.descriptor
|
54
57
|
hash.each do |key, val|
|
55
58
|
field_descriptor = message_descriptor.lookup key.to_s
|
56
|
-
coerced[key] =
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
59
|
+
coerced[key] =
|
60
|
+
if field_descriptor&.type == :message
|
61
|
+
coerce_submessage val, field_descriptor
|
62
|
+
elsif field_descriptor&.type == :bytes && (val.is_a?(IO) || val.is_a?(StringIO))
|
63
|
+
val.binmode.read
|
64
|
+
else
|
65
|
+
# For non-message fields, just pass the scalar value through.
|
66
|
+
# Note: if field_descriptor is not found, we just pass the value
|
67
|
+
# through and let protobuf raise an error.
|
68
|
+
val
|
69
|
+
end
|
66
70
|
end
|
67
71
|
coerced
|
68
72
|
end
|
69
73
|
|
70
74
|
##
|
71
|
-
# Coerces
|
75
|
+
# Coerces a message-typed field.
|
76
|
+
# The field can be a normal single message, a repeated message, or a map.
|
72
77
|
#
|
73
78
|
# @private
|
74
79
|
#
|
75
|
-
# @param val [Object] The value to
|
76
|
-
# @param field_descriptor [Google::Protobuf::FieldDescriptor] The field descriptor
|
80
|
+
# @param val [Object] The value to coerce
|
81
|
+
# @param field_descriptor [Google::Protobuf::FieldDescriptor] The field descriptor.
|
77
82
|
#
|
78
|
-
# @return [Object] The coerced version of the given value.
|
79
83
|
def self.coerce_submessage val, field_descriptor
|
80
|
-
if
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
+
if val.is_a? Array
|
85
|
+
# Assume this is a repeated message field, iterate over it and coerce
|
86
|
+
# each to the message class.
|
87
|
+
# Protobuf will raise an error if this assumption is incorrect.
|
88
|
+
val.map do |elem|
|
89
|
+
coerce elem, to: field_descriptor.subtype.msgclass
|
90
|
+
end
|
91
|
+
elsif field_descriptor.label == :repeated
|
92
|
+
# Non-array passed to a repeated field: assume this is a map, and that
|
93
|
+
# a hash is being passed, and let protobuf handle the conversion.
|
94
|
+
# Protobuf will raise an error if this assumption is incorrect.
|
95
|
+
val
|
84
96
|
else
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
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
|
97
|
+
# Assume this is a normal single message, and coerce to the message
|
98
|
+
# class.
|
99
|
+
coerce val, to: field_descriptor.subtype.msgclass
|
102
100
|
end
|
103
101
|
end
|
104
102
|
|
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
103
|
##
|
131
104
|
# Utility for converting a Google::Protobuf::Timestamp instance to a Ruby time.
|
132
105
|
#
|
@@ -147,6 +120,6 @@ module Gapic
|
|
147
120
|
Google::Protobuf::Timestamp.new seconds: time.to_i, nanos: time.nsec
|
148
121
|
end
|
149
122
|
|
150
|
-
private_class_method :coerce_submessages, :coerce_submessage
|
123
|
+
private_class_method :coerce_submessages, :coerce_submessage
|
151
124
|
end
|
152
125
|
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.
|
4
|
+
version: 0.19.0
|
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-
|
11
|
+
date: 2023-05-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|