| 1 | # Licensed to Elasticsearch B.V. under one or more contributor | ||||||||||||||||||||||||||||
| 2 | # license agreements. See the NOTICE file distributed with | ||||||||||||||||||||||||||||
| 3 | # this work for additional information regarding copyright | ||||||||||||||||||||||||||||
| 4 | # ownership. Elasticsearch B.V. licenses this file to you under | ||||||||||||||||||||||||||||
| 5 | # the Apache License, Version 2.0 (the "License"); you may | ||||||||||||||||||||||||||||
| 6 | # not use this file except in compliance with the License. | ||||||||||||||||||||||||||||
| 7 | # You may obtain a copy of the License at | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||||||||
| 10 | # | ||||||||||||||||||||||||||||
| 11 | # Unless required by applicable law or agreed to in writing, | ||||||||||||||||||||||||||||
| 12 | # software distributed under the License is distributed on an | ||||||||||||||||||||||||||||
| 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||||||||||||||||||||||||||||
| 14 | # KIND, either express or implied. See the License for the | ||||||||||||||||||||||||||||
| 15 | # specific language governing permissions and limitations | ||||||||||||||||||||||||||||
| 16 | # under the License. | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | package Search::Elasticsearch::Role::API; | ||||||||||||||||||||||||||||
| 19 | 55 | $Search::Elasticsearch::Role::API::VERSION = '8.12'; | |||||||||||||||||||||||||||
| 20 | 55 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 55 | requires 'api_version'; | |||||||||||||||||||||||||||
| 22 | 55 | requires 'api'; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 55 | ▶ | use Scalar::Util qw(looks_like_number); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 55 | ▶ | use Search::Elasticsearch::Util qw(throw); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 55 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 55 | our %Handler = ( | |||||||||||||||||||||||||||
| 29 | string => \&_string, | ||||||||||||||||||||||||||||
| 30 | time => \&_string, | ||||||||||||||||||||||||||||
| 31 | date => \&_string, | ||||||||||||||||||||||||||||
| 32 | list => \&_list, | ||||||||||||||||||||||||||||
| 33 | boolean => \&_bool, | ||||||||||||||||||||||||||||
| 34 | enum => \&_list, | ||||||||||||||||||||||||||||
| 35 | number => \&_num, | ||||||||||||||||||||||||||||
| 36 | int => \&_num, | ||||||||||||||||||||||||||||
| 37 | integer => \&_num, | ||||||||||||||||||||||||||||
| 38 | float => \&_num, | ||||||||||||||||||||||||||||
| 39 | double => \&_num, | ||||||||||||||||||||||||||||
| 40 | "number|string" => \&_numOrString, | ||||||||||||||||||||||||||||
| 41 | "boolean|long" => \&_booleanOrLong | ||||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | #=================================== | ||||||||||||||||||||||||||||
| 45 | 0 | ▶ | sub _bool { | ||||||||||||||||||||||||||
|
Subroutine
_bool: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 46 | #=================================== | ||||||||||||||||||||||||||||
| 47 | 0 | my $val = _detect_bool(@_); | |||||||||||||||||||||||||||
| 48 | 0 | ▶ | return ( $val && $val ne 'false' ) ? 'true' : 'false'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$val and $val ne "false"
MC/DC0%0/2
$val and $val ne "false"
$val
$val ne "false"
Truth table0%0/3
A and B
A= $valB= $val ne "false"
| |||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | #=================================== | ||||||||||||||||||||||||||||
| 52 | 0 | ▶ | sub _detect_bool { | ||||||||||||||||||||||||||
|
Subroutine
_detect_bool: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 53 | #=================================== | ||||||||||||||||||||||||||||
| 54 | 0 | my $val = shift; | |||||||||||||||||||||||||||
| 55 | 0 | ▶ | return '' unless defined $val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | if ( ref $val eq 'SCALAR' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | return 'false' if $$val eq 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | return 'true' if $$val eq 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | elsif ( UNIVERSAL::isa( $val, "JSON::PP::Boolean" ) ) { | ||||||||||||||||||||||||||||
| 61 | 0 | ▶ | return "$val" ? 'true' : 'false'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | 0 | return "$val"; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | #=================================== | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | sub _list { | ||||||||||||||||||||||||||
|
Subroutine
_list: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 68 | #=================================== | ||||||||||||||||||||||||||||
| 69 | 0 | ▶ | return join ",", map { _detect_bool($_) } # | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | ref $_[0] eq 'ARRAY' ? @{ $_[0] } : $_[0]; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | #=================================== | ||||||||||||||||||||||||||||
| 74 | 0 | ▶ | sub _num { | ||||||||||||||||||||||||||
|
Subroutine
_num: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 75 | #=================================== | ||||||||||||||||||||||||||||
| 76 | 0 | return 0 + $_[0]; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | #=================================== | ||||||||||||||||||||||||||||
| 80 | 0 | ▶ | sub _string { | ||||||||||||||||||||||||||
|
Subroutine
_string: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 81 | #=================================== | ||||||||||||||||||||||||||||
| 82 | 0 | return "$_[0]"; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | #=================================== | ||||||||||||||||||||||||||||
| 86 | 0 | ▶ | sub _numOrString { | ||||||||||||||||||||||||||
|
Subroutine
_numOrString: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 87 | #=================================== | ||||||||||||||||||||||||||||
| 88 | 0 | ▶ | if (looks_like_number($_[0])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 0 | return _num($_[0]); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 0 | return _string($_[0]); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | #=================================== | ||||||||||||||||||||||||||||
| 95 | 0 | ▶ | sub _booleanOrLong { | ||||||||||||||||||||||||||
|
Subroutine
_booleanOrLong: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 96 | #=================================== | ||||||||||||||||||||||||||||
| 97 | 0 | ▶ | if (looks_like_number($_[0])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 0 | return _num($_[0]); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 0 | my $val = _detect_bool(@_); | |||||||||||||||||||||||||||
| 101 | 0 | ▶ | return ( $val && $val ne 'false' ) ? 'true' : 'false'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$val and $val ne "false"
MC/DC0%0/2
$val and $val ne "false"
$val
$val ne "false"
Truth table0%0/3
A and B
A= $valB= $val ne "false"
| |||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | #=================================== | ||||||||||||||||||||||||||||
| 105 | 55 | ▶ | sub _qs_init { | ||||||||||||||||||||||||||
|
Subroutine
_qs_init: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 106 | #=================================== | ||||||||||||||||||||||||||||
| 107 | 55 | my $class = shift; | |||||||||||||||||||||||||||
| 108 | 55 | my $API = shift; | |||||||||||||||||||||||||||
| 109 | 55 | for my $spec ( keys %$API ) { | |||||||||||||||||||||||||||
| 110 | 26455 | my $qs = $API->{$spec}{qs}; | |||||||||||||||||||||||||||
| 111 | 26455 | for my $param ( keys %$qs ) { | |||||||||||||||||||||||||||
| 112 | my $handler = $Handler{ $qs->{$param} } | ||||||||||||||||||||||||||||
| 113 | or throw( "Internal", | ||||||||||||||||||||||||||||
| 114 | "Unknown type <" | ||||||||||||||||||||||||||||
| 115 | 146465 | ▶ | . $qs->{$param} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | . "> for param <$param> in API <$spec>" ); | ||||||||||||||||||||||||||||
| 117 | 146465 | $qs->{$param} = $handler; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 55 | 1; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | # ABSTRACT: Provides common functionality for API implementations | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | __END__ | ||||||||||||||||||||||||||||