Skip to content

Commit

Permalink
use our own ES type using Type::Tiny
Browse files Browse the repository at this point in the history
  • Loading branch information
haarg committed Jan 29, 2025
1 parent 087d367 commit d593825
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 30 deletions.
2 changes: 1 addition & 1 deletion lib/Catalyst/Plugin/Session/Store/ElasticSearch.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package Catalyst::Plugin::Session::Store::ElasticSearch;

use Moose;
extends 'Catalyst::Plugin::Session::Store';
use MooseX::Types::ElasticSearch qw( ES );
use MetaCPAN::Types::TypeTiny qw( ES );

use MetaCPAN::ESConfig qw( es_doc_path );
use MetaCPAN::Server::Config ();
Expand Down
14 changes: 7 additions & 7 deletions lib/MetaCPAN/API.pm
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ To run the api web server, run the following on one of the servers:

use Mojo::Base 'Mojolicious';

use File::Temp ();
use List::Util qw( any );
use MetaCPAN::Script::Runner ();
use Search::Elasticsearch ();
use Try::Tiny qw( catch try );
use MetaCPAN::Server::Config ();
use MooseX::Types::ElasticSearch qw(ES);
use File::Temp ();
use List::Util qw( any );
use MetaCPAN::Script::Runner ();
use Search::Elasticsearch ();
use Try::Tiny qw( catch try );
use MetaCPAN::Server::Config ();
use MetaCPAN::Types::TypeTiny qw( ES );

has es => sub {
ES->assert_coerce(
Expand Down
6 changes: 3 additions & 3 deletions lib/MetaCPAN/Query.pm
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package MetaCPAN::Query;
use Moose;

use Module::Runtime qw( require_module );
use Module::Pluggable::Object ();
use MooseX::Types::ElasticSearch qw( ES );
use Module::Runtime qw( require_module );
use Module::Pluggable::Object ();
use MetaCPAN::Types::TypeTiny qw( ES );

has es => (
is => 'ro',
Expand Down
2 changes: 1 addition & 1 deletion lib/MetaCPAN/Query/Role/Common.pm
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package MetaCPAN::Query::Role::Common;
use Moose::Role;

use MooseX::Types::ElasticSearch qw( ES );
use MetaCPAN::Types::TypeTiny qw( ES );

has es => (
is => 'ro',
Expand Down
19 changes: 9 additions & 10 deletions lib/MetaCPAN/Role/Script.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ package MetaCPAN::Role::Script;

use Moose::Role;

use Carp ();
use MooseX::Types::ElasticSearch qw( ES );
use IO::Prompt::Tiny qw( prompt );
use Log::Contextual qw( :log :dlog );
use MetaCPAN::Model ();
use MetaCPAN::Types::TypeTiny qw( AbsPath Bool HashRef Int Path Str );
use MetaCPAN::Util qw( root_dir );
use Mojo::Server ();
use Term::ANSIColor qw( colored );
use Carp ();
use IO::Prompt::Tiny qw( prompt );
use Log::Contextual qw( :log :dlog );
use MetaCPAN::Model ();
use MetaCPAN::Types::TypeTiny qw( AbsPath Bool ES HashRef Int Path Str );
use MetaCPAN::Util qw( root_dir );
use Mojo::Server ();
use Term::ANSIColor qw( colored );

use MooseX::Getopt::OptionTypeMap ();
for my $type ( Path, AbsPath ) {
for my $type ( Path, AbsPath, ES ) {
MooseX::Getopt::OptionTypeMap->add_option_type_to_map( $type, '=s' );
}

Expand Down
4 changes: 2 additions & 2 deletions lib/MetaCPAN/Server/Model/ES.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package MetaCPAN::Server::Model::ES;

use Moose;

use MetaCPAN::Server::Config ();
use MooseX::Types::ElasticSearch qw( ES );
use MetaCPAN::Server::Config ();
use MetaCPAN::Types::TypeTiny qw( ES );

extends 'Catalyst::Model';

Expand Down
28 changes: 28 additions & 0 deletions lib/MetaCPAN/Types/TypeTiny.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use Type::Library -base, -declare => ( qw(
HashRefCPANMeta
CommaSepOption
ES
) );
use Type::Utils qw( as coerce declare extends from via );

Expand Down Expand Up @@ -129,4 +131,30 @@ coerce CommaSepOption, from Str, via {
return [ map split(/\s*,\s*/), $_ ];
};

declare ES, as Object;
coerce ES, from Str, via {
my $server = $_;
$server = "127.0.0.1$server" if ( $server =~ /^:/ );
return Search::Elasticsearch->new(
nodes => $server,
cxn => 'HTTPTiny',
);
};

coerce ES, from HashRef, via {
return Search::Elasticsearch->new( {
cxn => 'HTTPTiny',
%$_,
} );
};

coerce ES, from ArrayRef, via {
my @servers = @$_;
@servers = map { /^:/ ? "127.0.0.1$_" : $_ } @servers;
return Search::Elasticsearch->new(
nodes => \@servers,
cxn => 'HTTPTiny',
);
};

1;
12 changes: 6 additions & 6 deletions t/lib/MetaCPAN/Server/Test.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use strict;
use warnings;
use feature qw(state);

use HTTP::Request::Common qw( DELETE GET POST ); ## no perlimports
use MetaCPAN::Model ();
use MetaCPAN::Server ();
use MetaCPAN::Server::Config ();
use MooseX::Types::ElasticSearch qw( ES );
use Plack::Test; ## no perlimports
use HTTP::Request::Common qw( DELETE GET POST ); ## no perlimports
use MetaCPAN::Model ();
use MetaCPAN::Server ();
use MetaCPAN::Server::Config ();
use MetaCPAN::Types::TypeTiny qw( ES );
use Plack::Test; ## no perlimports

use base 'Exporter';
our @EXPORT_OK = qw(
Expand Down

0 comments on commit d593825

Please sign in to comment.