| 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::Code; | ||||||||||||||||||||||||||||
| 6 | # ABSTRACT: BSON type wrapper for Javascript code | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 71 | ▶ | use version; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 71 | our $VERSION = 'v1.12.2'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 71 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
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 code | ||||||||||||||||||||||||||||
| 17 | #pod | ||||||||||||||||||||||||||||
| 18 | #pod A string containing Javascript code. Defaults to the empty string. | ||||||||||||||||||||||||||||
| 19 | #pod | ||||||||||||||||||||||||||||
| 20 | #pod =attr scope | ||||||||||||||||||||||||||||
| 21 | #pod | ||||||||||||||||||||||||||||
| 22 | #pod An optional hash reference containing variables in the scope of C<code>. | ||||||||||||||||||||||||||||
| 23 | #pod Defaults to C<undef>. | ||||||||||||||||||||||||||||
| 24 | #pod | ||||||||||||||||||||||||||||
| 25 | #pod =cut | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 71 | has [ qw/code scope/ ] => ( | |||||||||||||||||||||||||||
| 28 | is => 'ro' | ||||||||||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 71 | ▶ | use namespace::clean -except => 'meta'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | #pod =method length | ||||||||||||||||||||||||||||
| 34 | #pod | ||||||||||||||||||||||||||||
| 35 | #pod Returns the length of the C<code> attribute. | ||||||||||||||||||||||||||||
| 36 | #pod | ||||||||||||||||||||||||||||
| 37 | #pod =cut | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | sub length { | ||||||||||||||||||||||||||
|
Subroutine
length: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | 0 | length( $_[0]->code ); | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # Support legacy constructor shortcut | ||||||||||||||||||||||||||||
| 44 | 17953 | ▶ | sub BUILDARGS { | ||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 45 | 17953 | my ($class) = shift; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 17953 | my %args; | |||||||||||||||||||||||||||
| 48 | 17953 | ▶ | if ( @_ && $_[0] !~ /^[c|s]/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ and not $_[0] =~ /^[c|s]/
MC/DC100%2/2
@_ and not $_[0] =~ /^[c|s]/
@_
not $_[0] =~ /^[c|s]/
Truth table100%3/3
A and not(B)
A= @_B= not $_[0] =~ /^[c|s]/
| |||||||||||||||||||||||||||||
| 49 | 8784 | $args{code} = $_[0]; | |||||||||||||||||||||||||||
| 50 | 8784 | ▶ | $args{scope} = $_[1] if defined $_[1]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | else { | ||||||||||||||||||||||||||||
| 53 | 9169 | ▶ | Carp::croak( __PACKAGE__ . "::new called with an odd number of elements\n" ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | unless @_ % 2 == 0; | ||||||||||||||||||||||||||||
| 55 | 9169 | %args = @_; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 17953 | return \%args; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 17953 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=4 SCAR=14.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 62 | 17953 | my ($self) = @_; | |||||||||||||||||||||||||||
| 63 | 17953 | ▶ | $self->{code} = '' unless defined $self->{code}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | Carp::croak( __PACKAGE__ . " scope must be hash reference, not $self->{scope}") | ||||||||||||||||||||||||||||
| 65 | 17953 | ▶ | if exists($self->{scope}) && ref($self->{scope}) ne 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $self->{"scope"} and ref $self->{"scope"} ne "HASH"
MC/DC0%0/2
exists $self->{"scope"} and ref $self->{"scope"} ne "HASH"
exists $self->{"scope"}
ref $self->{"scope"} ne "HASH"
Truth table66%2/3
A and B
A= exists $self->{"scope"}B= ref $self->{"scope"} ne "HASH"
| |||||||||||||||||||||||||||||
| 66 | 17953 | return; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | #pod =method TO_JSON | ||||||||||||||||||||||||||||
| 70 | #pod | ||||||||||||||||||||||||||||
| 71 | #pod If the C<BSON_EXTJSON> option is true, returns a hashref compatible with | ||||||||||||||||||||||||||||
| 72 | #pod MongoDB's L<extended JSON|https://github.com/mongodb/specifications/blob/master/source/extended-json.rst> | ||||||||||||||||||||||||||||
| 73 | #pod format, which represents it as a document as follows: | ||||||||||||||||||||||||||||
| 74 | #pod | ||||||||||||||||||||||||||||
| 75 | #pod {"$code" : "<code>"} | ||||||||||||||||||||||||||||
| 76 | #pod {"$code" : "<code>", "$scope" : { ... } } | ||||||||||||||||||||||||||||
| 77 | #pod | ||||||||||||||||||||||||||||
| 78 | #pod If the C<BSON_EXTJSON> option is false, an error is thrown, as this value | ||||||||||||||||||||||||||||
| 79 | #pod can't otherwise be represented in JSON. | ||||||||||||||||||||||||||||
| 80 | #pod | ||||||||||||||||||||||||||||
| 81 | #pod =cut | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 23 | ▶ | sub TO_JSON { | ||||||||||||||||||||||||||
|
Subroutine
TO_JSON: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | 23 | require BSON; | |||||||||||||||||||||||||||
| 85 | 23 | ▶ | if ( $ENV{BSON_EXTJSON} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 22 | my %data; | |||||||||||||||||||||||||||
| 87 | 22 | tie( %data, 'Tie::IxHash' ); | |||||||||||||||||||||||||||
| 88 | 22 | $data{'$code'} = $_[0]->{code}; | |||||||||||||||||||||||||||
| 89 | $data{'$scope'} = BSON->perl_to_extjson($_[0]->{scope}) | ||||||||||||||||||||||||||||
| 90 | 22 | ▶ | if defined $_[0]->{scope}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 22 | return \%data; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 1 | Carp::croak( "The value '$_[0]' is illegal in JSON" ); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 71 | 1; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =pod | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =head1 NAME | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | BSON::Code - BSON type wrapper for Javascript code | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =head1 VERSION | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | version v1.12.2 | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | use BSON::Types ':all'; | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | $code = bson_code( $javascript ); | ||||||||||||||||||||||||||||
| 116 | $code = bson_code( $javascript, $scope ); | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | This module provides a BSON type wrapper for the "Javascript code" type | ||||||||||||||||||||||||||||
| 121 | and the "Javascript with Scope" BSON types. | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =head2 code | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | A string containing Javascript code. Defaults to the empty string. | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =head2 scope | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | An optional hash reference containing variables in the scope of C<code>. | ||||||||||||||||||||||||||||
| 132 | Defaults to C<undef>. | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head1 METHODS | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =head2 length | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | Returns the length of the C<code> attribute. | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =head2 TO_JSON | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | If the C<BSON_EXTJSON> option is true, returns a hashref compatible with | ||||||||||||||||||||||||||||
| 143 | MongoDB's L<extended JSON|https://github.com/mongodb/specifications/blob/master/source/extended-json.rst> | ||||||||||||||||||||||||||||
| 144 | format, which represents it as a document as follows: | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | {"$code" : "<code>"} | ||||||||||||||||||||||||||||
| 147 | {"$code" : "<code>", "$scope" : { ... } } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | If the C<BSON_EXTJSON> option is false, an error is thrown, as this value | ||||||||||||||||||||||||||||
| 150 | can't otherwise be represented in JSON. | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =for Pod::Coverage BUILD BUILDARGS | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =over 4 | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =item * | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | David Golden <david@mongodb.com> | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =item * | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | Stefan G. <minimalist@lavabit.com> | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =back | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | This software is Copyright (c) 2020 by Stefan G. and MongoDB, Inc. | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | This is free software, licensed under: | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | The Apache License, Version 2.0, January 2004 | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =cut | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | __END__ | ||||||||||||||||||||||||||||