Utils/FieldMasks.pm

filter: Statementstmt 100% 76 / 76 Branchbran 96.4% 27 / 28 Conditioncond 75.0% 18 / 24 MC/DCmcdc 66.6% 8 / 12 Subroutinesub 100% 14 / 14 Podpod 100% 4 / 4 totaltotal 93.0% 147 / 158 CC 27 SCAR 23.1
CC
27
Cov
95%
__compare
18.0
__is_clearing_message
7.8
is_hash_ref
3.0
CRAP 10.0
« LongRunning/OperationService.pm ↑ Summary OAuth2ApplicationsHandler.pm »
1 # Copyright 2019, Google LLC
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 #
15 # Utility for constructing field masks, which are necessary for update
16 # operations.
17  
18 package Google::Ads::GoogleAds::Utils::FieldMasks;
19  
20 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 # The following needs to be on one line because CPAN uses a particularly hacky
25 # eval() to determine module versions.
26 2 ▶use Google::Ads::GoogleAds::Constants; our $VERSION = ${Google::Ads::GoogleAds::Constants::VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use Google::Ads::GoogleAds::Common::FieldMask;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use Google::Ads::GoogleAds::Utils::GoogleAdsHelper;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 our @EXPORT = qw(field_mask all_set_fields_of get_field_value is_hash_ref);
32  
33 2 ▶use Data::Compare;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 # Compares two hash objects and computes a FieldMask object based on the differences
36 # between them. The field mask is necessary for update operations, and the field
37 # paths in the field mask are in lower underscore format.
38 27 ▶sub field_mask {
Subroutine field_mask: called CC=1 SCAR=0.0
Pod documented
39 27 my ($original, $modified) = @_;
40 27 my $paths = [];
41 27 __compare($paths, "", $original, $modified);
42  
43 27 return Google::Ads::GoogleAds::Common::FieldMask->new({paths => $paths});
44 }
45  
46 # Constructs a FieldMask object that captures the list of all set fields of an object.
47 # The field paths in the field mask are in lower underscore format.
48 5 ▶sub all_set_fields_of {
Subroutine all_set_fields_of: called CC=1 SCAR=0.0
Pod documented
49 5 my ($modified) = @_;
50 5 return field_mask({}, $modified);
51 }
52  
53 # Looks up the value of the field located at the given path on an object.
54 4 ▶sub get_field_value {
Subroutine get_field_value: called CC=2 SCAR=6.9
Pod documented
55 4 my ($object, $path) = @_;
56  
57 4 my $value = $object;
58 4 $value = $value->{$_} for split(/\./, $path);
59  
60 4 return $value;
61 }
62  
63 # The private method to compare a given field for two objects, and capture the
64 # differences between them recursively.
65 47 ▶sub __compare {
Subroutine __compare: called CC=18 SCAR=28.9
66 47 my ($paths, $current_field, $original, $modified) = @_;
67  
68 # Combine fields from original and modified resource, since the original might
69 # have nested fields which are cleared in the modified resource.
70 47 my %key_hash = ();
71 47 foreach my $key (keys %$original) { $key_hash{$key} = 1; }
72 47 foreach my $key (keys %$modified) { $key_hash{$key} = 1; }
73  
74 47 foreach my $key (keys %key_hash) {
75 # The field mask should contain the field path in underscore format.
76 68 my $field = to_lower_underscore($key);
77 68 ▶ my $field_path = $current_field ? $current_field . "." . $field : $field;
Branch: 1 branch
BranchTrueFalseTotal
$current_field ? : 27 41 68
78  
79 # Extract values from original and modified objects.
80 68 my $modified_value = $modified->{$key};
81 68 my $original_value = $original->{$key};
82 68 my $original_key_exists = exists $original->{$key};
83  
84 68 ▶ if (ref($original_value) eq "ARRAY" || ref($modified_value) eq "ARRAY") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $original_value eq 'ARRAY' or ref $modified_value eq 'ARRAY') { } 8 60 68
elsif (is_hash_ref($original_value) or is_hash_ref($modified_value)) { } 27 33 60
Condition100%3/3
ref $original_value eq "ARRAY" or ref $modified_value eq "ARRAY"
l!l&&r!l&&!r
6260
MC/DC100%2/2
ref $original_value eq "ARRAY" or ref $modified_value eq "ARRAY"
ref $original_value eq "ARRAY" ref $modified_value eq "ARRAY"
Truth table100%3/3
A or B
A= ref $original_value eq "ARRAY"B= ref $modified_value eq "ARRAY"
ABresult
1X1
011
000
85 # Array reference field.
86 8 ▶ push @$paths, $field_path
Branch: 1 branch
BranchTrueFalseTotal
unless Compare($original_value, $modified_value) 7 1 8
87 unless Compare($original_value, $modified_value);
88 ▶ } elsif (is_hash_ref($original_value) || is_hash_ref($modified_value)) {
Condition100%3/3
is_hash_ref($original_value) or is_hash_ref($modified_value)
l!l&&r!l&&!r
72033
MC/DC100%2/2
is_hash_ref($original_value) or is_hash_ref($modified_value)
is_hash_ref($original_value) is_hash_ref($modified_value)
Truth table100%3/3
A or B
A= is_hash_ref($original_value)B= is_hash_ref($modified_value)
ABresult
1X1
011
000
89 # Hash or class reference field whose ref name is not empty.
90 27 ▶ next if Compare($original_value, $modified_value);
Branch: 1 branch
BranchTrueFalseTotal
if Compare($original_value, $modified_value) 0 27 27
91 27 ▶ if (!$original_key_exists) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $original_key_exists) { } 20 7 27
elsif (__is_clearing_message($original_value, $modified_value)) { } 1 6 7
92 # If the modified value is an empty object that doesn't exist in the original
93 # then add it to the paths list. Otherwise recurse on the modified value object.
94 20 ▶ if (!%$modified_value) { push @$paths, $field_path; }
Branch: 1 branch
BranchTrueFalseTotal
if (not %$modified_value) { } 6 14 20
95 else {
96 14 __compare($paths, $field_path, {}, $modified_value);
97 }
98 } elsif (__is_clearing_message($original_value, $modified_value)) {
99 1 push @$paths, $field_path;
100 } else {
101 6 ▶ if (!defined $modified_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $modified_value) { } 1 5 6
102 1 __compare($paths, $field_path, $original_value, {});
103 } else {
104 5 __compare($paths, $field_path, $original_value, $modified_value);
105 }
106 }
107 } else {
108 # Scalar field or both $modified_value and $original_value are undef.
109 33 ▶ if (!$original_key_exists && !defined $modified_value) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($original_key_exists or defined $modified_value) 1 32 33
Condition100%3/3
$original_key_exists or defined $modified_value
l!l&&r!l&&!r
12201
MC/DC100%2/2
$original_key_exists or defined $modified_value
$original_key_exists defined $modified_value
Truth table100%3/3
A or B
A= $original_key_existsB= defined $modified_value
ABresult
1X1
011
000
110 1 push @$paths, $field_path;
111 1 next;
112 }
113  
114 32 ▶ push @$paths, $field_path
Branch: 1 branch
BranchTrueFalseTotal
unless Compare($original_value, $modified_value) 30 2 32
115 unless Compare($original_value, $modified_value);
116 }
117 }
118 }
119  
120 # The method to check if a reference object is for a hash or a class instance.
121 113 ▶sub is_hash_ref {
Subroutine is_hash_ref: called CC=3 SCAR=11.0
Pod documented
122 113 my $ref_type = ref shift;
123 113 ▶ return 0 if !$ref_type;
Branch: 1 branch
BranchTrueFalseTotal
unless $ref_type 83 30 113
124  
125 # A boolean value reference in a JSON object is evaluated as "JSON::PP::Boolean".
126 30 my @invalid_types = ("SCALAR", "ARRAY", "JSON::PP::Boolean");
127 30 ▶ return 0 if grep /^$ref_type/, @invalid_types;
Branch: 1 branch
BranchTrueFalseTotal
if grep /^$ref_type/, @invalid_types 3 27 30
128  
129 27 return 1;
130 }
131  
132 7 ▶sub __is_clearing_message {
Subroutine __is_clearing_message: called CC=7 SCAR=20.5
133 7 my ($original_value, $modified_value) = @_;
134  
135 7 my $original_is_defined = defined $original_value;
136 7 my $modified_is_defined = defined $modified_value;
137  
138 7 my $original_values_size = values %$original_value;
139 7 my $modified_values_size = values %$modified_value;
140  
141 # Returns true if the original message contains an empty message field that is not present on the
142 # modified message, or vice-versa, in which case the user is attempting to clear the top level
143 # message field.
144 7 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if not $modified_is_defined and $original_is_defined and $original_values_size == 0 or not $original_is_defined and $modified_is_defined and $modified_values_size == 0 1 6 7
Condition60%9/15
not $modified_is_defined and $original_is_defined and $original_values_size == 0 or not $original_is_defined and $modified_is_defined and $modified_values_size == 0
l!l&&r!l&&!r
106
not $modified_is_defined and $original_is_defined and $original_values_size == 0
!ll&&!rl&&r
511
not $modified_is_defined and $original_is_defined
!ll&&!rl&&r
502
not $original_is_defined and $modified_is_defined and $modified_values_size == 0
!ll&&!rl&&r
600
not $original_is_defined and $modified_is_defined
!ll&&!rl&&r
600
MC/DC33%2/6
not $modified_is_defined and $original_is_defined and $original_values_size == 0 or not $original_is_defined and $modified_is_defined and $modified_values_size == 0
not $modified_is_defined $original_is_defined $original_values_size == 0 not $original_is_defined $modified_is_defined $modified_values_size == 0
Truth table23%3/13
not(A) and B and C or not(D) and E and F
A= not $modified_is_definedB= $original_is_definedC= $original_values_size == 0D= not $original_is_definedE= $modified_is_definedF= $modified_values_size == 0
ABCDEFresult
111XXX1
0XX1111
10X1111
1101111
0XX0XX0
0XX10X0
0XX1100
10X0XX0
10X10X0
10X1100
1100XX0
11010X0
1101100
145 if ((
146 !$modified_is_defined
147 and $original_is_defined
148 and $original_values_size == 0
149 )
150 or ( !$original_is_defined
151 and $modified_is_defined
152 and $modified_values_size == 0));
153  
154 6 return 0;
155 }
156  
157 2 1;
158  
159 =pod
160  
161 =head1 NAME
162  
163 Google::Ads::GoogleAds::Utils::FieldMasks
164  
165 =head1 DESCRIPTION
166  
167 Utility for constructing field masks, which are necessary for update operations.
168  
169 =head1 METHODS
170  
171 =head2 field_mask
172  
173 Compares two hash objects and computes a L<Google::Ads::GoogleAds::Common::FieldMask>
174 object based on the differences between them. The field mask is necessary for
175 update operations, and the field paths in the field mask are in lower underscore format.
176  
177 =head3 Parameters
178  
179 =over
180  
181 =item *
182  
183 I<original>: the original hash object.
184  
185 =item *
186  
187 I<modified>: the modified hash object.
188  
189 =back
190  
191 =head3 Returns
192  
193 A L<Google::Ads::GoogleAds::Common::FieldMask> object reflecting the changes
194 between the original and modified objects.
195  
196 =head2 all_set_fields_of
197  
198 Constructs a L<Google::Ads::GoogleAds::Common::FieldMask> object that captures
199 the list of all set fields of an object. The field paths in the field mask are
200 in lower underscore format.
201  
202 =head3 Parameters
203  
204 =over
205  
206 =item *
207  
208 I<modified>: the modified hash object.
209  
210 =back
211  
212 =head3 Returns
213  
214 A L<Google::Ads::GoogleAds::Common::FieldMask> object that captures the list of
215 all set fields of an object.
216  
217 =head2 get_field_value
218  
219 Looks up the value of the field located at the given path on an object.
220  
221 =head3 Parameters
222  
223 =over
224  
225 =item *
226  
227 I<object>: the object to search on.
228  
229 =item *
230  
231 I<path>: the path of the field.
232  
233 =back
234  
235 =head3 Returns
236  
237 The value of the field located at the give path on the object.
238  
239 =head2 __compare
240  
241 The private method to compare a given field for two objects, and capture the
242 differences between them recursively.
243  
244 =head3 Parameters
245  
246 =over
247  
248 =item *
249  
250 I<paths>: the paths array to store the differences.
251  
252 =item *
253  
254 I<current_field>: the field name to compare.
255  
256 =item *
257  
258 I<original>: the original hash object.
259  
260 =item *
261  
262 I<modified>: the modified hash object.
263  
264 =back
265  
266 =head2 is_hash_ref
267  
268 The method to check if a reference object is for a hash or a class instance.
269  
270 =head3 Parameters
271  
272 =over
273  
274 =item *
275  
276 I<ref>: the reference object to check.
277  
278 =back
279  
280 =head3 Returns
281  
282 True, if the reference object is for a hash or a class instance. False, otherwise.
283  
284 =head1 LICENSE AND COPYRIGHT
285  
286 Copyright 2019 Google LLC
287  
288 Licensed under the Apache License, Version 2.0 (the "License");
289 you may not use this file except in compliance with the License.
290 You may obtain a copy of the License at
291  
292 http://www.apache.org/licenses/LICENSE-2.0
293  
294 Unless required by applicable law or agreed to in writing, software
295 distributed under the License is distributed on an "AS IS" BASIS,
296 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
297 See the License for the specific language governing permissions and
298 limitations under the License.
299  
300 =head1 REPOSITORY INFORMATION
301  
302 $Rev: $
303 $LastChangedBy: $
304 $Id: $
305  
306 =cut
« LongRunning/OperationService.pm ↑ Summary OAuth2ApplicationsHandler.pm »