RestApi/SheetsApi4/Types.pm

filter: Statementstmt 84.9% 90 / 106 Branchbran 87.5% 7 / 8 Conditioncond 77.7% 7 / 9 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 72.0% 31 / 43 Podpod n/a totaltotal 80.7% 138 / 171 CC 8 SCAR 6.4
CC
8
Cov
85%
_col_i2a
5.2
__ANON__
4.0
__ANON__
2.0
CRAP 1.9
« RestApi/SheetsApi4/RangeGroup/Tie.pm ↑ Summary RestApi/DriveApi3.pm »
1 package Google::RestApi::SheetsApi4::Types;
2  
3 # custom type constrants. see Type::Library.
4 # NOTE: can't use Google::RestApi::Setup here because that module imports this one.
5  
6 # this handles the complex coercions of different formats for specifying ranges
7 # using type::library to handle it. having different formats for ranges is probably
8 # more comlicated that it's worth, it grew in complexity organically, but it works
9 # and we have it now.
10  
11 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 our $VERSION = '2.2.5';
15  
16 2 ▶use feature qw( state );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 ▶use Type::Params qw( signature );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Types::Standard qw( Undef Defined Value Str StrMatch Int ArrayRef HashRef Tuple Dict HasMethods );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Types::Common::Numeric qw( PositiveInt PositiveOrZeroInt );
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 2 ▶use Google::RestApi::Types qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 2 my @types = qw(
25 DimCol DimRow DimColRow DimAll
26 RangeCol RangeRow RangeCell RangeAny RangeAll
27 RangeNamed RangeIndex
28 HasRange
29 );
30  
31 2 ▶use Type::Library -base, -declare => @types;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 2 our %EXPORT_TAGS = (all => \@types);
35  
36 2 my $meta = __PACKAGE__->meta;
37  
38  
39 my $dim_col = $meta->add_type(
40 name => 'DimCol',
41 parent => StrMatch[qr/^(col)/i],
42 0 ▶ message => sub { "Must be spreadsheet dimension 'col'" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
43 2 );
44  
45 my $dim_row = $meta->add_type(
46 name => 'DimRow',
47 parent => StrMatch[qr/^(row)/i],
48 0 ▶ message => sub { "Must be spreadsheet dimension 'row'" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 2 );
50  
51 my $dim_all = $meta->add_type(
52 name => 'DimAll',
53 parent => StrMatch[qr/^(all)/i],
54 0 ▶ message => sub { "Must be spreadsheet dimension 'all'" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
55 2 );
56  
57 $meta->add_type(
58 name => 'DimColRow',
59 parent => $dim_col | $dim_row,
60 0 ▶ message => sub { "Must be a spreadsheet dimension (col or row)" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
61 2 );
62  
63 $_->coercion->add_type_coercions(
64 0 ▶ Str, sub { lc(substr($_, 0, 3)); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
65 2 ) for ($dim_col, $dim_row, $dim_all);
66  
67  
68  
69  
70 2 my $col_str_int = StrMatch[qr/^([A-Z]+|\d+)$/];
71  
72 my $col = $meta->add_type(
73 name => 'RangeCol',
74 parent => StrMatch[qr/^([A-Z]+)\d*:\1\d*$/],
75 119 ▶ message => sub { "Must be a spreadsheet range column Ax:Ay" },
Subroutine __ANON__: called CC=1 SCAR=0.0
76 2 );
77 $col->coercion->add_type_coercions(
78 21 ▶ StrMatch[qr/^([A-Z]+)$/], sub { "$_:$_"; }, # 'A' => 'A:A', 1 should be a row.
Subroutine __ANON__: called CC=1 SCAR=0.0
79 10 ▶ Dict[col => $col_str_int], sub { $_ = _col_i2a($_->{col}); "$_:$_"; },
Subroutine __ANON__: called CC=1 SCAR=0.0
80 4 ▶ Tuple[Dict[col => $col_str_int]], sub { $_ = _col_i2a($_->[0]->{col}); "$_:$_"; },
Subroutine __ANON__: called CC=1 SCAR=0.0
81 11 ▶ Tuple[$col_str_int], sub { $_ = _col_i2a($_->[0]); "$_:$_"; },
Subroutine __ANON__: called CC=1 SCAR=0.0
82 3 ▶ Tuple[$col_str_int, False], sub { $_ = _col_i2a($_->[0]); "$_:$_"; },
Subroutine __ANON__: called CC=1 SCAR=0.0
83 4 ▶ Tuple[Tuple[$col_str_int]], sub { $_ = _col_i2a($_->[0]->[0]); "$_:$_"; },
Subroutine __ANON__: called CC=1 SCAR=0.0
84 3 ▶ Tuple[Tuple[$col_str_int, False]], sub { $_ = _col_i2a($_->[0]->[0]); "$_:$_"; },
Subroutine __ANON__: called CC=1 SCAR=0.0
85 2 );
86 108 ▶sub _col_i2a {
Subroutine _col_i2a: called CC=5 SCAR=16.5
87 108 my $col = shift;
88 108 ▶ return '' if !defined $col || $col eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $col or $col eq "" 0 108 108
Condition33%1/3
not defined $col or $col eq ""
l!l&&r!l&&!r
00108
MC/DC0%0/2
not defined $col or $col eq ""
not defined $col $col eq ""
Truth table33%1/3
not(A) or B
A= not defined $colB= $col eq ""
ABresult
1X1
011
000
89 108 ▶ return $col if $col =~ qr/\D/;
Branch: 1 branch
BranchTrueFalseTotal
if $col =~ qr/\D/ 41 67 108
90 67 my $l = int($col / 27);
91 67 my $r = $col - $l * 26;
92 67 ▶ return $l > 0 ? (pack 'CC', $l+64, $r+64) : (pack 'C', $r+64);
Branch: 1 branch
BranchTrueFalseTotal
$l > 0 ? : 2 65 67
93 }
94  
95  
96 my $row = $meta->add_type(
97 name => 'RangeRow',
98 parent => StrMatch[qr/^[A-Z]*(\d+):[A-Z]*\1$/],
99 109 ▶ message => sub { "Must be a spreadsheet range row x1:y1" },
Subroutine __ANON__: called CC=1 SCAR=0.0
100 2 );
101 $row->coercion->add_type_coercions(
102 20 ▶ PositiveInt, sub { "$_:$_"; }, # 1 => 1:1
Subroutine __ANON__: called CC=1 SCAR=0.0
103 6 ▶ Dict[row => PositiveInt], sub { "$_->{row}:$_->{row}"; },
Subroutine __ANON__: called CC=1 SCAR=0.0
104 2 ▶ Tuple[Dict[row => PositiveInt]], sub { "$_->[0]->{row}:$_->[0]->{row}"; },
Subroutine __ANON__: called CC=1 SCAR=0.0
105 10 ▶ Tuple[False, PositiveInt] => sub { "$_->[1]:$_->[1]"; },
Subroutine __ANON__: called CC=1 SCAR=0.0
106 6 ▶ Tuple[Tuple[False, PositiveInt]] => sub { "$_->[0]->[1]:$_->[0]->[1]"; },
Subroutine __ANON__: called CC=1 SCAR=0.0
107 2 );
108  
109  
110  
111 2 my $cell_str_int = StrMatch[qr/^[A-Z]+\d+$/];
112  
113 my $cell = $meta->add_type(
114 name => 'RangeCell',
115 parent => $cell_str_int,
116 59 ▶ message => sub { "Must be a spreadsheet range cell A1" },
Subroutine __ANON__: called CC=1 SCAR=0.0
117 2 );
118 $cell->coercion->add_type_coercions(
119 2 ▶ StrMatch[qr/^([A-Z]+\d+):\1$/], sub { (split(':'))[0]; }, # 'A1:A1' should be a cell.
Subroutine __ANON__: called CC=1 SCAR=0.0
120 28 ▶ Dict[col => $col_str_int, row => PositiveInt], sub { _col_i2a($_->{col}) . $_->{row}; },
Subroutine __ANON__: called CC=1 SCAR=0.0
121 4 ▶ Tuple[Dict[col => $col_str_int, row => PositiveInt]], sub { _col_i2a($_->[0]->{col}) . $_->[0]->{row}; },
Subroutine __ANON__: called CC=1 SCAR=0.0
122 37 ▶ Tuple[$col_str_int, PositiveInt], sub { _col_i2a($_->[0]) . $_->[1]; },
Subroutine __ANON__: called CC=1 SCAR=0.0
123 4 ▶ Tuple[Tuple[$col_str_int, PositiveInt]], sub { _col_i2a($_->[0]->[0]) . $_->[0]->[1]; },
Subroutine __ANON__: called CC=1 SCAR=0.0
124 2 );
125  
126  
127 my $range_any = $meta->add_type(
128 name => 'RangeAny',
129 parent => StrMatch[qr/^[A-Z]*\d*(:[A-Z]*\d*)?$/],
130 0 ▶ message => sub { "Must be a spreadsheet range A1:B2" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
131 2 );
132 # drive the coercions on each type by running them through compile/check.
133 $range_any->coercion->add_type_coercions(
134 Tuple[Defined, Defined],
135 23 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
136 23 state $check = signature(positional => [Tuple[$col | $row | $cell, $col | $row | $cell]]);
137 23 my ($range) = $check->($_);
138  
139 # these look odd but if 'A' is passed as one of the tuples, it will be
140 # translated to 'A:A' for that one tuple by col coercions above,
141 # so have to squash it here. same goes for row.
142 23 ($range->[0]) = (split(':', $range->[0]))[0];
143 23 ($range->[1]) = (split(':', $range->[1]))[0];
144
145 # if this is 'A1:A1' squash it to 'A1'.
146 23 ▶ return $range->[0] if
Branch: 1 branch
BranchTrueFalseTotal
if $$range[0] =~ qr/^[A-Z]+\d+/ and $$range[1] =~ qr/^[A-Z]+\d+/ and $$range[0] eq $$range[1] 2 21 23
Condition100%6/6
$$range[0] =~ qr/^[A-Z]+\d+/ and $$range[1] =~ qr/^[A-Z]+\d+/ and $$range[0] eq $$range[1]
!ll&&!rl&&r
1472
$$range[0] =~ qr/^[A-Z]+\d+/ and $$range[1] =~ qr/^[A-Z]+\d+/
!ll&&!rl&&r
4109
MC/DC100%3/3
$$range[0] =~ qr/^[A-Z]+\d+/ and $$range[1] =~ qr/^[A-Z]+\d+/ and $$range[0] eq $$range[1]
$$range[0] =~ qr/^[A-Z]+\d+/ $$range[1] =~ qr/^[A-Z]+\d+/ $$range[0] eq $$range[1]
Truth table100%4/4
A and B and C
A= $$range[0] =~ qr/^[A-Z]+\d+/B= $$range[1] =~ qr/^[A-Z]+\d+/C= $$range[0] eq $$range[1]
ABCresult
0XX0
10X0
1100
1111
147 $range->[0] =~ qr/^[A-Z]+\d+/ &&
148 $range->[1] =~ qr/^[A-Z]+\d+/ &&
149 $range->[0] eq $range->[1];
150  
151 21 return "$range->[0]:$range->[1]";
152 },
153 Tuple[Defined],
154 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
155 0 state $check = signature(positional => [Tuple[$col | $row | $cell]]);
156 0 my ($range) = $check->($_);
157 0 return $range;
158 },
159 Value,
160 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
161 0 state $check = signature(positional => [$col | $row | $cell]);
162 0 my ($range) = $check->($_);
163 0 return $range;
164 },
165 2 );
166  
167  
168  
169 $meta->add_type(
170 name => 'RangeAll',
171 parent => $col | $row | $cell | $range_any,
172 0 ▶ message => sub { "Must be a spreadsheet range, col, row, or cell" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
173 2 );
174  
175  
176  
177 # https://support.google.com/docs/answer/63175?co=GENIE.Platform%3DDesktop&hl=en
178 $meta->add_type(
179 name => 'RangeNamed',
180 parent => StrMatch[qr/^[A-Za-z_][A-Za-z0-9_]+/],
181 0 ▶ message => sub { "Must be a spreadsheet named range" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
182 2 );
183  
184 $meta->add_type(
185 name => 'RangeIndex',
186 parent => PositiveOrZeroInt,
187 0 ▶ message => sub { "Must be a spreadsheet range index (0-based)" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
188 2 );
189  
190  
191 $meta->add_type(
192 name => 'HasRange',
193 parent => HasMethods[qw(range)],
194 0 ▶ message => sub { "Must be a range object"; }
Subroutine __ANON__: not called CC=1 SCAR=6.9
195 2 );
196  
197 2 __PACKAGE__->make_immutable;
198  
199 2 1;
« RestApi/SheetsApi4/RangeGroup/Tie.pm ↑ Summary RestApi/DriveApi3.pm »