1package Search::Elasticsearch::Util; 2$Search::Elasticsearch::Util::VERSION = '6.00'; 3use Moo; 4use Search::Elasticsearch::Error(); 5use Scalar::Util qw(blessed); 6use Module::Runtime qw(compose_module_name is_module_name use_module); 7use Sub::Exporter -setup => { 8 exports => [ qw( 9 parse_params 10 to_list 11 load_plugin 12 new_error 13 throw 14 upgrade_error 15 is_compat 16 ) 17 ] 18}; 19 20#=================================== 21sub to_list { 22#=================================== 23 grep {defined} ref $_[0] eq 'ARRAY' ? @{ $_[0] } : @_; 24} 25 26#=================================== 27sub parse_params { 28#=================================== 29 my $self = shift; 30 my %params; 31 if ( @_ % 2 ) { 32 throw( 33 "Param", 34 'Expecting a HASH ref or a list of key-value pairs', 35 { params => \@_ } 36 ) unless ref $_[0] eq 'HASH'; 37 %params = %{ shift() }; 38 } 39 else { 40 %params = @_; 41 } 42 return ( $self, \%params ); 43} 44 45#=================================== 46sub load_plugin { 47#=================================== 48 my ( $base, $spec ) = @_; 49 $spec ||= "+$base"; 50 return $spec if blessed $spec; 51 52 my ( $class, $version ); 53 if ( ref $spec eq 'ARRAY' ) { 54 ( $class, $version ) = @$spec; 55 } 56 else { 57 $class = $spec; 58 } 59 60 unless ( $class =~ s/\A\+// ) { 61 $class = compose_module_name( $base, $class ); 62 } 63 64 $version ? use_module( $class, $version ) : use_module($class); 65} 66 67#=================================== 68sub throw { 69#=================================== 70 my ( $type, $msg, $vars ) = @_; 71 die Search::Elasticsearch::Error->new( $type, $msg, $vars, 1 ); 72} 73 74#=================================== 75sub new_error { 76#=================================== 77 my ( $type, $msg, $vars ) = @_; 78 return Search::Elasticsearch::Error->new( $type, $msg, $vars, 1 ); 79} 80 81#=================================== 82sub upgrade_error { 83#=================================== 84 my ( $error, $vars ) = @_; 85 return 86 ref($error) && $error->isa('Search::Elasticsearch::Error') 87 ? $error 88 : Search::Elasticsearch::Error->new( "Internal", $error, $vars || {}, 89 1 ); 90} 91 92#=================================== 93sub is_compat { 94#=================================== 95 my ( $attr, $one, $two ) = @_; 96 my $role 97 = $one->does('Search::Elasticsearch::Role::Is_Sync') 98 ? 'Search::Elasticsearch::Role::Is_Sync' 99 : 'Search::Elasticsearch::Role::Is_Async'; 100 101 return if eval { $two->does($role); }; 102 my $class = ref($two) || $two; 103 die "$attr ($class) does not do $role"; 104} 105 1061; 107 108# ABSTRACT: A utility class for internal use by Search::Elasticsearch 109 110__END__ 111 112=pod 113 114=encoding UTF-8 115 116=head1 NAME 117 118Search::Elasticsearch::Util - A utility class for internal use by Search::Elasticsearch 119 120=head1 VERSION 121 122version 6.00 123 124=head1 AUTHOR 125 126Clinton Gormley <drtech@cpan.org> 127 128=head1 COPYRIGHT AND LICENSE 129 130This software is Copyright (c) 2017 by Elasticsearch BV. 131 132This is free software, licensed under: 133 134 The Apache License, Version 2.0, January 2004 135 136=cut 137