File Coverage

blib/lib/OpenSearch/Client/Role/API.pm
Criterion Covered Total %
statement 19 39 48.7
branch 1 20 5.0
condition 0 3 0.0
subroutine 5 13 38.4
pod n/a
total 25 75 33.3


line stmt bran cond sub pod time code
1             # OpenSearch::Client is an unofficial client for OpenSearch.
2             # It is derived from Search::Elasticsearch version 7.714
3             # License details from the original work are contained in the
4             # NOTICE file distributed with this work.
5             #
6             #-----------------------------------------------------------------------
7             # OpenSearch::Client
8             #-----------------------------------------------------------------------
9             # Copyright 2026 Mark Dootson
10             #
11             # Licensed under the Apache License, Version 2.0 (the "License");
12             # you may not use this file except in compliance with the License.
13             # You may obtain a copy of the License at
14             #
15             # http://www.apache.org/licenses/LICENSE-2.0
16             #
17             # Unless required by applicable law or agreed to in writing, software
18             # distributed under the License is distributed on an "AS IS" BASIS,
19             # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20             # See the License for the specific language governing permissions and
21             # limitations under the License.
22              
23              
24             package OpenSearch::Client::Role::API;
25             $OpenSearch::Client::Role::API::VERSION = '3.007002';
26 55     55   27892 use Moo::Role;
  55         109  
  55         366  
27             requires 'api_version';
28             requires 'api';
29              
30 55     55   24333 use Scalar::Util qw(looks_like_number);
  55         105  
  55         5712  
31 55     55   283 use OpenSearch::Client::Util qw(throw);
  55         117  
  55         436  
32 55     55   13324 use namespace::clean;
  55         86  
  55         329  
33              
34             our %Handler = (
35             string => \&_string,
36             time => \&_string,
37             date => \&_string,
38             list => \&_list,
39             boolean => \&_bool,
40             enum => \&_list,
41             number => \&_num,
42             int => \&_num,
43             float => \&_num,
44             double => \&_num,
45             'number|string' => \&_numOrString,
46             'boolean|string' => \&_boolOrString,
47             'boolean|number' => \&_boolOrNumber,
48            
49             );
50              
51             #===================================
52             sub _bool {
53             #===================================
54 0     0   0 my $val = _detect_bool(@_);
55 0 0 0     0 return ( $val && $val ne 'false' ) ? 'true' : 'false';
56             }
57              
58             #===================================
59             sub _detect_bool {
60             #===================================
61 0     0   0 my $val = shift;
62 0 0       0 return '' unless defined $val;
63 0 0       0 if ( ref $val eq 'SCALAR' ) {
    0          
64 0 0       0 return 'false' if $$val eq 0;
65 0 0       0 return 'true' if $$val eq 1;
66             }
67             elsif ( UNIVERSAL::isa( $val, "JSON::PP::Boolean" ) ) {
68 0 0       0 return "$val" ? 'true' : 'false';
69             }
70 0         0 return "$val";
71             }
72              
73             #===================================
74             sub _list {
75             #===================================
76 0         0 return join ",", map { _detect_bool($_) } #
77 0 0   0   0 ref $_[0] eq 'ARRAY' ? @{ $_[0] } : $_[0];
  0         0  
78             }
79              
80             #===================================
81             sub _num {
82             #===================================
83 0     0   0 return 0 + $_[0];
84             }
85              
86             #===================================
87             sub _string {
88             #===================================
89 0     0   0 return "$_[0]";
90             }
91              
92             #===================================
93             sub _numOrString {
94             #===================================
95 0 0   0   0 if (looks_like_number($_[0])) {
96 0         0 return _num($_[0]);
97             }
98 0         0 return _string($_[0]);
99             }
100              
101             #===================================
102             sub _boolOrString {
103             #===================================
104 0     0   0 return _detect_bool( @_ );
105             }
106              
107             #===================================
108             sub _boolOrNumber {
109             #===================================
110 0     0   0 my $val = _detect_bool(@_);
111 0         0 return _numOrString($val);
112             }
113              
114             #===================================
115             sub _qs_init {
116             #===================================
117 55     55   154 my $class = shift;
118 55         86 my $API = shift;
119 55         3112 for my $spec ( keys %$API ) {
120 26620         33538 my $qs = $API->{$spec}{qs};
121 26620         51895 for my $param ( keys %$qs ) {
122             my $handler = $Handler{ $qs->{$param} }
123             or throw( "Internal",
124             "Unknown type <"
125 197560 50       266456 . $qs->{$param}
126             . "> for param <$param> in API <$spec>" );
127 197560         233662 $qs->{$param} = $handler;
128             }
129             }
130             }
131              
132             1;
133              
134             __END__