This file is indexed.

/usr/share/perl5/SQL/Translator/Types.pm is in libsql-translator-perl 0.11024-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
package SQL::Translator::Types;

use warnings;
use strict;

=head1 NAME

SQL::Translator::Types - Type checking functions

=head1 SYNOPSIS

    package Foo;
    use Moo;
    use SQL::Translator::Types qw(schema_obj enum);

    has foo => ( is => 'rw', isa => schema_obj('Trigger') );
    has bar => ( is => 'rw', isa => enum([qw(baz quux quuz)], {
        msg => "Invalid value for bar: '%s'", icase => 1,
    });

=head1 DESCRIPTIONS

This module exports functions that return coderefs suitable for L<Moo>
C<isa> type checks.
Errors are reported using L<SQL::Translator::Utils/throw>.

=cut

use SQL::Translator::Utils qw(throw);
use Scalar::Util qw(blessed);

use Exporter qw(import);
our @EXPORT_OK = qw(schema_obj enum);

=head1 FUNCTIONS

=head2 schema_obj($type)

Returns a coderef that checks that its arguments is an object of the
class C<< SQL::Translator::Schema::I<$type> >>.

=cut

sub schema_obj {
    my ($class) = @_;
    my $name = lc $class;
    $class = 'SQL::Translator::Schema' . ($class eq 'Schema' ? '' : "::$class");
    return sub {
        throw("Not a $name object")
            unless blessed($_[0]) and $_[0]->isa($class);
    };
}

=head2 enum(\@strings, [$msg | \%parameters])

Returns a coderef that checks that the argument is one of the provided
C<@strings>.

=head3 Parameters

=over

=item msg

L<sprintf|perlfunc/sprintf> string for the error message.
If no other parameters are needed, this can be provided on its own,
instead of the C<%parameters> hashref.
The invalid value is passed as the only argument.
Defaults to C<Invalid value: '%s'>.

=item icase

If true, folds the values to lower case before checking for equality.

=item allow_undef

If true, allow C<undef> in addition to the specified strings.

=item allow_false

If true, allow any false value in addition to the specified strings.

=back

=cut

sub enum {
    my ($values, $args) = @_;
    $args ||= {};
    $args = { msg => $args } unless ref($args) eq 'HASH';
    my $icase = !!$args->{icase};
    my %values = map { ($icase ? lc : $_) => undef } @{$values};
    my $msg = $args->{msg} || "Invalid value: '%s'";
    my $extra_test =
        $args->{allow_undef} ? sub { defined $_[0] } :
        $args->{allow_false} ? sub { !!$_[0] } : undef;

    return sub {
        my $val = $icase ? lc $_[0] : $_[0];
        throw(sprintf($msg, $val))
            if (!defined($extra_test) || $extra_test->($val))
                && !exists $values{$val};
    };
}

1;