Elasticsearch/Role/Client/Direct.pm

filter: Statementstmt 35.4% 34 / 96 Branchbran 10.5% 4 / 38 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 47.0% 8 / 17 Podpod 0.0% 0 / 1 totaltotal 25.9% 46 / 177 CC 32 SCAR 39.7
CC
32
Cov
25%
_parse_path
152.8
_parse_qs
42.0
_parse_body
30.0
CRAP 53.2
↑ Summary Elasticsearch/Client/8_0/Role/Bulk.pm »
1 # Licensed to Elasticsearch B.V under one or more agreements.
2 # Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3 # See the LICENSE file in the project root for more information
4  
5 package Search::Elasticsearch::Role::Client::Direct;
6 55 $Search::Elasticsearch::Role::Client::Direct::VERSION = '8.12';
7 55 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 55 with 'Search::Elasticsearch::Role::Client';
9 55 ▶use Search::Elasticsearch::Util qw(load_plugin is_compat throw);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 55 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 55 ▶use Package::Stash 0.34 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 55 ▶use Any::URI::Escape qw(uri_escape);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 55 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 #===================================
17 0 ▶sub parse_request {
Subroutine parse_request: not called CC=3 SCAR=24.8
Pod undocumented
18 #===================================
19 0 my $self = shift;
20 0 ▶ my $defn = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
21 0 ▶ my $params = { ref $_[0] ? %{ shift() } : @_ };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
22  
23 0 my $request;
24 0 ▶ try {
Subroutine __ANON__: not called CC=4 SCAR=30.0
25 $request = {
26 ignore => delete $params->{ignore} || [],
27 method => $defn->{method} || 'GET',
28 serialize => $defn->{serialize} || 'std',
29 path => $self->_parse_path( $defn, $params ),
30 body => $self->_parse_body( $defn->{body}, $params ),
31 0 ▶ qs => $self->_parse_qs( $defn->{qs}, $params ),
Condition0%0/6
delete $params->{'ignore'} || []
l!l
00
$defn->{'method'} || 'GET'
l!l
00
$defn->{'serialize'} || 'std'
l!l
00
MC/DC0%0/1
delete $params->{'ignore'} || []
delete $params->{'ignore'}
MC/DC0%0/1
$defn->{'method'} || 'GET'
$defn->{'method'}
MC/DC0%0/1
$defn->{'serialize'} || 'std'
$defn->{'serialize'}
Truth table0%0/2
A
A= delete $params->{'ignore'}
Aresult
11
00
Truth table0%0/2
A
A= $defn->{'method'}
Aresult
11
00
Truth table0%0/2
A
A= $defn->{'serialize'}
Aresult
11
00
32 };
33 }
34 0 ▶ catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
35 0 chomp $_;
36 0 ▶ my $name = $defn->{name} || '<unknown method>';
Condition0%0/2
$defn->{'name'} || '<unknown method>'
l!l
00
MC/DC0%0/1
$defn->{'name'} || '<unknown method>'
$defn->{'name'}
Truth table0%0/2
A
A= $defn->{'name'}
Aresult
11
00
37 0 $self->logger->throw_error( 'Param', "$_ in ($name) request. " );
38 0 };
39 0 return $request;
40 }
41  
42 #===================================
43 0 ▶sub _parse_path {
Subroutine _parse_path: not called CC=13 SCAR=50.3
44 #===================================
45 0 my ( $self, $defn, $params ) = @_;
46 return delete $params->{path}
47 0 ▶ if $params->{path};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"path"} 0 0 0
48 0 my $paths = $defn->{paths};
49 0 my $parts = $defn->{parts};
50  
51 0 my %args;
52 0 keys %$parts;
53 55 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
54 0 ▶ while ( my ( $key, $req ) = each %$parts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$parts) 0 0 0
55 0 my $val = delete $params->{$key};
56 0 ▶ if ( ref $val eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq "ARRAY") 0 0 0
57 die "Param ($key) must contain a single value\n"
58 0 ▶ if @$val > 1 and not $req->{multi};
Branch: 1 branch
BranchTrueFalseTotal
if @$val > 1 and not $req->{"multi"} 0 0 0
Condition0%0/3
@$val > 1 and not $req->{"multi"}
!ll&&!rl&&r
000
MC/DC0%0/2
@$val > 1 and not $req->{"multi"}
@$val > 1 not $req->{"multi"}
Truth table0%0/3
A and not(B)
A= @$val > 1B= not $req->{"multi"}
ABresult
0X0
100
111
59 0 $val = join ",", @$val;
60 }
61 0 ▶ if ( !length $val ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $val) 0 0 0
62 die "Missing required param ($key)\n"
63 0 ▶ if $req->{required};
Branch: 1 branch
BranchTrueFalseTotal
if $req->{"required"} 0 0 0
64 0 next;
65 }
66 0 utf8::encode($val);
67 0 $args{$key} = uri_escape($val);
68 }
69 0 PATH: for my $path (@$paths) {
70 0 my @keys = keys %{ $path->[0] };
71 0 ▶ next PATH unless @keys == keys %args;
Branch: 1 branch
BranchTrueFalseTotal
unless @keys == keys %args 0 0 0
72 0 for (@keys) {
73 0 ▶ next PATH unless exists $args{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{$_} 0 0 0
74 }
75 0 my ( $pos, @parts ) = @$path;
76 0 for ( keys %$pos ) {
77 0 $parts[ $pos->{$_} ] = $args{$_};
78 }
79 0 return join "/", '', @parts;
80 }
81  
82 throw(
83 0 'Internal',
84 "Couldn't determine path",
85 { params => $params, defn => $defn }
86 );
87 }
88  
89 #===================================
90 0 ▶sub _parse_body {
Subroutine _parse_body: not called CC=5 SCAR=34.0
91 #===================================
92 0 my ( $self, $defn, $params ) = @_;
93 0 ▶ if ( defined $defn ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $defn) 0 0 0
94 die("Missing required param (body)\n")
95 0 ▶ if $defn->{required} && !$params->{body};
Branch: 1 branch
BranchTrueFalseTotal
if $defn->{"required"} and not $params->{"body"} 0 0 0
Condition0%0/3
$defn->{"required"} and not $params->{"body"}
!ll&&!rl&&r
000
MC/DC0%0/2
$defn->{"required"} and not $params->{"body"}
$defn->{"required"} not $params->{"body"}
Truth table0%0/3
A and not(B)
A= $defn->{"required"}B= not $params->{"body"}
ABresult
0X0
100
111
96 0 return delete $params->{body};
97 }
98 0 ▶ die("Unknown param (body)\n") if $params->{body};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"body"} 0 0 0
99 0 return undef;
100 }
101  
102 #===================================
103 0 ▶sub _parse_qs {
Subroutine _parse_qs: not called CC=6 SCAR=37.4
104 #===================================
105 0 my ( $self, $handlers, $params ) = @_;
106 0 ▶ die "No (qs) defined\n" unless $handlers;
Branch: 1 branch
BranchTrueFalseTotal
unless $handlers 0 0 0
107 0 my %qs;
108  
109 0 ▶ if ( my $raw = delete $params->{params} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $raw = delete $params->{"params"}) 0 0 0
110 0 ▶ die("Arg (params) shoud be a hashref\n")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $raw eq "HASH" 0 0 0
111 unless ref $raw eq 'HASH';
112 0 %qs = %$raw;
113 }
114  
115 0 for my $key ( keys %$params ) {
116 0 ▶ my $handler = $handlers->{$key}
Branch: 1 branch
BranchTrueFalseTotal
my $handler = $handlers->{$key} or die "Unknown param ($key)\n" 0 0 0
117 or die("Unknown param ($key)\n");
118 0 $qs{$key} = $handler->( delete $params->{$key} );
119 }
120 0 return \%qs;
121 }
122  
123 #===================================
124 55 ▶sub _install_api {
Subroutine _install_api: called CC=5 SCAR=16.4
125 #===================================
126 55 my ( $class, $group ) = @_;
127 55 my $defns = $class->api;
128 55 my $stash = Package::Stash->new($class);
129  
130 55 ▶ my $group_qr = $group ? qr/$group\./ : qr//;
Branch: 1 branch
BranchTrueFalseTotal
$group ? : 0 55 55
131 55 for my $action ( keys %$defns ) {
132 26455 ▶ my ($name) = ( $action =~ /^$group_qr([^.]+)$/ )
Branch: 1 branch
BranchTrueFalseTotal
my($name) = $action =~ /^$group_qr([^.]+)$/ or next 2420 24035 26455
133 or next;
134 2420 ▶ next if $stash->has_symbol( '&' . $name );
Branch: 1 branch
BranchTrueFalseTotal
if $stash->has_symbol('&' . $name) 0 2420 2420
135  
136 2420 my %defn = ( name => $name, %{ $defns->{$action} } );
137 $stash->add_symbol(
138 0 ▶ '&' . $name => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
139 0 shift->perform_request( \%defn, @_ );
140 }
141 2420 );
142 }
143 }
144  
145 #===================================
146 0 ▶sub _build_namespace {
Subroutine _build_namespace: not called CC=1 SCAR=6.9
147 #===================================
148 0 my ( $self, $ns ) = @_;
149 0 my $class = load_plugin( $self->_namespace, [$ns] );
150 0 return $class->new(
151 { transport => $self->transport,
152 logger => $self->logger
153 }
154 );
155 }
156  
157 #===================================
158 0 ▶sub _build_helper {
Subroutine _build_helper: not called CC=1 SCAR=6.9
159 #===================================
160 0 my ( $self, $name, $sub_class ) = @_;
161 0 my $class = load_plugin( 'Search::Elasticsearch', $sub_class );
162 0 is_compat( $name . '_helper_class', $self->transport, $class );
163 0 return $class;
164 }
165  
166 55 1;
167  
168 # ABSTRACT: Request parsing for Direct clients
169  
170 __END__
↑ Summary Elasticsearch/Client/8_0/Role/Bulk.pm »