File Coverage

blib/lib/BSON/Bytes.pm
Criterion Covered Total %
statement 36 36 100.0
branch 6 6 100.0
condition n/a
subroutine 12 12 100.0
pod 1 2 50.0
total 55 56 98.2


line stmt bran cond sub pod time code
1 71     71   24715 use 5.010001;
  71         228  
2 71     71   321 use strict;
  71         126  
  71         1318  
3 71     71   286 use warnings;
  71         152  
  71         2303  
4              
5             package BSON::Bytes;
6             # ABSTRACT: BSON type wrapper for binary byte strings
7              
8 71     71   399 use version;
  71         178  
  71         356  
9             our $VERSION = 'v1.12.0';
10              
11 71     71   5305 use MIME::Base64 ();
  71         62835  
  71         1505  
12 71     71   391 use Tie::IxHash;
  71         151  
  71         1205  
13              
14 71     71   292 use Moo;
  71         389  
  71         441  
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             has [qw/data subtype/] => (
29             is => 'ro',
30             );
31              
32 71     71   24222 use namespace::clean -except => 'meta';
  71         158  
  71         1650  
33              
34             sub BUILD {
35 17032     17032 0 194998 my ($self) = @_;
36 17032 100       40537 $self->{data} = '' unless defined $self->{data};
37 17032 100       86221 $self->{subtype} = 0 unless defined $self->{subtype};
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 option is true, it will instead be compatible with
45             #pod MongoDB's L
46             #pod format, which represents it as a document as follows:
47             #pod
48             #pod {"$binary" : { "base64": "", "subType" : ""} }
49             #pod
50             #pod =cut
51              
52             sub TO_JSON {
53 26 100   26 1 183 return MIME::Base64::encode_base64($_[0]->{data}, "") unless $ENV{BSON_EXTJSON};
54              
55 25         31 my %data;
56 25         70 tie( %data, 'Tie::IxHash' );
57 25         398 $data{base64} = MIME::Base64::encode_base64($_[0]->{data}, "");
58 25         429 $data{subType} = sprintf("%02x",$_[0]->{subtype});
59              
60             return {
61 25         356 '$binary' => \%data,
62             };
63             }
64              
65             use overload (
66 16795     16795   3268156 q{""} => sub { $_[0]->{data} },
67 71         763 fallback => 1,
68 71     71   31777 );
  71         201  
69              
70             # backwards compatibility alias
71             *type = \&subtype;
72              
73             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.0
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 option is true, it will instead be compatible with
118             MongoDB's L
119             format, which represents it as a document as follows:
120              
121             {"$binary" : { "base64": "", "subType" : ""} }
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
137              
138             =item *
139              
140             Stefan G.
141              
142             =back
143              
144             =head1 COPYRIGHT AND LICENSE
145              
146             This software is Copyright (c) 2019 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__