File Coverage

blib/lib/BSON/Types.pm
Criterion Covered Total %
statement 116 117 99.1
branch 29 30 96.6
condition 2 2 100.0
subroutine 47 47 100.0
pod 18 18 100.0
total 212 214 99.0


line stmt bran cond sub pod time code
1 71     71   28313 use 5.010001;
  71         247  
2 71     71   425 use strict;
  71         173  
  71         1418  
3 71     71   1283 use warnings;
  71         611  
  71         2452  
4              
5             package BSON::Types;
6             # ABSTRACT: Helper functions to wrap BSON type classes
7              
8 71     71   329 use version;
  71         115  
  71         287  
9             our $VERSION = 'v1.12.2';
10              
11 71     71   4945 use base 'Exporter';
  71         146  
  71         13666  
12             our @EXPORT_OK = qw(
13             bson_bool
14             bson_bytes
15             bson_code
16             bson_dbref
17             bson_decimal128
18             bson_doc
19             bson_array
20             bson_double
21             bson_int32
22             bson_int64
23             bson_maxkey
24             bson_minkey
25             bson_oid
26             bson_raw
27             bson_regex
28             bson_string
29             bson_time
30             bson_timestamp
31             );
32             our %EXPORT_TAGS = ( 'all' => [ @EXPORT_OK ] );
33              
34 71     71   467 use Carp;
  71         127  
  71         5556  
35              
36 71     71   421 use boolean; # bson_bool
  71         11051  
  71         1094  
37 71     71   4597 use BSON::Bytes; # bson_bytes
  71         158  
  71         1945  
38 71     71   766 use BSON::Code; # bson_code
  71         188  
  71         2022  
39 71     71   701 use BSON::DBRef; # bson_dbref
  71         232  
  71         1958  
40 71     71   711 use BSON::Decimal128; # bson_decimal128
  71         260  
  71         2539  
41 71     71   883 use BSON::Doc; # bson_doc
  71         190  
  71         1835  
42 71     71   659 use BSON::Array; # bson_array
  71         171  
  71         1775  
43 71     71   663 use BSON::Double; # bson_double
  71         194  
  71         2223  
44 71     71   850 use BSON::Int32; # bson_int32
  71         203  
  71         2014  
45 71     71   776 use BSON::Int64; # bson_int64
  71         214  
  71         2427  
46 71     71   781 use BSON::MaxKey; # bson_maxkey
  71         176  
  71         1747  
47 71     71   1173 use BSON::MinKey; # bson_minkey
  71         160  
  71         1775  
48 71     71   427 use BSON::OID; # bson_oid
  71         123  
  71         1169  
49 71     71   568 use BSON::Raw; # bson_raw
  71         193  
  71         1959  
50 71     71   714 use BSON::Regex; # bson_regex
  71         204  
  71         2224  
51 71     71   798 use BSON::String; # bson_string
  71         207  
  71         2080  
52 71     71   756 use BSON::Time; # bson_time
  71         229  
  71         2285  
53 71     71   788 use BSON::Timestamp; # bson_timestamp
  71         193  
  71         2068  
54 71     71   731 use BSON::Symbol;
  71         204  
  71         1785  
55 71     71   709 use BSON::DBPointer;
  71         185  
  71         1986  
56              
57             # deprecated, but load anyway
58 71     71   689 use BSON::Bool;
  71         173  
  71         543  
59 71     71   5144 use BSON::Binary;
  71         170  
  71         1728  
60 71     71   659 use BSON::ObjectId;
  71         176  
  71         57695  
61              
62             #pod =func bson_bytes
63             #pod
64             #pod $bytes = bson_bytes( $byte_string );
65             #pod $bytes = bson_bytes( $byte_string, $subtype );
66             #pod
67             #pod This function returns a L object wrapping the provided string.
68             #pod A numeric subtype may be provided as a second argument, but this is not
69             #pod recommended for new applications.
70             #pod
71             #pod =cut
72              
73             sub bson_bytes {
74 7 100 100 7 1 2477 return BSON::Bytes->new(
75             data => ( defined( $_[0] ) ? $_[0] : '' ),
76             subtype => ( $_[1] || 0 ),
77             );
78             }
79              
80             #pod =func bson_code
81             #pod
82             #pod $code = bson_code( $javascript );
83             #pod $code = bson_code( $javascript, $hashref );
84             #pod
85             #pod This function returns a L object wrapping the provided Javascript
86             #pod code. An optional hashref representing variables in scope for the function
87             #pod may be given as well.
88             #pod
89             #pod =cut
90              
91             sub bson_code {
92 11 100   11 1 4557 return BSON::Code->new unless defined $_[0];
93 8 100       146 return BSON::Code->new( code => $_[0] ) unless defined $_[1];
94 4         103 return BSON::Code->new( code => $_[0], scope => $_[1] );
95             }
96              
97             #pod =func bson_dbref
98             #pod
99             #pod $dbref = bson_dbref( $object_id, $collection_name );
100             #pod
101             #pod This function returns a L object wrapping the provided Object ID
102             #pod and collection name.
103             #pod
104             #pod =cut
105              
106             sub bson_dbref {
107 4 100   4 1 2366 croak "Arguments to bson_dbref must an id and collection name"
108             unless @_ == 2;
109 1         9 return BSON::DBRef->new( id => $_[0], ref => $_[1] );
110             }
111              
112             #pod =func bson_decimal128
113             #pod
114             #pod $decimal = bson_decimal128( "0.12" );
115             #pod $decimal = bson_decimal128( "1.23456789101112131415116E-412" );
116             #pod
117             #pod This function returns a L object wrapping the provided
118             #pod decimal B. Unlike floating point values, this preserves exact
119             #pod decimal precision.
120             #pod
121             #pod =cut
122              
123             sub bson_decimal128 {
124 142 100   142 1 168835 return BSON::Decimal128->new( value => defined $_[0] ? $_[0] : 0 )
125             }
126              
127             #pod =func bson_doc
128             #pod
129             #pod $doc = bson_doc( first => "hello, second => "world" );
130             #pod
131             #pod This function returns a L object, which preserves the order
132             #pod of the provided key-value pairs.
133             #pod
134             #pod =cut
135              
136             sub bson_doc {
137 59     59 1 47828 return BSON::Doc->new( @_ );
138             }
139              
140             #pod =func bson_array
141             #pod
142             #pod $doc = bson_array(...);
143             #pod
144             #pod This function returns a L object, which preserves the order
145             #pod of the provided list of elements.
146             #pod
147             #pod =cut
148              
149             sub bson_array {
150 1     1 1 20 return BSON::Array->new( @_ );
151             }
152              
153             #pod =func bson_double
154             #pod
155             #pod $double = bson_double( 1.0 );
156             #pod
157             #pod This function returns a L object wrapping a native
158             #pod double value. This ensures it serializes to BSON as a double rather
159             #pod than a string or integer given Perl's lax typing for scalars.
160             #pod
161             #pod =cut
162              
163             sub bson_double {
164 14     14 1 7801 return BSON::Double->new( value => $_[0] )
165             }
166              
167             #pod =func bson_int32
168             #pod
169             #pod $int32 = bson_int32( 42 );
170             #pod
171             #pod This function returns a L object wrapping a native
172             #pod integer value. This ensures it serializes to BSON as an Int32 rather
173             #pod than a string or double given Perl's lax typing for scalars.
174             #pod
175             #pod =cut
176              
177             sub bson_int32 {
178 14 100   14 1 8360 return BSON::Int32->new unless defined $_[0];
179 13         291 return BSON::Int32->new( value => $_[0] )
180             }
181              
182             #pod =func bson_int64
183             #pod
184             #pod $int64 = bson_int64( 0 ); # 64-bit zero
185             #pod
186             #pod This function returns a L object, wrapping a native
187             #pod integer value. This ensures it serializes to BSON as an Int64 rather
188             #pod than a string or double given Perl's lax typing for scalars.
189             #pod
190             #pod =cut
191              
192             sub bson_int64 {
193 18 100   18 1 5128 return BSON::Int64->new unless defined $_[0];
194 17         390 return BSON::Int64->new( value => $_[0] )
195             }
196              
197             #pod =func bson_maxkey
198             #pod
199             #pod $maxkey = bson_maxkey();
200             #pod
201             #pod This function returns a singleton representing the "maximum key"
202             #pod BSON type.
203             #pod
204             #pod =cut
205              
206             sub bson_maxkey {
207 4     4 1 3096 return BSON::MaxKey->new;
208             }
209              
210             #pod =func bson_minkey
211             #pod
212             #pod $minkey = bson_minkey();
213             #pod
214             #pod This function returns a singleton representing the "minimum key"
215             #pod BSON type.
216             #pod
217             #pod =cut
218              
219             sub bson_minkey {
220 4     4 1 1263 return BSON::MinKey->new;
221             }
222              
223             #pod =func bson_oid
224             #pod
225             #pod $oid = bson_oid(); # generate a new one
226             #pod $oid = bson_oid( $bytes ); # from 12-byte packed OID
227             #pod $oid = bson_oid( $hex ); # from 24 hex characters
228             #pod
229             #pod This function returns a L object wrapping a 12-byte MongoDB Object
230             #pod ID. With no arguments, a new, unique Object ID is generated instead. If
231             #pod 24 hexadecimal characters are given, they will be packed into a 12-byte
232             #pod Object ID.
233             #pod
234             #pod =cut
235              
236             sub bson_oid {
237 11 100   11 1 150 return BSON::OID->new unless defined $_[0];
238 4 100       76 return BSON::OID->new( oid => $_[0] ) if length( $_[0] ) == 12;
239 1 50       28 return BSON::OID->new( oid => pack( "H*", $_[0] ) )
240             if $_[0] =~ m{\A[0-9a-f]{24}\z}i;
241 0         0 croak "Arguments to bson_oid must be 12 packed bytes or 24 bytes of hex";
242             }
243              
244             #pod =func bson_raw
245             #pod
246             #pod $raw = bson_raw( $bson_encoded );
247             #pod
248             #pod This function returns a L object wrapping an already BSON-encoded
249             #pod document.
250             #pod
251             #pod =cut
252              
253             sub bson_raw {
254 3     3 1 55 return BSON::Raw->new( bson => $_[0] );
255             }
256              
257             #pod =func bson_regex
258             #pod
259             #pod $regex = bson_regex( $pattern );
260             #pod $regex = bson_regex( $pattern, $flags );
261             #pod
262             #pod This function returns a L object wrapping a PCRE pattern and
263             #pod optional flags.
264             #pod
265             #pod =cut
266              
267             sub bson_regex {
268 9 100   9 1 3418 return BSON::Regex->new unless defined $_[0];
269 6 100       56 return BSON::Regex->new( pattern => $_[0] ) unless defined $_[1];
270 4         88 return BSON::Regex->new( pattern => $_[0], flags => $_[1] );
271             }
272              
273             #pod =func bson_string
274             #pod
275             #pod $string = bson_string( "08544" );
276             #pod
277             #pod This function returns a L object, wrapping a native
278             #pod string value. This ensures it serializes to BSON as a UTF-8 string rather
279             #pod than an integer or double given Perl's lax typing for scalars.
280             #pod
281             #pod =cut
282              
283             sub bson_string {
284 5     5 1 3097 return BSON::String->new( value => $_[0] );
285             }
286              
287             #pod =func bson_time
288             #pod
289             #pod $time = bson_time( $seconds_from_epoch );
290             #pod
291             #pod This function returns a L object representing a UTC date and
292             #pod time to millisecond precision. The argument must be given as a number of
293             #pod seconds relative to the Unix epoch (positive or negative). The number may
294             #pod be a floating point value for fractional seconds. If no argument is
295             #pod provided, the current time from L is used.
296             #pod
297             #pod =cut
298              
299             sub bson_time {
300 10 100   10 1 2538 return BSON::Time->new unless defined $_[0];
301             # Old constructor format handles floating point math right on
302             # 32-bit platforms.
303 5         102 return BSON::Time->new( $_[0] );
304             }
305              
306             #pod =func bson_timestamp
307             #pod
308             #pod $timestamp = bson_timestamp( $seconds_from_epoch, $increment );
309             #pod
310             #pod This function returns a L object. It is not recommended
311             #pod for general use.
312             #pod
313             #pod =cut
314              
315             sub bson_timestamp {
316 26 100   26 1 8920 return BSON::Timestamp->new unless defined $_[0];
317 23 100       81 return BSON::Timestamp->new( seconds => $_[0] ) unless defined $_[1];
318 21         899 return BSON::Timestamp->new( seconds => $_[0], increment => $_[1] );
319             }
320              
321             #pod =func bson_bool (DISCOURAGED)
322             #pod
323             #pod # for consistency with other helpers
324             #pod $bool = bson_bool( $expression );
325             #pod
326             #pod # preferred for efficiency
327             #pod use boolean;
328             #pod $bool = boolean( $expression );
329             #pod
330             #pod This function returns a L object (true or false) based on the
331             #pod provided expression (or false if no expression is provided). It is
332             #pod provided for consistency so that all BSON types have a corresponding helper
333             #pod function.
334             #pod
335             #pod For efficiency, use C directly, instead.
336             #pod
337             #pod =cut
338              
339             sub bson_bool {
340 6     6 1 2046 return boolean($_[0]);
341             }
342              
343             1;
344              
345             =pod
346              
347             =encoding UTF-8
348              
349             =head1 NAME
350              
351             BSON::Types - Helper functions to wrap BSON type classes
352              
353             =head1 VERSION
354              
355             version v1.12.2
356              
357             =head1 SYNOPSIS
358              
359             use BSON::Types ':all';
360              
361             $int32 = bson_int32(42);
362             $double = bson_double(3.14159);
363             $decimal = bson_decimal("24.01");
364             $time = bson_time(); # now
365             ...
366              
367             =head1 DESCRIPTION
368              
369             This module provides helper functions for BSON type wrappers. Type
370             wrappers use objects corresponding to BSON types to represent data that
371             would have ambiguous type or don't have a native Perl representation
372              
373             For example, because Perl scalars can represent strings, integers or
374             floating point numbers, the serialization rules depend on various
375             heuristics. By wrapping a Perl scalar with a class, such as
376             L, users can specify exactly how a scalar should serialize to
377             BSON.
378              
379             =head1 FUNCTIONS
380              
381             =head2 bson_bytes
382              
383             $bytes = bson_bytes( $byte_string );
384             $bytes = bson_bytes( $byte_string, $subtype );
385              
386             This function returns a L object wrapping the provided string.
387             A numeric subtype may be provided as a second argument, but this is not
388             recommended for new applications.
389              
390             =head2 bson_code
391              
392             $code = bson_code( $javascript );
393             $code = bson_code( $javascript, $hashref );
394              
395             This function returns a L object wrapping the provided Javascript
396             code. An optional hashref representing variables in scope for the function
397             may be given as well.
398              
399             =head2 bson_dbref
400              
401             $dbref = bson_dbref( $object_id, $collection_name );
402              
403             This function returns a L object wrapping the provided Object ID
404             and collection name.
405              
406             =head2 bson_decimal128
407              
408             $decimal = bson_decimal128( "0.12" );
409             $decimal = bson_decimal128( "1.23456789101112131415116E-412" );
410              
411             This function returns a L object wrapping the provided
412             decimal B. Unlike floating point values, this preserves exact
413             decimal precision.
414              
415             =head2 bson_doc
416              
417             $doc = bson_doc( first => "hello, second => "world" );
418              
419             This function returns a L object, which preserves the order
420             of the provided key-value pairs.
421              
422             =head2 bson_array
423              
424             $doc = bson_array(...);
425              
426             This function returns a L object, which preserves the order
427             of the provided list of elements.
428              
429             =head2 bson_double
430              
431             $double = bson_double( 1.0 );
432              
433             This function returns a L object wrapping a native
434             double value. This ensures it serializes to BSON as a double rather
435             than a string or integer given Perl's lax typing for scalars.
436              
437             =head2 bson_int32
438              
439             $int32 = bson_int32( 42 );
440              
441             This function returns a L object wrapping a native
442             integer value. This ensures it serializes to BSON as an Int32 rather
443             than a string or double given Perl's lax typing for scalars.
444              
445             =head2 bson_int64
446              
447             $int64 = bson_int64( 0 ); # 64-bit zero
448              
449             This function returns a L object, wrapping a native
450             integer value. This ensures it serializes to BSON as an Int64 rather
451             than a string or double given Perl's lax typing for scalars.
452              
453             =head2 bson_maxkey
454              
455             $maxkey = bson_maxkey();
456              
457             This function returns a singleton representing the "maximum key"
458             BSON type.
459              
460             =head2 bson_minkey
461              
462             $minkey = bson_minkey();
463              
464             This function returns a singleton representing the "minimum key"
465             BSON type.
466              
467             =head2 bson_oid
468              
469             $oid = bson_oid(); # generate a new one
470             $oid = bson_oid( $bytes ); # from 12-byte packed OID
471             $oid = bson_oid( $hex ); # from 24 hex characters
472              
473             This function returns a L object wrapping a 12-byte MongoDB Object
474             ID. With no arguments, a new, unique Object ID is generated instead. If
475             24 hexadecimal characters are given, they will be packed into a 12-byte
476             Object ID.
477              
478             =head2 bson_raw
479              
480             $raw = bson_raw( $bson_encoded );
481              
482             This function returns a L object wrapping an already BSON-encoded
483             document.
484              
485             =head2 bson_regex
486              
487             $regex = bson_regex( $pattern );
488             $regex = bson_regex( $pattern, $flags );
489              
490             This function returns a L object wrapping a PCRE pattern and
491             optional flags.
492              
493             =head2 bson_string
494              
495             $string = bson_string( "08544" );
496              
497             This function returns a L object, wrapping a native
498             string value. This ensures it serializes to BSON as a UTF-8 string rather
499             than an integer or double given Perl's lax typing for scalars.
500              
501             =head2 bson_time
502              
503             $time = bson_time( $seconds_from_epoch );
504              
505             This function returns a L object representing a UTC date and
506             time to millisecond precision. The argument must be given as a number of
507             seconds relative to the Unix epoch (positive or negative). The number may
508             be a floating point value for fractional seconds. If no argument is
509             provided, the current time from L is used.
510              
511             =head2 bson_timestamp
512              
513             $timestamp = bson_timestamp( $seconds_from_epoch, $increment );
514              
515             This function returns a L object. It is not recommended
516             for general use.
517              
518             =head2 bson_bool (DISCOURAGED)
519              
520             # for consistency with other helpers
521             $bool = bson_bool( $expression );
522              
523             # preferred for efficiency
524             use boolean;
525             $bool = boolean( $expression );
526              
527             This function returns a L object (true or false) based on the
528             provided expression (or false if no expression is provided). It is
529             provided for consistency so that all BSON types have a corresponding helper
530             function.
531              
532             For efficiency, use C directly, instead.
533              
534             =for Pod::Coverage BUILD
535              
536             =head1 AUTHORS
537              
538             =over 4
539              
540             =item *
541              
542             David Golden
543              
544             =item *
545              
546             Stefan G.
547              
548             =back
549              
550             =head1 COPYRIGHT AND LICENSE
551              
552             This software is Copyright (c) 2020 by Stefan G. and MongoDB, Inc.
553              
554             This is free software, licensed under:
555              
556             The Apache License, Version 2.0, January 2004
557              
558             =cut
559              
560             __END__