File Coverage

blib/lib/OpenSearch/Client/Util.pm
Criterion Covered Total %
statement 45 48 93.7
branch 13 20 65.0
condition 3 11 27.2
subroutine 12 12 100.0
pod 0 7 0.0
total 73 98 74.4


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 that work are contained in the NOTICE
4             # file distributed with this work.
5             #-----------------------------------------------------------------------
6             # OpenSearch::Client
7             #-----------------------------------------------------------------------
8             # Copyright 2026 Mark Dootson
9             #
10             # Licensed under the Apache License, Version 2.0 (the "License");
11             # you may not use this file except in compliance with the License.
12             # You may obtain a copy of the License at
13             #
14             # http://www.apache.org/licenses/LICENSE-2.0
15             #
16             # Unless required by applicable law or agreed to in writing, software
17             # distributed under the License is distributed on an "AS IS" BASIS,
18             # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19             # See the License for the specific language governing permissions and
20             # limitations under the License.
21              
22             package OpenSearch::Client::Util;
23             $OpenSearch::Client::Util::VERSION = '3.007002';
24 59     59   1028 use Moo;
  59         154  
  59         323  
25 59     59   116455 use OpenSearch::Client::Error();
  59         135  
  59         1686  
26 59     59   323 use Scalar::Util qw(blessed);
  59         88  
  59         3059  
27 59     59   23869 use Module::Runtime qw(compose_module_name is_module_name use_module);
  59         85383  
  59         419  
28 59         437 use Sub::Exporter -setup => {
29             exports => [ qw(
30             parse_params
31             to_list
32             load_plugin
33             new_error
34             throw
35             upgrade_error
36             is_compat
37             )
38             ]
39 59     59   35103 };
  59         588851  
40              
41             #===================================
42             sub to_list {
43             #===================================
44 304 100   304 0 1003 grep {defined} ref $_[0] eq 'ARRAY' ? @{ $_[0] } : @_;
  297         2609  
  72         411  
45             }
46              
47             #===================================
48             sub parse_params {
49             #===================================
50 562     562 0 1000 my $self = shift;
51 562         842 my %params;
52 562 100       1601 if ( @_ % 2 ) {
53 150 50       498 throw(
54             "Param",
55             'Expecting a HASH ref or a list of key-value pairs',
56             { params => \@_ }
57             ) unless ref $_[0] eq 'HASH';
58 150         240 %params = %{ shift() };
  150         696  
59             }
60             else {
61 412         1387 %params = @_;
62             }
63 562         1647 return ( $self, \%params );
64             }
65              
66             #===================================
67             sub load_plugin {
68             #===================================
69 700     700 0 1134 my ( $base, $spec ) = @_;
70 700   66     1815 $spec ||= "+$base";
71 700 50       1335 return $spec if blessed $spec;
72              
73 700         980 my ( $class, $version );
74 700 50       1365 if ( ref $spec eq 'ARRAY' ) {
75 0         0 ( $class, $version ) = @$spec;
76             }
77             else {
78 700         950 $class = $spec;
79             }
80              
81 700 100       2139 unless ( $class =~ s/\A\+// ) {
82 468         1362 $class = compose_module_name( $base, $class );
83             }
84              
85 700 50       20064 $version ? use_module( $class, $version ) : use_module($class);
86             }
87              
88             #===================================
89             sub throw {
90             #===================================
91 89     89 0 539 my ( $type, $msg, $vars ) = @_;
92 89         459 die OpenSearch::Client::Error->new( $type, $msg, $vars, 1 );
93             }
94              
95             #===================================
96             sub new_error {
97             #===================================
98 29     29 0 78 my ( $type, $msg, $vars ) = @_;
99 29         134 return OpenSearch::Client::Error->new( $type, $msg, $vars, 1 );
100             }
101              
102             #===================================
103             sub upgrade_error {
104             #===================================
105 46     46 0 215 my ( $error, $vars ) = @_;
106             return
107 46 50 33     468 ref($error) && $error->isa('OpenSearch::Client::Error')
      0        
108             ? $error
109             : OpenSearch::Client::Error->new( "Internal", $error, $vars || {},
110             1 );
111             }
112              
113             #===================================
114             sub is_compat {
115             #===================================
116 200     200 0 410 my ( $attr, $one, $two ) = @_;
117 200 50       547 my $role
118             = $one->does('OpenSearch::Client::Role::Is_Sync')
119             ? 'OpenSearch::Client::Role::Is_Sync'
120             : 'OpenSearch::Client::Role::Is_Async';
121              
122 200 50       3991 return if eval { $two->does($role); };
  200         559  
123 0   0       my $class = ref($two) || $two;
124 0           die "$attr ($class) does not do $role";
125             }
126              
127             1;
128              
129             __END__