google-ads-googleads 2.2.1 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fc8e4f351fdb74766a4707c673e7128ce176d6ccd71ea7502f1239f9c9f541ae
4
- data.tar.gz: a9d1a0e8b06282165ac3e164085f79b22fa3ab07ba71d645c4359cd113f48435
3
+ metadata.gz: 6414dafc809b2e584848d7c95481177a28145d053b231a8df0350d130a3bd0b0
4
+ data.tar.gz: 0cd532287d4dd3c7c1d89becddcbdb5941515406fcb62eeccad32a479d44a0f4
5
5
  SHA512:
6
- metadata.gz: 7aea8684a68e7caee59fb24a5a4d89b36317759422c157f93ba41f51fddd31b2ec2d400b5f2d02ad9f16acf8ff5ca529012cf70e7a326254d3ca7131cb347007
7
- data.tar.gz: 85a689bd378f7cc2d7943952dd205175137e579b39832c121e09e640c645a6d48a57299d78e2cbe3a1118ae615979048ac715b143d109b2c3e529a4f4afef301
6
+ metadata.gz: 37fa253c3b4bcb815b292bf700eb5a5c2ab5ef326e0ab48431a931b47fc432efc24763a44d78256a7a472bd6d580ea34692564eb9f94f8d5a938240746259bf9
7
+ data.tar.gz: 30b84e57df5d640c481eb76a65aab43ac23d0a11016d5c5bd8bacd17aa17a44eba45ff337e4c410940368a541ee233f10e49f495f0c846698ef6be92f5854140
data/ChangeLog CHANGED
@@ -1,3 +1,6 @@
1
+ 2.3.0:
2
+ - Add autoboxing fields that set value wrappers from primitives
3
+
1
4
  2.2.1:
2
5
  - Fix support for long running operations.
3
6
 
@@ -0,0 +1,102 @@
1
+ require 'google/protobuf/message_exts'
2
+ require 'google/protobuf/wrappers_pb'
3
+ module Google
4
+ module Ads
5
+ module GoogleAds
6
+ module AutoboxingFields
7
+ MAPPINGS = {
8
+ Google::Protobuf::Int32Value => lambda { |x| Integer(x) },
9
+ Google::Protobuf::Int64Value => lambda { |x| Integer(x) },
10
+ Google::Protobuf::StringValue => lambda { |x| String(x) },
11
+ Google::Protobuf::BoolValue => lambda { |x|
12
+ case x
13
+ when TrueClass, true, "true", "TRUE", "True", "1", 1
14
+ true
15
+ when FalseClass, false, "false", "FALSE", "False", "0", 0
16
+ false
17
+ else
18
+ raise ArgumentError.new("Value #{x} is not boolish")
19
+ end
20
+ },
21
+ Google::Protobuf::DoubleValue => lambda { |x| Float(x) },
22
+ Google::Protobuf::BytesValue => lambda { |x| x.force_encoding("ASCII-8BIT") },
23
+ }
24
+
25
+ def self.patch_class(klass)
26
+ return if klass.instance_variable_get(:@_patched_for_autoboxing_fields)
27
+ klass.instance_variable_set(:@_patched_for_autoboxing_fields, true)
28
+
29
+ klass.instance_eval do
30
+ fields = []
31
+ descriptor.each do |field|
32
+ if field.type == :message && AutoboxingFields.is_value_field?(field.subtype.msgclass)
33
+ fields << field
34
+ AutoboxingFields.patch_field_for_autoboxing(field, self)
35
+ end
36
+ end
37
+
38
+ AutoboxingFields.patch_constructor_for_autoboxing(fields, self)
39
+ end
40
+ end
41
+
42
+ def self.is_value_field?(class_name)
43
+ MAPPINGS.keys.include?(class_name)
44
+ end
45
+
46
+ def self.patch_constructor_for_autoboxing(fields, klass_to_patch)
47
+ orig_initialize = klass_to_patch.instance_method(:initialize)
48
+ klass_to_patch.instance_eval do
49
+ define_method(:initialize) do |**kwargs|
50
+ new_kwargs = Hash[kwargs.map { |name, value|
51
+ field = fields.select { |x| x.name == name.to_s }.first
52
+ actual_value = if field
53
+ if field.subtype.msgclass === value
54
+ value
55
+ else
56
+ field.subtype.msgclass.new(value: MAPPINGS.fetch(field.subtype.msgclass).call(value))
57
+ end
58
+ else
59
+ value
60
+ end
61
+ [name, actual_value]
62
+ }]
63
+ orig_initialize.bind(self).call(**new_kwargs)
64
+ end
65
+ end
66
+ end
67
+
68
+ def self.patch_field_for_autoboxing(field, klass_to_patch)
69
+ name = field.name
70
+ field_klass = field.subtype.msgclass
71
+ mapping = MAPPINGS.fetch(field_klass)
72
+
73
+ klass_to_patch.instance_eval do
74
+ define_method("#{name}=".to_sym) do |value|
75
+ if value.nil?
76
+ send(:method_missing, :"#{name}=", value)
77
+ elsif field_klass === value
78
+ send(:method_missing, :"#{name}=", value)
79
+ else
80
+ send(:method_missing, :"#{name}=", field_klass.new(value: mapping.call(value)))
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
89
+
90
+ module Google::Protobuf
91
+ module MessageExts
92
+ module ClassMethods
93
+ def new(*args, &blk)
94
+ if self.name.start_with?("Google::Ads::GoogleAds")
95
+ Google::Ads::GoogleAds::AutoboxingFields.patch_class(self)
96
+ end
97
+
98
+ super
99
+ end
100
+ end
101
+ end
102
+ end
@@ -38,6 +38,10 @@ module Google
38
38
  end
39
39
  end
40
40
 
41
+ # this require needs to always come first to patch protobufs to have "autoboxing
42
+ # fields"
43
+ require 'google/ads/google_ads/autoboxing_fields'
44
+
41
45
  require 'googleauth'
42
46
 
43
47
  require 'google/ads/google_ads/patches'
@@ -19,7 +19,7 @@
19
19
  module Google
20
20
  module Ads
21
21
  module GoogleAds
22
- CLIENT_LIB_VERSION = '2.2.1'.freeze
22
+ CLIENT_LIB_VERSION = '2.3.0'.freeze
23
23
  end
24
24
  end
25
25
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-ads-googleads
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.1
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Google Inc
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-28 00:00:00.000000000 Z
11
+ date: 2019-06-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: google-gax
@@ -85,6 +85,7 @@ files:
85
85
  - google_ads_config.rb
86
86
  - lib/google/ads/google_ads.rb
87
87
  - lib/google/ads/google_ads/api_versions.rb
88
+ - lib/google/ads/google_ads/autoboxing_fields.rb
88
89
  - lib/google/ads/google_ads/config.rb
89
90
  - lib/google/ads/google_ads/errors.rb
90
91
  - lib/google/ads/google_ads/factories.rb