Skip to content

Commit

Permalink
use real booleans when interacting with Elasticsearch
Browse files Browse the repository at this point in the history
Fixes #1273
  • Loading branch information
haarg committed Sep 30, 2024
1 parent 6679936 commit 65dd216
Show file tree
Hide file tree
Showing 43 changed files with 269 additions and 216 deletions.
9 changes: 4 additions & 5 deletions lib/MetaCPAN/Document/Author.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,18 @@ use ElasticSearchX::Model::Document;

# load order not important
use Gravatar::URL ();
use MetaCPAN::Types qw( Profile );
use MetaCPAN::Types qw( ESBool Profile );
use MetaCPAN::Types::TypeTiny qw(
ArrayRef
ArrayRefPromote
Blog
Bool
Dict
HashRef
NonEmptySimpleStr
PerlMongers
Str
);
use MetaCPAN::Util;
use MetaCPAN::Util qw(true false);

has name => (
is => 'ro',
Expand Down Expand Up @@ -105,9 +104,9 @@ has updated => (

has is_pause_custodial_account => (
is => 'ro',
isa => Bool,
isa => ESBool,
coerce => 1,
default => 0,
default => sub {false},
);

sub _build_gravatar_url {
Expand Down
5 changes: 3 additions & 2 deletions lib/MetaCPAN/Document/Distribution.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use Moose;
use ElasticSearchX::Model::Document;

use MetaCPAN::Types::TypeTiny qw( BugSummary RiverSummary );
use MetaCPAN::Util qw(true false);

has name => (
is => 'ro',
Expand Down Expand Up @@ -49,11 +50,11 @@ sub set_first_release {
my @releases = $self->releases->sort( ["date"] )->all;

my $first = shift @releases;
$first->_set_first(1);
$first->_set_first(true);
$first->put;

for my $rel (@releases) {
$rel->_set_first(0);
$rel->_set_first(false);
$rel->put;
}

Expand Down
59 changes: 30 additions & 29 deletions lib/MetaCPAN/Document/File.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@ use ElasticSearchX::Model::Document;

use List::Util qw( any );
use MetaCPAN::Document::Module ();
use MetaCPAN::Types qw( Module );
use MetaCPAN::Types qw( ESBool Module );
use MetaCPAN::Types::TypeTiny qw(
ArrayRef
Bool
Int
Maybe
Num
ScalarRef
Stat
Str
);
use MetaCPAN::Util qw(numify_version);
use MetaCPAN::Util qw(numify_version true false);
use Plack::MIME ();
use Pod::Text ();
use Try::Tiny qw( catch try );
Expand Down Expand Up @@ -50,8 +49,8 @@ it is also set if the entire release is marked deprecated (see L<MetaCPAN::Docum

has deprecated => (
is => 'ro',
isa => Bool,
default => 0,
isa => ESBool,
default => sub {false},
writer => '_set_deprecated',
);

Expand Down Expand Up @@ -260,9 +259,9 @@ File is binary or not.

has binary => (
is => 'ro',
isa => Bool,
isa => ESBool,
required => 1,
default => 0,
default => sub {false},
);

=head2 authorized
Expand All @@ -274,8 +273,8 @@ See L</set_authorized>.
has authorized => (
required => 1,
is => 'ro',
isa => Bool,
default => 1,
isa => ESBool,
default => sub {true},
writer => '_set_authorized',
);

Expand All @@ -301,8 +300,8 @@ Return true if this object represents a directory.
has directory => (
is => 'ro',
required => 1,
isa => Bool,
default => 0,
isa => ESBool,
default => sub {false},
);

=head2 documentation
Expand Down Expand Up @@ -433,13 +432,13 @@ not. See L</set_indexed> for a more verbose explanation.
has indexed => (
required => 1,
is => 'ro',
isa => Bool,
isa => ESBool,
lazy => 1,
default => sub {
my ($self) = @_;
return 0 if $self->is_in_other_files;
return 0 if !$self->metadata->should_index_file( $self->path );
return 1;
return false if $self->is_in_other_files;
return false if !$self->metadata->should_index_file( $self->path );
return true;
},
writer => '_set_indexed',
);
Expand Down Expand Up @@ -897,24 +896,24 @@ sub set_indexed {
if ( exists $meta->provides->{ $mod->name }
and $self->path eq $meta->provides->{ $mod->name }{file} )
{
$mod->_set_indexed(1);
$mod->_set_indexed(true);
return;
}
}

# files listed under 'other files' are not shown in a search
if ( $self->is_in_other_files() ) {
foreach my $mod ( @{ $self->module } ) {
$mod->_set_indexed(0);
$mod->_set_indexed(false);
}
$self->_set_indexed(0);
$self->_set_indexed(false);
return;
}

# files under no_index directories should not be indexed
foreach my $dir ( @{ $meta->no_index->{directory} } ) {
if ( $self->path eq $dir or $self->path =~ /^$dir\// ) {
$self->_set_indexed(0);
$self->_set_indexed(false);
return;
}
}
Expand All @@ -923,24 +922,26 @@ sub set_indexed {
if ( $mod->name !~ /^[A-Za-z]/
or !$meta->should_index_package( $mod->name ) )
{
$mod->_set_indexed(0);
$mod->_set_indexed(false);
next;
}

$mod->_set_indexed(
$mod->hide_from_pause( ${ $self->content }, $self->name )
? 0
: 1
? false
: true
);
}

$self->_set_indexed(
(

# .pm file with no package declaration but pod should be indexed
!@{ $self->module } ||
# .pm file with no package declaration but pod should be indexed
!@{ $self->module } ||

# don't index if the documentation doesn't match any of its modules
!!grep { $self->documentation eq $_->name } @{ $self->module }
!!grep { $self->documentation eq $_->name } @{ $self->module }
) ? true : false
) if ( $self->documentation );
}

Expand Down Expand Up @@ -974,18 +975,18 @@ sub set_authorized {
if ( $self->distribution eq 'perl' ) {
my $allowed = grep $_ eq $self->author, @{ $perms->{perl} };
foreach my $module ( @{ $self->module } ) {
$module->_set_authorized($allowed);
$module->_set_authorized( $allowed ? true : false );
}
$self->_set_authorized($allowed);
$self->_set_authorized( $allowed ? true : false );
}
else {
foreach my $module ( @{ $self->module } ) {
$module->_set_authorized(0)
$module->_set_authorized(false)
if ( $perms->{ $module->name }
&& !grep { $_ eq $self->author }
@{ $perms->{ $module->name } } );
}
$self->_set_authorized(0)
$self->_set_authorized(false)
if ( $self->authorized
&& $self->documentation
&& $perms->{ $self->documentation }
Expand Down
39 changes: 21 additions & 18 deletions lib/MetaCPAN/Document/File/Set.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use List::Util qw( max );
use MetaCPAN::Query::Favorite ();
use MetaCPAN::Query::File ();
use MetaCPAN::Query::Release ();
use MetaCPAN::Util qw( single_valued_arrayref_to_scalar );
use MetaCPAN::Util qw( single_valued_arrayref_to_scalar true false );

extends 'ElasticSearchX::Model::Document::Set';

Expand Down Expand Up @@ -78,8 +78,8 @@ sub find {
my @candidates = $self->index->type('file')->query( {
bool => {
must => [
{ term => { indexed => 1 } },
{ term => { authorized => 1 } },
{ term => { indexed => true } },
{ term => { authorized => true } },
{ term => { status => 'latest' } },
{
bool => {
Expand All @@ -100,8 +100,8 @@ sub find {
[
{ term =>
{ "module.authorized"
=> 1 }
},
=> true
} },
{ exists =>
{ field =>
'module.associated_pod'
Expand Down Expand Up @@ -175,7 +175,10 @@ sub documented_modules {
exists =>
{ field => 'module.name' }
},
{ term => { 'module.indexed' => 1 } },
{
term =>
{ 'module.indexed' => true }
},
],
}
},
Expand All @@ -186,7 +189,7 @@ sub documented_modules {
exists =>
{ field => 'pod.analyzed' }
},
{ term => { indexed => 1 } },
{ term => { indexed => true } },
],
}
},
Expand Down Expand Up @@ -217,8 +220,8 @@ sub history {
filter => {
bool => {
must => [
{ term => { "module.authorized" => 1 } },
{ term => { "module.indexed" => 1 } },
{ term => { "module.authorized" => true } },
{ term => { "module.indexed" => true } },
{ term => { "module.name" => $module } },
]
}
Expand All @@ -242,8 +245,8 @@ sub history {
bool => {
must => [
{ match_phrase => { documentation => $module } },
{ term => { indexed => 1 } },
{ term => { authorized => 1 } },
{ term => { indexed => true } },
{ term => { authorized => true } },
]
}
} )
Expand All @@ -252,8 +255,8 @@ sub history {
: $self->query(
bool => {
must => [
{ term => { indexed => 1 } },
{ term => { authorized => 1 } },
{ term => { indexed => true } },
{ term => { authorized => true } },
]
}
);
Expand All @@ -279,8 +282,8 @@ sub autocomplete {
},
{ exists => { field => 'documentation' } },
{ term => { status => 'latest' } },
{ term => { indexed => 1 } },
{ term => { authorized => 1 } }
{ term => { indexed => true } },
{ term => { authorized => true } }
],
must_not =>
[ { terms => { distribution => \@ROGUE_DISTRIBUTIONS } }, ],
Expand Down Expand Up @@ -340,8 +343,8 @@ sub autocomplete_suggester {
query => {
bool => {
must => [
{ term => { indexed => 1 } },
{ term => { authorized => 1 } },
{ term => { indexed => true } },
{ term => { authorized => true } },
{ term => { status => 'latest' } },
{ terms => { documentation => [ keys %docs ] } },
],
Expand Down Expand Up @@ -390,7 +393,7 @@ sub autocomplete_suggester {
sub find_changes_files {
my ( $self, $author, $release ) = @_;
my $result = $self->files_by_category( $author, $release, ['changelog'],
{ fields => \1 } );
{ fields => true } );
my ($file) = @{ $result->{categories}{changelog} || [] };
return $file;
}
Expand Down
13 changes: 7 additions & 6 deletions lib/MetaCPAN/Document/Module.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ use ElasticSearchX::Model::Document;

with 'ElasticSearchX::Model::Document::EmbeddedRole';

use MetaCPAN::Types::TypeTiny qw( Bool Maybe Num Str );
use MetaCPAN::Util;
use MetaCPAN::Types qw( ESBool );
use MetaCPAN::Types::TypeTiny qw( Maybe Num Str );
use MetaCPAN::Util qw(true false);

=head1 SYNOPSIS
Expand Down Expand Up @@ -73,16 +74,16 @@ has version => ( is => 'ro' );
has indexed => (
is => 'ro',
required => 1,
isa => Bool,
default => 1,
isa => ESBool,
default => sub {true},
writer => '_set_indexed',
);

has authorized => (
is => 'ro',
required => 1,
isa => Bool,
default => 1,
isa => ESBool,
default => sub {true},
writer => '_set_authorized',
);

Expand Down
Loading

0 comments on commit 65dd216

Please sign in to comment.