Skip to content

Commit 82201c6

Browse files
authored
Merge pull request #1341 from metacpan/haarg/type-tiny
Use Type::Tiny based types in most cases
2 parents f72eadb + 02c1d5a commit 82201c6

File tree

20 files changed

+100
-89
lines changed

20 files changed

+100
-89
lines changed

lib/Catalyst/Authentication/Store/Proxy.pm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package Catalyst::Authentication::Store::Proxy;
33
# ABSTRACT: Delegates authentication logic to the user object
44
use Moose;
55
use Catalyst::Utils ();
6-
use MetaCPAN::Types::TypeTiny qw( HashRef Str );
6+
use MetaCPAN::Types::TypeTiny qw( ClassName HashRef Str );
77

88
has user_class => (
99
is => 'ro',
@@ -14,7 +14,7 @@ has user_class => (
1414
);
1515
has handles => ( is => 'ro', isa => HashRef );
1616
has config => ( is => 'ro', isa => HashRef );
17-
has app => ( is => 'ro', isa => 'ClassName' );
17+
has app => ( is => 'ro', isa => ClassName );
1818
has realm => ( is => 'ro' );
1919

2020
sub BUILDARGS {

lib/Catalyst/Plugin/Session/Store/ElasticSearch.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package Catalyst::Plugin::Session::Store::ElasticSearch;
44

55
use Moose;
66
extends 'Catalyst::Plugin::Session::Store';
7-
use MooseX::Types::ElasticSearch qw( ES );
7+
use MetaCPAN::Types::TypeTiny qw( ES );
88

99
use MetaCPAN::ESConfig qw( es_doc_path );
1010
use MetaCPAN::Server::Config ();

lib/MetaCPAN/API.pm

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ To run the api web server, run the following on one of the servers:
1919

2020
use Mojo::Base 'Mojolicious';
2121

22-
use File::Temp ();
23-
use List::Util qw( any );
24-
use MetaCPAN::Script::Runner ();
25-
use Search::Elasticsearch ();
26-
use Try::Tiny qw( catch try );
27-
use MetaCPAN::Server::Config ();
28-
use MooseX::Types::ElasticSearch qw(ES);
22+
use File::Temp ();
23+
use List::Util qw( any );
24+
use MetaCPAN::Script::Runner ();
25+
use Search::Elasticsearch ();
26+
use Try::Tiny qw( catch try );
27+
use MetaCPAN::Server::Config ();
28+
use MetaCPAN::Types::TypeTiny qw( ES );
2929

3030
has es => sub {
3131
ES->assert_coerce(

lib/MetaCPAN/Model/Archive.pm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package MetaCPAN::Model::Archive;
33
use v5.10;
44
use Moose;
55
use MooseX::StrictConstructor;
6-
use MetaCPAN::Types::TypeTiny qw( AbsPath ArrayRef Bool Str );
6+
use MetaCPAN::Types::TypeTiny qw( AbsPath ArrayRef Bool InstanceOf Str );
77

88
use Archive::Any ();
99
use Carp qw( croak );
@@ -52,7 +52,7 @@ has file => (
5252

5353
has _extractor => (
5454
is => 'ro',
55-
isa => 'Archive::Any',
55+
isa => InstanceOf ['Archive::Any'],
5656
handles => [ qw(
5757
is_impolite
5858
is_naughty

lib/MetaCPAN/Model/Release.pm

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,25 @@ use Moose;
44

55
use v5.10;
66

7-
use CPAN::DistnameInfo ();
8-
use CPAN::Meta ();
9-
use DateTime ();
10-
use File::Find ();
11-
use File::Spec ();
12-
use Log::Contextual qw( :log :dlog );
13-
use MetaCPAN::ESConfig qw( es_doc_path );
14-
use MetaCPAN::Model::Archive ();
15-
use MetaCPAN::Types::TypeTiny qw( AbsPath ArrayRef Str );
16-
use MetaCPAN::Util qw( fix_version true false );
17-
use Module::Metadata 1.000012 (); # Improved package detection.
7+
use CPAN::DistnameInfo ();
8+
use CPAN::Meta ();
9+
use DateTime ();
10+
use File::Find ();
11+
use File::Spec ();
12+
use Log::Contextual::Easy::Default qw( :log :dlog );
13+
use MetaCPAN::ESConfig qw( es_doc_path );
14+
use MetaCPAN::Model::Archive ();
15+
use MetaCPAN::Types::TypeTiny qw( AbsPath ArrayRef InstanceOf Str );
16+
use MetaCPAN::Util qw( fix_version true false );
17+
use Module::Metadata 1.000012 (); # Improved package detection.
1818
use MooseX::StrictConstructor;
1919
use Parse::PMFile ();
2020
use Path::Tiny qw( path );
2121
use Try::Tiny qw( catch try );
2222

23-
with 'MetaCPAN::Role::Logger';
24-
2523
has archive => (
2624
is => 'ro',
27-
isa => 'MetaCPAN::Model::Archive',
25+
isa => InstanceOf ['MetaCPAN::Model::Archive'],
2826
lazy => 1,
2927
builder => '_build_archive',
3028
);
@@ -38,7 +36,7 @@ has dependencies => (
3836

3937
has distinfo => (
4038
is => 'ro',
41-
isa => 'CPAN::DistnameInfo',
39+
isa => InstanceOf ['CPAN::DistnameInfo'],
4240
handles => {
4341
maturity => 'maturity',
4442
author => 'cpanid',
@@ -55,7 +53,7 @@ has distinfo => (
5553

5654
has document => (
5755
is => 'ro',
58-
isa => 'MetaCPAN::Document::Release',
56+
isa => InstanceOf ['MetaCPAN::Document::Release'],
5957
lazy => 1,
6058
builder => '_build_document',
6159
);
@@ -77,7 +75,7 @@ has files => (
7775

7876
has date => (
7977
is => 'ro',
80-
isa => 'DateTime',
78+
isa => InstanceOf ['DateTime'],
8179
lazy => 1,
8280
default => sub {
8381
my $self = shift;
@@ -89,7 +87,7 @@ has model => ( is => 'ro' );
8987

9088
has metadata => (
9189
is => 'ro',
92-
isa => 'CPAN::Meta',
90+
isa => InstanceOf ['CPAN::Meta'],
9391
lazy => 1,
9492
builder => '_build_metadata',
9593
);

lib/MetaCPAN/Query.pm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package MetaCPAN::Query;
22
use Moose;
33

4-
use Module::Runtime qw( require_module );
5-
use Module::Pluggable::Object ();
6-
use MooseX::Types::ElasticSearch qw( ES );
4+
use Module::Runtime qw( require_module );
5+
use Module::Pluggable::Object ();
6+
use MetaCPAN::Types::TypeTiny qw( ES );
77

88
has es => (
99
is => 'ro',

lib/MetaCPAN/Query/Role/Common.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package MetaCPAN::Query::Role::Common;
22
use Moose::Role;
33

4-
use MooseX::Types::ElasticSearch qw( ES );
4+
use MetaCPAN::Types::TypeTiny qw( ES );
55

66
has es => (
77
is => 'ro',

lib/MetaCPAN/Role/Logger.pm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use Moose::Role;
66
use Log::Contextual qw( set_logger );
77
use Log::Log4perl ':easy';
88
use MetaCPAN::Types::TypeTiny qw( Logger Str );
9+
use MooseX::Getopt ();
910
use Path::Tiny qw( path );
1011

1112
has level => (

lib/MetaCPAN/Role/Script.pm

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@ package MetaCPAN::Role::Script;
22

33
use Moose::Role;
44

5-
use Carp ();
6-
use MooseX::Types::ElasticSearch qw( ES );
7-
use IO::Prompt::Tiny qw( prompt );
8-
use Log::Contextual qw( :log :dlog );
9-
use MetaCPAN::Model ();
10-
use MetaCPAN::Types::TypeTiny qw( Bool HashRef Int Path Str );
11-
use MetaCPAN::Util qw( root_dir );
12-
use Mojo::Server ();
13-
use Term::ANSIColor qw( colored );
5+
use Carp ();
6+
use IO::Prompt::Tiny qw( prompt );
7+
use Log::Contextual qw( :log :dlog );
8+
use MetaCPAN::Model ();
9+
use MetaCPAN::Types::TypeTiny qw( AbsPath Bool ES HashRef Int Path Str );
10+
use MetaCPAN::Util qw( root_dir );
11+
use Mojo::Server ();
12+
use Term::ANSIColor qw( colored );
13+
14+
use MooseX::Getopt::OptionTypeMap ();
15+
for my $type ( Path, AbsPath, ES ) {
16+
MooseX::Getopt::OptionTypeMap->add_option_type_to_map( $type, '=s' );
17+
}
1418

1519
with( 'MetaCPAN::Role::HasConfig', 'MetaCPAN::Role::Fastly',
1620
'MetaCPAN::Role::Logger' );

lib/MetaCPAN/Script/Release.pm

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,6 @@ sub _get_release_model {
270270
distinfo => $d,
271271
file => $archive_path,
272272
model => $self->model,
273-
level => $self->level,
274-
logger => $self->logger,
275273
status => $self->detect_status( $d->cpanid, $d->filename ),
276274
);
277275

lib/MetaCPAN/Script/Snapshot.pm

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use DateTime::Format::ISO8601 ();
99
use HTTP::Tiny ();
1010
use Log::Contextual qw( :log :dlog );
1111
use MetaCPAN::Server::Config ();
12-
use MetaCPAN::Types::TypeTiny qw( ArrayRef Bool Str );
12+
use MetaCPAN::Types::TypeTiny qw( ArrayRef Bool Str Uri );
1313
use Moose;
1414
use Sys::Hostname qw( hostname );
1515

@@ -81,7 +81,8 @@ has snap_name => (
8181

8282
has host => (
8383
is => 'ro',
84-
isa => 'URI::http',
84+
isa => Uri,
85+
coerce => 1,
8586
default => sub {
8687
my $self = shift;
8788
return $self->es->transport->cxn_pool->cxns->[0]->uri;

lib/MetaCPAN/Server/Model/ES.pm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package MetaCPAN::Server::Model::ES;
22

33
use Moose;
44

5-
use MetaCPAN::Server::Config ();
6-
use MooseX::Types::ElasticSearch qw( ES );
5+
use MetaCPAN::Server::Config ();
6+
use MetaCPAN::Types::TypeTiny qw( ES );
77

88
extends 'Catalyst::Model';
99

lib/MetaCPAN/Types/TypeTiny.pm

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ use Type::Library -base, -declare => ( qw(
2020
HashRefCPANMeta
2121
2222
CommaSepOption
23+
24+
ES
2325
) );
2426
use Type::Utils qw( as coerce declare extends from via );
2527

@@ -121,13 +123,6 @@ coerce HashRefCPANMeta, from InstanceOf ['CPAN::Meta'], via {
121123
return $struct ? $struct : $_->as_struct;
122124
};
123125

124-
# optionally add Getopt option type (adapted from MooseX::Types:Path::Class)
125-
if ( eval { require MooseX::Getopt; 1 } ) {
126-
for my $type ( Path, AbsPath ) {
127-
MooseX::Getopt::OptionTypeMap->add_option_type_to_map( $type, '=s' );
128-
}
129-
}
130-
131126
declare CommaSepOption, as ArrayRef [ StrMatch [qr{^[^, ]+$}] ];
132127
coerce CommaSepOption, from ArrayRef [Str], via {
133128
return [ map split(/\s*,\s*/), @$_ ];
@@ -136,4 +131,30 @@ coerce CommaSepOption, from Str, via {
136131
return [ map split(/\s*,\s*/), $_ ];
137132
};
138133

134+
declare ES, as Object;
135+
coerce ES, from Str, via {
136+
my $server = $_;
137+
$server = "127.0.0.1$server" if ( $server =~ /^:/ );
138+
return Search::Elasticsearch->new(
139+
nodes => $server,
140+
cxn => 'HTTPTiny',
141+
);
142+
};
143+
144+
coerce ES, from HashRef, via {
145+
return Search::Elasticsearch->new( {
146+
cxn => 'HTTPTiny',
147+
%$_,
148+
} );
149+
};
150+
151+
coerce ES, from ArrayRef, via {
152+
my @servers = @$_;
153+
@servers = map { /^:/ ? "127.0.0.1$_" : $_ } @servers;
154+
return Search::Elasticsearch->new(
155+
nodes => \@servers,
156+
cxn => 'HTTPTiny',
157+
);
158+
};
159+
139160
1;

t/lib/MetaCPAN/Script/MockError.pm

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,46 @@ package MetaCPAN::Script::MockError;
22

33
use Moose;
44
use Exception::Class ('MockException');
5+
use MetaCPAN::Types::TypeTiny qw( Bool Int Str );
56

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

89
has arg_error_message => (
910
init_arg => 'message',
1011
is => 'ro',
11-
isa => 'Str',
12+
isa => Str,
1213
default => "",
1314
documentation => 'mock an Error Message',
1415
);
1516

1617
has arg_error_code => (
1718
init_arg => 'error',
1819
is => 'ro',
19-
isa => 'Int',
20+
isa => Int,
2021
default => -1,
2122
documentation => 'mock an Exit Code',
2223
);
2324

2425
has arg_die => (
2526
init_arg => 'die',
2627
is => 'ro',
27-
isa => 'Bool',
28+
isa => Bool,
2829
default => 0,
2930
documentation => 'mock an Exception',
3031
);
3132

3233
has arg_handle_error => (
3334
init_arg => 'handle_error',
3435
is => 'ro',
35-
isa => 'Bool',
36+
isa => Bool,
3637
default => 0,
3738
documentation => 'mock a handled error',
3839
);
3940

4041
has arg_exception => (
4142
init_arg => 'exception',
4243
is => 'ro',
43-
isa => 'Bool',
44+
isa => Bool,
4445
default => 0,
4546
documentation => 'mock an Exception Class',
4647
);

t/lib/MetaCPAN/Server/Test.pm

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ use strict;
44
use warnings;
55
use feature qw(state);
66

7-
use HTTP::Request::Common qw( DELETE GET POST ); ## no perlimports
8-
use MetaCPAN::Model ();
9-
use MetaCPAN::Server ();
10-
use MetaCPAN::Server::Config ();
11-
use MooseX::Types::ElasticSearch qw( ES );
12-
use Plack::Test; ## no perlimports
7+
use HTTP::Request::Common qw( DELETE GET POST ); ## no perlimports
8+
use MetaCPAN::Model ();
9+
use MetaCPAN::Server ();
10+
use MetaCPAN::Server::Config ();
11+
use MetaCPAN::Types::TypeTiny qw( ES );
12+
use Plack::Test; ## no perlimports
1313

1414
use base 'Exporter';
1515
our @EXPORT_OK = qw(

t/lib/MetaCPAN/Tests/Extra.pm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package MetaCPAN::Tests::Extra;
22
use Test::More;
33
use Test::Routine;
4+
use MetaCPAN::Types::TypeTiny qw( CodeRef );
45

56
has _extra_tests => (
67
is => 'ro',
7-
isa => 'CodeRef',
8+
isa => CodeRef,
89
init_arg => 'extra_tests',
910
predicate => 'has_extra_tests',
1011
);

t/lib/MetaCPAN/Tests/Model.pm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package MetaCPAN::Tests::Model;
33
use Test::Routine;
44

55
use MetaCPAN::Server::Test ();
6-
use MetaCPAN::Types::TypeTiny qw( ArrayRef HashRef Str );
6+
use MetaCPAN::Types::TypeTiny qw( ArrayRef HashRef InstanceOf Str );
77
use Test::More;
88
use Try::Tiny qw( try );
99

@@ -42,7 +42,7 @@ has _type => (
4242

4343
has model => (
4444
is => 'ro',
45-
isa => 'MetaCPAN::Model',
45+
isa => InstanceOf ['MetaCPAN::Model'],
4646
lazy => 1,
4747
default => sub { MetaCPAN::Server::Test::model() },
4848
);

0 commit comments

Comments
 (0)