File Coverage

blib/lib/MongoDB/_Types.pm
Criterion Covered Total %
statement 27 27 100.0
branch n/a
condition n/a
subroutine 9 9 100.0
pod n/a
total 36 36 100.0


line stmt bran cond sub pod time code
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     70   422 use strict;
  70         124  
  70         2214  
16 70     70   357 use warnings;
  70         123  
  70         2140  
17             package MongoDB::_Types;
18              
19             # MongoDB type definitions
20              
21 70     70   696 use version;
  70         491  
  70         436  
22             our $VERSION = 'v2.2.2';
23              
24             use Type::Library
25 70         885 -base,
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     70   36835 );
  70         1670019  
74              
75 70     70   364057 use Type::Utils -all;
  70         292248  
  70         608  
76 70         757 use Types::Standard qw(
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     70   234052 );
  70         2973984  
91              
92 70     70   149674 use Scalar::Util qw/reftype/;
  70         178  
  70         3937  
93 70     70   23553 use boolean 0.25;
  70         55497  
  70         401  
94 70     70   28416 use MongoDB::_Constants;
  70         159  
  70         121745  
95             require Tie::IxHash;
96              
97             #--------------------------------------------------------------------------#
98             # Type declarations (without inherited coercions)
99             #--------------------------------------------------------------------------#
100              
101             declare Stringish, as Str|Overload['""'];
102              
103             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             declare Boolish, as Any;
110              
111             declare ArrayOfHashRef, as ArrayRef [HashRef];
112              
113             enum AuthMechanism,
114             [qw/NONE DEFAULT MONGODB-CR MONGODB-X509 GSSAPI PLAIN SCRAM-SHA-1 SCRAM-SHA-256/];
115              
116             duck_type BSONCodec, [ qw/encode_one decode_one/ ];
117              
118             class_type BSONDoc, { class => 'BSON::Doc' };
119              
120             class_type ClientSession, { class => 'MongoDB::ClientSession' };
121              
122             enum CompressionType, [qw/zlib zstd snappy/];
123              
124             declare ZlibCompressionLevel, as Int,
125             where { $_ >= -1 && $_ <= 9 },
126             message { "zlib compression value must be value from -1 to 9" };
127              
128             enum ConnectType, [qw/replicaSet direct none/];
129              
130             enum CursorType, [qw/non_tailable tailable tailable_await/];
131              
132             declare ErrorStr, as Stringish, where { defined($_) && length($_) }; # needs a true value
133              
134             declare HashLike, as Ref, where { reftype($_) eq 'HASH' };
135              
136             declare HeartbeatFreq, as Num,
137             where { defined($_) && $_ >= 500 },
138             message { "value must be at least 500" };
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             where { $_ =~ /^[^:]+:[0-9]+$/ and lc($_) eq $_ }, message {
144             "Address '$_' either not lowercased or not formatted as 'hostname:port'"
145             };
146              
147             declare HostAddressList, as ArrayRef [HostAddress], message {
148             "Address list <@$_> not all formatted as lowercased 'hostname:port' pairs"
149             };
150              
151             declare Intish, as Numish, where { defined $_ and $_ == int($_) };
152              
153             class_type IxHash, { class => 'Tie::IxHash' };
154              
155             declare MaybeHashRef, as Maybe[ HashRef ];
156              
157             class_type MongoDBClient, { class => 'MongoDB::MongoClient' };
158              
159             class_type MongoDBCollection, { class => 'MongoDB::Collection' };
160              
161             class_type MongoDBDatabase, { class => 'MongoDB::Database' };
162              
163             class_type BSONTimestamp, { class => 'BSON::Timestamp' };
164              
165             declare NonEmptyStr, as Stringish, where { defined $_ && length $_ };
166              
167             declare NonNegNum, as Numish,
168             where { defined($_) && $_ >= 0 },
169             message { "value must be a non-negative number" };
170              
171             declare MaxStalenessNum, as Numish,
172             where { defined($_) && ( $_ > 0 || $_ == -1 ) },
173             message { "value must be a positive number or -1" };
174              
175             declare OID, as Str, where { /\A[0-9a-f]{24}\z/ }, message {
176             "Value '$_' is not a valid OID"
177             };
178              
179             declare PairArrayRef, as ArrayRef,
180             where { @$_ % 2 == 0 };
181              
182             enum ReadPrefMode,
183             [qw/primary primaryPreferred secondary secondaryPreferred nearest/];
184              
185             class_type ReadPreference, { class => 'MongoDB::ReadPreference' };
186              
187             class_type ReadConcern, { class => 'MongoDB::ReadConcern' };
188              
189             class_type ServerDesc, { class => 'MongoDB::_Server' };
190              
191             enum ServerType,
192             [
193             qw/Standalone Mongos PossiblePrimary RSPrimary RSSecondary RSArbiter RSOther RSGhost Unknown/
194             ];
195              
196             declare SingleChar, as Str, where { length $_ eq 1 };
197              
198             declare SingleKeyHash, as HashRef, where { 1 == scalar keys %$_ };
199              
200             enum TopologyType,
201             [qw/Single ReplicaSetNoPrimary ReplicaSetWithPrimary Sharded Direct Unknown/];
202              
203             enum TransactionState,
204             [ TXN_NONE, TXN_STARTING, TXN_IN_PROGRESS, TXN_COMMITTED, TXN_ABORTED ];
205              
206             class_type WriteConcern, { class => 'MongoDB::WriteConcern' };
207              
208             # after SingleKeyHash, PairArrayRef and IxHash
209             declare OrderedDoc, as BSONDoc|PairArrayRef|IxHash|SingleKeyHash;
210             declare Document, as HashRef|BSONDoc|PairArrayRef|IxHash|HashLike;
211              
212             # after NonEmptyStr
213             declare DBRefColl, as NonEmptyStr;
214             declare DBRefDB, as NonEmptyStr|Undef;
215              
216             # after OrderedDoc
217             declare IndexModel, as Dict [ keys => OrderedDoc, options => Optional [HashRef] ];
218             declare IndexModelList, as ArrayRef [IndexModel];
219              
220             #--------------------------------------------------------------------------#
221             # Coercions
222             #--------------------------------------------------------------------------#
223              
224             coerce ArrayOfHashRef, from HashRef, via { [$_] };
225              
226             coerce BSONCodec, from HashRef,
227             via { require BSON; BSON->new($_) };
228              
229             coerce Boolish, from Any, via { !!$_ };
230              
231             coerce DBRefColl, from MongoDBCollection, via { $_->name };
232              
233             coerce DBRefDB, from MongoDBDatabase, via { $_->name };
234              
235             coerce ErrorStr, from Str, via { $_ || "unspecified error" };
236              
237             coerce ReadPrefMode, from Str, via { $_ = lc $_; s/_?preferred/Preferred/; $_ };
238              
239             coerce IxHash, from HashRef, via { Tie::IxHash->new(%$_) };
240              
241             coerce IxHash, from ArrayRef, via { Tie::IxHash->new(@$_) };
242              
243             coerce IxHash, from HashLike, via { Tie::IxHash->new(%$_) };
244              
245             coerce IxHash, from BSONDoc, via { Tie::IxHash->new(@$_) };
246              
247             coerce OID, from Str, via { lc $_ };
248              
249             coerce ReadPreference, from HashRef,
250             via { require MongoDB::ReadPreference; MongoDB::ReadPreference->new($_) };
251              
252             coerce ReadPreference, from Str,
253             via { require MongoDB::ReadPreference; MongoDB::ReadPreference->new( mode => $_ ) };
254              
255             coerce ReadPreference, from ArrayRef,
256             via { require MongoDB::ReadPreference; MongoDB::ReadPreference->new( mode => $_->[0], tag_sets => $_->[1] ) };
257              
258             coerce ReadConcern, from Str,
259             via { require MongoDB::ReadConcern; MongoDB::ReadConcern->new( level => $_ ) };
260              
261             coerce ReadConcern, from HashRef,
262             via { require MongoDB::ReadConcern; MongoDB::ReadConcern->new($_) };
263              
264             coerce WriteConcern, from HashRef,
265             via { require MongoDB::WriteConcern; MongoDB::WriteConcern->new($_) };
266              
267             1;