MongoDB/_Types.pm

filter: Statementstmt 78.5% 110 / 140 Branchbran 38.8% 7 / 18 Conditioncond 83.3% 5 / 6 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 48.9% 23 / 47 Podpod n/a totaltotal 68.6% 147 / 214 CC 11 SCAR 5.0
CC
11
Cov
74%
__ANON__
3.7
__ANON__
3.7
__ANON__
3.1
CRAP 1.6
« MongoDB/Op/_FSyncUnlock.pm ↑ Summary MongoDB.pm »
1 # Copyright 2014 - present MongoDB, 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  
15 70 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 70 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 package MongoDB::_Types;
18  
19 # MongoDB type definitions
20  
21 70 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 70 our $VERSION = 'v2.2.2';
23  
24 use Type::Library
25 70 ▶ -base,
Subroutine BEGIN: called CC=1 SCAR=0.0
26 -declare => qw(
27 ArrayOfHashRef
28 AuthMechanism
29 Boolish
30 Booleanpm
31 BSONCodec
32 BSONDoc
33 ClientSession
34 CompressionType
35 ZlibCompressionLevel
36 ConnectType
37 CursorType
38 DBRefColl
39 DBRefDB
40 Document
41 ErrorStr
42 HashLike
43 HeartbeatFreq
44 HostAddress
45 HostAddressList
46 Intish
47 IndexModel
48 IndexModelList
49 IxHash
50 MaxStalenessNum
51 MaybeHashRef
52 MongoDBClient
53 MongoDBCollection
54 MongoDBDatabase
55 BSONTimestamp
56 NonEmptyStr
57 NonNegNum
58 Numish
59 OID
60 OrderedDoc
61 PairArrayRef
62 ReadPrefMode
63 ReadConcern
64 ReadPreference
65 ServerDesc
66 ServerType
67 SingleChar
68 SingleKeyHash
69 Stringish
70 TopologyType
71 TransactionState
72 WriteConcern
73 70 );
74  
75 70 ▶use Type::Utils -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 70 ▶use Types::Standard qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
77 Any
78 Bool
79 ArrayRef
80 Dict
81 HashRef
82 Int
83 Maybe
84 Num
85 Optional
86 Overload
87 Ref
88 Str
89 Undef
90 70 );
91  
92 70 ▶use Scalar::Util qw/reftype/;
Subroutine BEGIN: called CC=1 SCAR=0.0
93 70 ▶use boolean 0.25;
Subroutine BEGIN: called CC=1 SCAR=0.0
94 70 ▶use MongoDB::_Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
95 70 require Tie::IxHash;
96  
97 #--------------------------------------------------------------------------#
98 # Type declarations (without inherited coercions)
99 #--------------------------------------------------------------------------#
100  
101 70 declare Stringish, as Str|Overload['""'];
102  
103 70 declare Numish, as Num|Overload['0+'];
104  
105 # Types::Standard::Bool is overly restrictive, not allowing objects that
106 # overload boolification, and Overload['bool'] doesn't detect objects that
107 # overload via fallback, so we use this type for documentation purposes,
108 # but allow any actual type.
109 70 declare Boolish, as Any;
110  
111 70 declare ArrayOfHashRef, as ArrayRef [HashRef];
112  
113 70 enum AuthMechanism,
114 [qw/NONE DEFAULT MONGODB-CR MONGODB-X509 GSSAPI PLAIN SCRAM-SHA-1 SCRAM-SHA-256/];
115  
116 70 duck_type BSONCodec, [ qw/encode_one decode_one/ ];
117  
118 70 class_type BSONDoc, { class => 'BSON::Doc' };
119  
120 70 class_type ClientSession, { class => 'MongoDB::ClientSession' };
121  
122 70 enum CompressionType, [qw/zlib zstd snappy/];
123  
124 declare ZlibCompressionLevel, as Int,
125 505 ▶ where { $_ >= -1 && $_ <= 9 },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_ >= -1 and $_ <= 9 505 0 505
126 0 ▶ message { "zlib compression value must be value from -1 to 9" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
127  
128 70 enum ConnectType, [qw/replicaSet direct none/];
129  
130 70 enum CursorType, [qw/non_tailable tailable tailable_await/];
131  
132 300 ▶declare ErrorStr, as Stringish, where { defined($_) && length($_) }; # needs a true value
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and length $_ 300 0 300
133  
134 0 ▶declare HashLike, as Ref, where { reftype($_) eq 'HASH' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
135  
136 declare HeartbeatFreq, as Num,
137 189 ▶ where { defined($_) && $_ >= 500 },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and $_ >= 500 189 0 189
138 2 ▶ message { "value must be at least 500" };
Subroutine __ANON__: called CC=1 SCAR=0.0
139  
140 # XXX loose address validation for now. Host part should really be hostname or
141 # IPv4/IPv6 literals
142 declare HostAddress, as Stringish,
143 1194 ▶ where { $_ =~ /^[^:]+:[0-9]+$/ and lc($_) eq $_ }, message {
Subroutine __ANON__: called CC=2 SCAR=7.6
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /^[^:]+:[0-9]+$/ and lc $_ eq $_ 1194 0 1194
144 0 "Address '$_' either not lowercased or not formatted as 'hostname:port'"
145 70 };
146  
147 0 ▶declare HostAddressList, as ArrayRef [HostAddress], message {
Subroutine __ANON__: not called CC=1 SCAR=6.9
148 0 "Address list <@$_> not all formatted as lowercased 'hostname:port' pairs"
149 70 };
150  
151 0 ▶declare Intish, as Numish, where { defined $_ and $_ == int($_) };
Subroutine __ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and $_ == int $_ 0 0 0
152  
153 70 class_type IxHash, { class => 'Tie::IxHash' };
154  
155 70 declare MaybeHashRef, as Maybe[ HashRef ];
156  
157 70 class_type MongoDBClient, { class => 'MongoDB::MongoClient' };
158  
159 70 class_type MongoDBCollection, { class => 'MongoDB::Collection' };
160  
161 70 class_type MongoDBDatabase, { class => 'MongoDB::Database' };
162  
163 70 class_type BSONTimestamp, { class => 'BSON::Timestamp' };
164  
165 32 ▶declare NonEmptyStr, as Stringish, where { defined $_ && length $_ };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and length $_ 32 0 32
166  
167 declare NonNegNum, as Numish,
168 1802 ▶ where { defined($_) && $_ >= 0 },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and $_ >= 0 1802 0 1802
169 2 ▶ message { "value must be a non-negative number" };
Subroutine __ANON__: called CC=1 SCAR=0.0
170  
171 declare MaxStalenessNum, as Numish,
172 698 ▶ where { defined($_) && ( $_ > 0 || $_ == -1 ) },
Subroutine __ANON__: called CC=3 SCAR=11.3
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and $_ > 0 || $_ == -1 698 0 698
Condition83%5/6
defined $_ && ($_ > 0 || $_ == -1)
!ll&&!rl&&r
02696
$_ > 0 or $_ == -1
l!l&&r!l&&!r
456512
MC/DC66%2/3
defined $_ && ($_ > 0 || $_ == -1)
defined $_ $_ > 0 $_ == -1
Truth table75%3/4
A && B or C
A= defined $_B= $_ > 0C= $_ == -1
ABCresult
0XX0
1000
11X1
1011
173 2 ▶ message { "value must be a positive number or -1" };
Subroutine __ANON__: called CC=1 SCAR=0.0
174  
175 0 ▶declare OID, as Str, where { /\A[0-9a-f]{24}\z/ }, message {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
176 0 "Value '$_' is not a valid OID"
177 70 };
178  
179 declare PairArrayRef, as ArrayRef,
180 0 ▶ where { @$_ % 2 == 0 };
Subroutine __ANON__: not called CC=1 SCAR=1.2
181  
182 70 enum ReadPrefMode,
183 [qw/primary primaryPreferred secondary secondaryPreferred nearest/];
184  
185 70 class_type ReadPreference, { class => 'MongoDB::ReadPreference' };
186  
187 70 class_type ReadConcern, { class => 'MongoDB::ReadConcern' };
188  
189 70 class_type ServerDesc, { class => 'MongoDB::_Server' };
190  
191 70 enum ServerType,
192 [
193 qw/Standalone Mongos PossiblePrimary RSPrimary RSSecondary RSArbiter RSOther RSGhost Unknown/
194 ];
195  
196 0 ▶declare SingleChar, as Str, where { length $_ eq 1 };
Subroutine __ANON__: not called CC=1 SCAR=1.2
197  
198 0 ▶declare SingleKeyHash, as HashRef, where { 1 == scalar keys %$_ };
Subroutine __ANON__: not called CC=1 SCAR=1.2
199  
200 70 enum TopologyType,
201 [qw/Single ReplicaSetNoPrimary ReplicaSetWithPrimary Sharded Direct Unknown/];
202  
203 70 enum TransactionState,
204 [ TXN_NONE, TXN_STARTING, TXN_IN_PROGRESS, TXN_COMMITTED, TXN_ABORTED ];
205  
206 70 class_type WriteConcern, { class => 'MongoDB::WriteConcern' };
207  
208 # after SingleKeyHash, PairArrayRef and IxHash
209 70 declare OrderedDoc, as BSONDoc|PairArrayRef|IxHash|SingleKeyHash;
210 70 declare Document, as HashRef|BSONDoc|PairArrayRef|IxHash|HashLike;
211  
212 # after NonEmptyStr
213 70 declare DBRefColl, as NonEmptyStr;
214 70 declare DBRefDB, as NonEmptyStr|Undef;
215  
216 # after OrderedDoc
217 70 declare IndexModel, as Dict [ keys => OrderedDoc, options => Optional [HashRef] ];
218 70 declare IndexModelList, as ArrayRef [IndexModel];
219  
220 #--------------------------------------------------------------------------#
221 # Coercions
222 #--------------------------------------------------------------------------#
223  
224 3 ▶coerce ArrayOfHashRef, from HashRef, via { [$_] };
Subroutine __ANON__: called CC=1 SCAR=0.0
225  
226 coerce BSONCodec, from HashRef,
227 1 ▶ via { require BSON; BSON->new($_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
228  
229 0 ▶coerce Boolish, from Any, via { !!$_ };
Subroutine __ANON__: not called CC=1 SCAR=1.2
230  
231 0 ▶coerce DBRefColl, from MongoDBCollection, via { $_->name };
Subroutine __ANON__: not called CC=1 SCAR=1.2
232  
233 0 ▶coerce DBRefDB, from MongoDBDatabase, via { $_->name };
Subroutine __ANON__: not called CC=1 SCAR=1.2
234  
235 0 ▶coerce ErrorStr, from Str, via { $_ || "unspecified error" };
Subroutine __ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
BranchTrueFalseTotal
$_ or "unspecified error" 0 0 0
236  
237 72 ▶coerce ReadPrefMode, from Str, via { $_ = lc $_; s/_?preferred/Preferred/; $_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
238  
239 0 ▶coerce IxHash, from HashRef, via { Tie::IxHash->new(%$_) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
240  
241 1 ▶coerce IxHash, from ArrayRef, via { Tie::IxHash->new(@$_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
242  
243 0 ▶coerce IxHash, from HashLike, via { Tie::IxHash->new(%$_) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
244  
245 0 ▶coerce IxHash, from BSONDoc, via { Tie::IxHash->new(@$_) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
246  
247 0 ▶coerce OID, from Str, via { lc $_ };
Subroutine __ANON__: not called CC=1 SCAR=1.2
248  
249 coerce ReadPreference, from HashRef,
250 0 ▶ via { require MongoDB::ReadPreference; MongoDB::ReadPreference->new($_) };
Subroutine __ANON__: not called CC=1 SCAR=2.6
251  
252 coerce ReadPreference, from Str,
253 0 ▶ via { require MongoDB::ReadPreference; MongoDB::ReadPreference->new( mode => $_ ) };
Subroutine __ANON__: not called CC=1 SCAR=2.6
254  
255 coerce ReadPreference, from ArrayRef,
256 0 ▶ via { require MongoDB::ReadPreference; MongoDB::ReadPreference->new( mode => $_->[0], tag_sets => $_->[1] ) };
Subroutine __ANON__: not called CC=1 SCAR=2.6
257  
258 coerce ReadConcern, from Str,
259 0 ▶ via { require MongoDB::ReadConcern; MongoDB::ReadConcern->new( level => $_ ) };
Subroutine __ANON__: not called CC=1 SCAR=2.6
260  
261 coerce ReadConcern, from HashRef,
262 0 ▶ via { require MongoDB::ReadConcern; MongoDB::ReadConcern->new($_) };
Subroutine __ANON__: not called CC=1 SCAR=2.6
263  
264 coerce WriteConcern, from HashRef,
265 0 ▶ via { require MongoDB::WriteConcern; MongoDB::WriteConcern->new($_) };
Subroutine __ANON__: not called CC=1 SCAR=2.6
266  
267 70 1;
« MongoDB/Op/_FSyncUnlock.pm ↑ Summary MongoDB.pm »