Skip to content

Commit

Permalink
Merge pull request #1341 from metacpan/haarg/type-tiny
Browse files Browse the repository at this point in the history
Use Type::Tiny based types in most cases
  • Loading branch information
haarg authored Feb 2, 2025
2 parents f72eadb + 02c1d5a commit 82201c6
Show file tree
Hide file tree
Showing 20 changed files with 100 additions and 89 deletions.
4 changes: 2 additions & 2 deletions lib/Catalyst/Authentication/Store/Proxy.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package Catalyst::Authentication::Store::Proxy;
# ABSTRACT: Delegates authentication logic to the user object
use Moose;
use Catalyst::Utils ();
use MetaCPAN::Types::TypeTiny qw( HashRef Str );
use MetaCPAN::Types::TypeTiny qw( ClassName HashRef Str );

has user_class => (
is => 'ro',
Expand All @@ -14,7 +14,7 @@ has user_class => (
);
has handles => ( is => 'ro', isa => HashRef );
has config => ( is => 'ro', isa => HashRef );
has app => ( is => 'ro', isa => 'ClassName' );
has app => ( is => 'ro', isa => ClassName );
has realm => ( is => 'ro' );

sub BUILDARGS {
Expand Down
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
4 changes: 2 additions & 2 deletions lib/MetaCPAN/Model/Archive.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package MetaCPAN::Model::Archive;
use v5.10;
use Moose;
use MooseX::StrictConstructor;
use MetaCPAN::Types::TypeTiny qw( AbsPath ArrayRef Bool Str );
use MetaCPAN::Types::TypeTiny qw( AbsPath ArrayRef Bool InstanceOf Str );

use Archive::Any ();
use Carp qw( croak );
Expand Down Expand Up @@ -52,7 +52,7 @@ has file => (

has _extractor => (
is => 'ro',
isa => 'Archive::Any',
isa => InstanceOf ['Archive::Any'],
handles => [ qw(
is_impolite
is_naughty
Expand Down
34 changes: 16 additions & 18 deletions lib/MetaCPAN/Model/Release.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,25 @@ use Moose;

use v5.10;

use CPAN::DistnameInfo ();
use CPAN::Meta ();
use DateTime ();
use File::Find ();
use File::Spec ();
use Log::Contextual qw( :log :dlog );
use MetaCPAN::ESConfig qw( es_doc_path );
use MetaCPAN::Model::Archive ();
use MetaCPAN::Types::TypeTiny qw( AbsPath ArrayRef Str );
use MetaCPAN::Util qw( fix_version true false );
use Module::Metadata 1.000012 (); # Improved package detection.
use CPAN::DistnameInfo ();
use CPAN::Meta ();
use DateTime ();
use File::Find ();
use File::Spec ();
use Log::Contextual::Easy::Default qw( :log :dlog );
use MetaCPAN::ESConfig qw( es_doc_path );
use MetaCPAN::Model::Archive ();
use MetaCPAN::Types::TypeTiny qw( AbsPath ArrayRef InstanceOf Str );
use MetaCPAN::Util qw( fix_version true false );
use Module::Metadata 1.000012 (); # Improved package detection.
use MooseX::StrictConstructor;
use Parse::PMFile ();
use Path::Tiny qw( path );
use Try::Tiny qw( catch try );

with 'MetaCPAN::Role::Logger';

has archive => (
is => 'ro',
isa => 'MetaCPAN::Model::Archive',
isa => InstanceOf ['MetaCPAN::Model::Archive'],
lazy => 1,
builder => '_build_archive',
);
Expand All @@ -38,7 +36,7 @@ has dependencies => (

has distinfo => (
is => 'ro',
isa => 'CPAN::DistnameInfo',
isa => InstanceOf ['CPAN::DistnameInfo'],
handles => {
maturity => 'maturity',
author => 'cpanid',
Expand All @@ -55,7 +53,7 @@ has distinfo => (

has document => (
is => 'ro',
isa => 'MetaCPAN::Document::Release',
isa => InstanceOf ['MetaCPAN::Document::Release'],
lazy => 1,
builder => '_build_document',
);
Expand All @@ -77,7 +75,7 @@ has files => (

has date => (
is => 'ro',
isa => 'DateTime',
isa => InstanceOf ['DateTime'],
lazy => 1,
default => sub {
my $self = shift;
Expand All @@ -89,7 +87,7 @@ has model => ( is => 'ro' );

has metadata => (
is => 'ro',
isa => 'CPAN::Meta',
isa => InstanceOf ['CPAN::Meta'],
lazy => 1,
builder => '_build_metadata',
);
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
1 change: 1 addition & 0 deletions lib/MetaCPAN/Role/Logger.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use Moose::Role;
use Log::Contextual qw( set_logger );
use Log::Log4perl ':easy';
use MetaCPAN::Types::TypeTiny qw( Logger Str );
use MooseX::Getopt ();
use Path::Tiny qw( path );

has level => (
Expand Down
22 changes: 13 additions & 9 deletions lib/MetaCPAN/Role/Script.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ 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( 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, ES ) {
MooseX::Getopt::OptionTypeMap->add_option_type_to_map( $type, '=s' );
}

with( 'MetaCPAN::Role::HasConfig', 'MetaCPAN::Role::Fastly',
'MetaCPAN::Role::Logger' );
Expand Down
2 changes: 0 additions & 2 deletions lib/MetaCPAN/Script/Release.pm
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,6 @@ sub _get_release_model {
distinfo => $d,
file => $archive_path,
model => $self->model,
level => $self->level,
logger => $self->logger,
status => $self->detect_status( $d->cpanid, $d->filename ),
);

Expand Down
5 changes: 3 additions & 2 deletions lib/MetaCPAN/Script/Snapshot.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use DateTime::Format::ISO8601 ();
use HTTP::Tiny ();
use Log::Contextual qw( :log :dlog );
use MetaCPAN::Server::Config ();
use MetaCPAN::Types::TypeTiny qw( ArrayRef Bool Str );
use MetaCPAN::Types::TypeTiny qw( ArrayRef Bool Str Uri );
use Moose;
use Sys::Hostname qw( hostname );

Expand Down Expand Up @@ -81,7 +81,8 @@ has snap_name => (

has host => (
is => 'ro',
isa => 'URI::http',
isa => Uri,
coerce => 1,
default => sub {
my $self = shift;
return $self->es->transport->cxn_pool->cxns->[0]->uri;
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
35 changes: 28 additions & 7 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 @@ -121,13 +123,6 @@ coerce HashRefCPANMeta, from InstanceOf ['CPAN::Meta'], via {
return $struct ? $struct : $_->as_struct;
};

# optionally add Getopt option type (adapted from MooseX::Types:Path::Class)
if ( eval { require MooseX::Getopt; 1 } ) {
for my $type ( Path, AbsPath ) {
MooseX::Getopt::OptionTypeMap->add_option_type_to_map( $type, '=s' );
}
}

declare CommaSepOption, as ArrayRef [ StrMatch [qr{^[^, ]+$}] ];
coerce CommaSepOption, from ArrayRef [Str], via {
return [ map split(/\s*,\s*/), @$_ ];
Expand All @@ -136,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;
11 changes: 6 additions & 5 deletions t/lib/MetaCPAN/Script/MockError.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,46 @@ package MetaCPAN::Script::MockError;

use Moose;
use Exception::Class ('MockException');
use MetaCPAN::Types::TypeTiny qw( Bool Int Str );

with 'MetaCPAN::Role::Script', 'MooseX::Getopt';

has arg_error_message => (
init_arg => 'message',
is => 'ro',
isa => 'Str',
isa => Str,
default => "",
documentation => 'mock an Error Message',
);

has arg_error_code => (
init_arg => 'error',
is => 'ro',
isa => 'Int',
isa => Int,
default => -1,
documentation => 'mock an Exit Code',
);

has arg_die => (
init_arg => 'die',
is => 'ro',
isa => 'Bool',
isa => Bool,
default => 0,
documentation => 'mock an Exception',
);

has arg_handle_error => (
init_arg => 'handle_error',
is => 'ro',
isa => 'Bool',
isa => Bool,
default => 0,
documentation => 'mock a handled error',
);

has arg_exception => (
init_arg => 'exception',
is => 'ro',
isa => 'Bool',
isa => Bool,
default => 0,
documentation => 'mock an Exception Class',
);
Expand Down
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
3 changes: 2 additions & 1 deletion t/lib/MetaCPAN/Tests/Extra.pm
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package MetaCPAN::Tests::Extra;
use Test::More;
use Test::Routine;
use MetaCPAN::Types::TypeTiny qw( CodeRef );

has _extra_tests => (
is => 'ro',
isa => 'CodeRef',
isa => CodeRef,
init_arg => 'extra_tests',
predicate => 'has_extra_tests',
);
Expand Down
4 changes: 2 additions & 2 deletions t/lib/MetaCPAN/Tests/Model.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package MetaCPAN::Tests::Model;
use Test::Routine;

use MetaCPAN::Server::Test ();
use MetaCPAN::Types::TypeTiny qw( ArrayRef HashRef Str );
use MetaCPAN::Types::TypeTiny qw( ArrayRef HashRef InstanceOf Str );
use Test::More;
use Try::Tiny qw( try );

Expand Down Expand Up @@ -42,7 +42,7 @@ has _type => (

has model => (
is => 'ro',
isa => 'MetaCPAN::Model',
isa => InstanceOf ['MetaCPAN::Model'],
lazy => 1,
default => sub { MetaCPAN::Server::Test::model() },
);
Expand Down
Loading

0 comments on commit 82201c6

Please sign in to comment.