BSON/Bytes.pm

filter: Statementstmt 100% 40 / 40 Branchbran 100% 6 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 50.0% 1 / 2 totaltotal 98.3% 59 / 60 CC 4 SCAR 4.3
CC
4
Cov
100%
BUILD
3.0
TO_JSON
2.0
BEGIN
1.0
CRAP 1.5
« BSON/Binary.pm ↑ Summary BSON/Raw.pm »
1 71 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 71 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 71 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package BSON::Bytes;
6 # ABSTRACT: BSON type wrapper for binary byte strings
7  
8 71 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 71 our $VERSION = 'v1.12.2';
10  
11 71 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 71 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 71 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 #pod =attr data
17 #pod
18 #pod A scalar, interpreted as bytes. (I.e. "character" data should be encoded
19 #pod to bytes.) It defaults to the empty string.
20 #pod
21 #pod =attr subtype
22 #pod
23 #pod A numeric BSON subtype between 0 and 255. This defaults to 0 and generally
24 #pod should not be modified. Subtypes 128 to 255 are "user-defined".
25 #pod
26 #pod =cut
27  
28 71 has [qw/data subtype/] => (
29 is => 'ro',
30 );
31  
32 71 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 17634 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.0
Pod undocumented
35 17634 my ($self) = @_;
36 17634 ▶ $self->{data} = '' unless defined $self->{data};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"data"} 8904 8730 17634
37 17634 ▶ $self->{subtype} = 0 unless defined $self->{subtype};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"subtype"} 8904 8730 17634
38 }
39  
40 #pod =method TO_JSON
41 #pod
42 #pod Returns Base64 encoded string equivalent to the data attribute.
43 #pod
44 #pod If the C<BSON_EXTJSON> option is true, it will instead be compatible with
45 #pod MongoDB's L<extended JSON|https://github.com/mongodb/specifications/blob/master/source/extended-json.rst>
46 #pod format, which represents it as a document as follows:
47 #pod
48 #pod {"$binary" : { "base64": "<base64 data>", "subType" : "<type>"} }
49 #pod
50 #pod =cut
51  
52 26 ▶sub TO_JSON {
Subroutine TO_JSON: called CC=2 SCAR=6.9
Pod documented
53 26 ▶ return MIME::Base64::encode_base64($_[0]->{data}, "") unless $ENV{BSON_EXTJSON};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"BSON_EXTJSON"} 1 25 26
54  
55 25 my %data;
56 25 tie( %data, 'Tie::IxHash' );
57 25 $data{base64} = MIME::Base64::encode_base64($_[0]->{data}, "");
58 25 $data{subType} = sprintf("%02x",$_[0]->{subtype});
59  
60 return {
61 25 '$binary' => \%data,
62 };
63 }
64  
65 71 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
66 17349 ▶ q{""} => sub { $_[0]->{data} },
Subroutine __ANON__: called CC=1 SCAR=0.0
67 71 fallback => 1,
68 71 );
69  
70 # backwards compatibility alias
71 71 *type = \&subtype;
72  
73 71 1;
74  
75 =pod
76  
77 =encoding UTF-8
78  
79 =head1 NAME
80  
81 BSON::Bytes - BSON type wrapper for binary byte strings
82  
83 =head1 VERSION
84  
85 version v1.12.2
86  
87 =head1 SYNOPSIS
88  
89 use BSON::Types ':all';
90  
91 $bytes = bson_bytes( $bytestring );
92 $bytes = bson_bytes( $bytestring, $subtype );
93  
94 =head1 DESCRIPTION
95  
96 This module provides a BSON type wrapper for binary data represented
97 as a string of bytes.
98  
99 =head1 ATTRIBUTES
100  
101 =head2 data
102  
103 A scalar, interpreted as bytes. (I.e. "character" data should be encoded
104 to bytes.) It defaults to the empty string.
105  
106 =head2 subtype
107  
108 A numeric BSON subtype between 0 and 255. This defaults to 0 and generally
109 should not be modified. Subtypes 128 to 255 are "user-defined".
110  
111 =head1 METHODS
112  
113 =head2 TO_JSON
114  
115 Returns Base64 encoded string equivalent to the data attribute.
116  
117 If the C<BSON_EXTJSON> option is true, it will instead be compatible with
118 MongoDB's L<extended JSON|https://github.com/mongodb/specifications/blob/master/source/extended-json.rst>
119 format, which represents it as a document as follows:
120  
121 {"$binary" : { "base64": "<base64 data>", "subType" : "<type>"} }
122  
123 =for Pod::Coverage BUILD type
124  
125 =head1 OVERLOADING
126  
127 The stringification operator (C<"">) is overloaded to return the binary data
128 and fallback overloading is enabled.
129  
130 =head1 AUTHORS
131  
132 =over 4
133  
134 =item *
135  
136 David Golden <david@mongodb.com>
137  
138 =item *
139  
140 Stefan G. <minimalist@lavabit.com>
141  
142 =back
143  
144 =head1 COPYRIGHT AND LICENSE
145  
146 This software is Copyright (c) 2020 by Stefan G. and MongoDB, Inc.
147  
148 This is free software, licensed under:
149  
150 The Apache License, Version 2.0, January 2004
151  
152 =cut
153  
154 __END__
« BSON/Binary.pm ↑ Summary BSON/Raw.pm »