google-gax 0.8.3 → 0.8.4

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
  SHA1:
3
- metadata.gz: b8f8fbb8074fa13d17a67dc1e137f8320ed9736c
4
- data.tar.gz: 90ec64abac56020821a06406097b2eb55624c19f
3
+ metadata.gz: 7fb40d292b456b41ebc200bcffb22da7e4ca50da
4
+ data.tar.gz: b9fcf56ab01370e96b5994882ebb920269d40a73
5
5
  SHA512:
6
- metadata.gz: 0dec6538bac3b781afcd5a1e8a6595115403e39136cd72f694cc97103e0a9e6bc84a44b152d3d508a9e25d19d6717d606443d11f09bbb41431933a74b42b4269
7
- data.tar.gz: 95d8c3af7900cf43432db82b93f0502c64fc367ef342b6fddefb9e827ec8ea453a7150d32b0b5a3633209b8ff93bc4e3e229854b85186a95ca2e1116b3fed06d
6
+ metadata.gz: 721417435023acd011e44287fa6d4cbeabc98b45f411f5d87bdd4a3bc672ea92d991e29957fadfa394d98b4d5696ca88cc55ed837ccbebc7513e934ae1d58cca
7
+ data.tar.gz: dda6c253c18408035897de6c50724609e03f3d7cfa799c2d246b981ad950c9a5313379da769723ea1f8c55f46192ee70f6d6528973cacf3db44ad7397c860c9d
data/lib/google/gax.rb CHANGED
@@ -32,6 +32,7 @@ require 'google/gax/constants'
32
32
  require 'google/gax/errors'
33
33
  require 'google/gax/path_template'
34
34
  require 'google/gax/settings'
35
+ require 'google/gax/util'
35
36
  require 'google/gax/version'
36
37
 
37
38
  module Google
@@ -0,0 +1,130 @@
1
+ # Copyright 2017, Google Inc.
2
+ # All rights reserved.
3
+ #
4
+ # Redistribution and use in source and binary forms, with or without
5
+ # modification, are permitted provided that the following conditions are
6
+ # met:
7
+ #
8
+ # * Redistributions of source code must retain the above copyright
9
+ # notice, this list of conditions and the following disclaimer.
10
+ # * Redistributions in binary form must reproduce the above
11
+ # copyright notice, this list of conditions and the following disclaimer
12
+ # in the documentation and/or other materials provided with the
13
+ # distribution.
14
+ # * Neither the name of Google Inc. nor the names of its
15
+ # contributors may be used to endorse or promote products derived from
16
+ # this software without specific prior written permission.
17
+ #
18
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
+ # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
+ # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
+ # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
+ # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
+ # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
+ # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
+ # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
+ # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
+
30
+ module Google
31
+ # Gax defines Google API extensions
32
+ module Gax
33
+ # Creates an instance of a protobuf message from a hash that may include
34
+ # nested hashes. `google/protobuf` allows for the instantiation of protobuf
35
+ # messages using hashes but does not allow for nested hashes to instantiate
36
+ # nested submessages.
37
+ #
38
+ # @param hash [Hash] The hash to be converted into a proto message.
39
+ # @param message_class [Class] The corresponding protobuf message class of
40
+ # the given hash.
41
+ #
42
+ # @return [Object] An instance of the given message class.
43
+ def to_proto(hash, message_class)
44
+ hash = coerce_submessages(hash, message_class)
45
+ message_class.new(hash)
46
+ end
47
+
48
+ # Coerces values of the given hash to be acceptable by the instantiation
49
+ # method provided by `google/protobuf`
50
+ #
51
+ # @private
52
+ #
53
+ # @param hash [Hash] The hash whose nested hashes will be coerced.
54
+ # @param message_class [Class] The corresponding protobuf message class of
55
+ # the given hash.
56
+ #
57
+ # @return [Hash] A hash whose nested hashes have been coerced.
58
+ def coerce_submessages(hash, message_class)
59
+ return nil if hash.nil?
60
+ coerced = {}
61
+ message_descriptor = message_class.descriptor
62
+ hash.each do |key, val|
63
+ field_descriptor = message_descriptor.lookup(key.to_s)
64
+ if field_descriptor && field_descriptor.type == :message
65
+ coerced[key] = coerce_submessage(val, field_descriptor)
66
+ else
67
+ # `google/protobuf` should throw an error if no field descriptor is
68
+ # found. Simply pass through.
69
+ coerced[key] = val
70
+ end
71
+ end
72
+ coerced
73
+ end
74
+
75
+ # Coerces the value of a field to be acceptable by the instantiation method
76
+ # of the wrapping message.
77
+ #
78
+ # @private
79
+ #
80
+ # @param val [Object] The value to be coerced.
81
+ # @param field_descriptor [Google::Protobuf::FieldDescriptor] The field
82
+ # descriptor of the value.
83
+ #
84
+ # @return [Object] The coerced version of the given value.
85
+ def coerce_submessage(val, field_descriptor)
86
+ if field_descriptor.label == :repeated
87
+ coerce_array(val, field_descriptor)
88
+ else
89
+ coerce(val, field_descriptor)
90
+ end
91
+ end
92
+
93
+ # Coerces the values of an array to be acceptable by the instantiation
94
+ # method the wrapping message.
95
+ #
96
+ # @private
97
+ #
98
+ # @param array [Array<Object>] The values to be coerced.
99
+ # @param field_descriptor [Google::Protobuf::FieldDescriptor] The field
100
+ # descriptor of the values.
101
+ #
102
+ # @return [Array<Object>] The coerced version of the given values.
103
+ def coerce_array(array, field_descriptor)
104
+ raise ArgumentError unless array.is_a? Array
105
+ array.map do |val|
106
+ coerce(val, field_descriptor)
107
+ end
108
+ end
109
+
110
+ # Coerces the value of a field to be acceptable by the instantiation method
111
+ # of the wrapping message.
112
+ #
113
+ # @private
114
+ #
115
+ # @param val [Object] The value to be coerced.
116
+ # @param field_descriptor [Google::Protobuf::FieldDescriptor] The field
117
+ # descriptor of the value.
118
+ #
119
+ # @return [Object] The coerced version of the given value.
120
+ def coerce(val, field_descriptor)
121
+ return val unless val.is_a? Hash
122
+ to_proto(val, field_descriptor.subtype.msgclass)
123
+ end
124
+
125
+ module_function :to_proto, :coerce_submessages, :coerce_submessage,
126
+ :coerce_array, :coerce
127
+ private_class_method :coerce_submessages, :coerce_submessage, :coerce_array,
128
+ :coerce
129
+ end
130
+ end
@@ -29,6 +29,6 @@
29
29
 
30
30
  module Google
31
31
  module Gax
32
- VERSION = '0.8.3'.freeze
32
+ VERSION = '0.8.4'.freeze
33
33
  end
34
34
  end
@@ -0,0 +1,35 @@
1
+ // Copyright 2016 Google Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ syntax = "proto3";
15
+
16
+ package google.protobuf;
17
+
18
+ message Request {
19
+ string name = 1;
20
+ User user = 2;
21
+ }
22
+
23
+ message User {
24
+ enum UserType {
25
+ UNSPECIFIED = 0;
26
+ ADMINISTRATOR = 1;
27
+ }
28
+ string name = 1;
29
+ UserType type = 2;
30
+ repeated Post posts = 3;
31
+ }
32
+
33
+ message Post {
34
+ string text = 1;
35
+ }
@@ -0,0 +1,32 @@
1
+ # Generated by the protocol buffer compiler. DO NOT EDIT!
2
+ # source: fixture.proto
3
+
4
+ require 'google/protobuf'
5
+
6
+ Google::Protobuf::DescriptorPool.generated_pool.build do
7
+ add_message "google.protobuf.Request" do
8
+ optional :name, :string, 1
9
+ optional :user, :message, 2, "google.protobuf.User"
10
+ end
11
+ add_message "google.protobuf.User" do
12
+ optional :name, :string, 1
13
+ optional :type, :enum, 2, "google.protobuf.User.UserType"
14
+ repeated :posts, :message, 3, "google.protobuf.Post"
15
+ end
16
+ add_enum "google.protobuf.User.UserType" do
17
+ value :UNSPECIFIED, 0
18
+ value :ADMINISTRATOR, 1
19
+ end
20
+ add_message "google.protobuf.Post" do
21
+ optional :text, :string, 1
22
+ end
23
+ end
24
+
25
+ module Google
26
+ module Protobuf
27
+ Request = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Request").msgclass
28
+ User = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.User").msgclass
29
+ User::UserType = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.User.UserType").enummodule
30
+ Post = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Post").msgclass
31
+ end
32
+ end
@@ -0,0 +1,104 @@
1
+ # Copyright 2017, Google Inc.
2
+ # All rights reserved.
3
+ #
4
+ # Redistribution and use in source and binary forms, with or without
5
+ # modification, are permitted provided that the following conditions are
6
+ # met:
7
+ #
8
+ # * Redistributions of source code must retain the above copyright
9
+ # notice, this list of conditions and the following disclaimer.
10
+ # * Redistributions in binary form must reproduce the above
11
+ # copyright notice, this list of conditions and the following disclaimer
12
+ # in the documentation and/or other materials provided with the
13
+ # distribution.
14
+ # * Neither the name of Google Inc. nor the names of its
15
+ # contributors may be used to endorse or promote products derived from
16
+ # this software without specific prior written permission.
17
+ #
18
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
+ # 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
+ # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
+ # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
+ # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
+ # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
+ # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
+ # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
+ # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
+
30
+ require 'google/gax'
31
+ require 'spec/fixtures/fixture_pb'
32
+
33
+ describe Google::Gax do
34
+ describe '#to_proto' do
35
+ REQUEST_NAME = 'path/to/ernest'.freeze
36
+ USER_NAME = 'Ernest'.freeze
37
+ USER_TYPE = :ADMINISTRATOR
38
+ POST_TEXT = 'This is a test post.'.freeze
39
+
40
+ it 'creates a protobuf message from a simple hash' do
41
+ hash = { name: USER_NAME, type: USER_TYPE }
42
+ user = Google::Gax.to_proto(hash, Google::Protobuf::User)
43
+ expect(user).to be_an_instance_of(Google::Protobuf::User)
44
+ expect(user.name).to eq(USER_NAME)
45
+ expect(user.type).to eq(USER_TYPE)
46
+ end
47
+
48
+ it 'creates a protobuf message from a hash with a nested message' do
49
+ request_hash = {
50
+ name: REQUEST_NAME,
51
+ user: Google::Protobuf::User.new(name: USER_NAME, type: USER_TYPE)
52
+ }
53
+ request = Google::Gax.to_proto(request_hash, Google::Protobuf::Request)
54
+ expect(request).to be_an_instance_of(Google::Protobuf::Request)
55
+ expect(request.name).to eq(REQUEST_NAME)
56
+ expect(request.user).to be_an_instance_of(Google::Protobuf::User)
57
+ expect(request.user.name).to eq(USER_NAME)
58
+ expect(request.user.type).to eq(USER_TYPE)
59
+ end
60
+
61
+ it 'creates a protobuf message from a hash with a nested hash' do
62
+ request_hash = {
63
+ name: REQUEST_NAME,
64
+ user: { name: USER_NAME, type: USER_TYPE }
65
+ }
66
+ request = Google::Gax.to_proto(request_hash, Google::Protobuf::Request)
67
+ expect(request).to be_an_instance_of(Google::Protobuf::Request)
68
+ expect(request.name).to eq(REQUEST_NAME)
69
+ expect(request.user).to be_an_instance_of(Google::Protobuf::User)
70
+ expect(request.user.name).to eq(USER_NAME)
71
+ expect(request.user.type).to eq(USER_TYPE)
72
+ end
73
+
74
+ it 'handles nested arrays of both messages and hashes' do
75
+ user_hash = {
76
+ name: USER_NAME,
77
+ type: USER_TYPE,
78
+ posts: [
79
+ { text: POST_TEXT },
80
+ Google::Protobuf::Post.new(text: POST_TEXT)
81
+ ]
82
+ }
83
+ user = Google::Gax.to_proto(user_hash, Google::Protobuf::User)
84
+ expect(user).to be_an_instance_of(Google::Protobuf::User)
85
+ expect(user.name).to eq(USER_NAME)
86
+ expect(user.type).to eq(USER_TYPE)
87
+ expect(user.posts).to be_a(Google::Protobuf::RepeatedField)
88
+ user.posts.each do |post|
89
+ expect(post).to be_an_instance_of(Google::Protobuf::Post)
90
+ expect(post.text).to eq(POST_TEXT)
91
+ end
92
+ end
93
+
94
+ it 'fails if a key does not exist in the target message type' do
95
+ user_hash = {
96
+ name: USER_NAME,
97
+ fake_key: 'fake data'
98
+ }
99
+ expect do
100
+ Google::Gax.to_proto(user_hash, Google::Protobuf::User)
101
+ end.to raise_error(ArgumentError)
102
+ end
103
+ end
104
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-gax
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.3
4
+ version: 0.8.4
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: 2017-06-06 00:00:00.000000000 Z
11
+ date: 2017-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: googleauth
@@ -126,16 +126,16 @@ dependencies:
126
126
  name: rubocop
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
- - - "~>"
129
+ - - '='
130
130
  - !ruby/object:Gem::Version
131
- version: '0.32'
131
+ version: 0.47.1
132
132
  type: :development
133
133
  prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
- - - "~>"
136
+ - - '='
137
137
  - !ruby/object:Gem::Version
138
- version: '0.32'
138
+ version: 0.47.1
139
139
  - !ruby/object:Gem::Dependency
140
140
  name: simplecov
141
141
  requirement: !ruby/object:Gem::Requirement
@@ -167,14 +167,18 @@ files:
167
167
  - lib/google/gax/operation.rb
168
168
  - lib/google/gax/path_template.rb
169
169
  - lib/google/gax/settings.rb
170
+ - lib/google/gax/util.rb
170
171
  - lib/google/gax/version.rb
171
172
  - lib/google/longrunning/operations_client.rb
172
173
  - lib/google/longrunning/operations_client_config.json
174
+ - spec/fixtures/fixture.proto
175
+ - spec/fixtures/fixture_pb.rb
173
176
  - spec/google/gax/api_callable_spec.rb
174
177
  - spec/google/gax/bundling_spec.rb
175
178
  - spec/google/gax/operation_spec.rb
176
179
  - spec/google/gax/path_template_spec.rb
177
180
  - spec/google/gax/settings_spec.rb
181
+ - spec/google/gax/util_spec.rb
178
182
  - spec/spec_helper.rb
179
183
  homepage: https://github.com/googleapis/gax-ruby
180
184
  licenses: