App-Oozie

 view release on metacpan or  search on metacpan

lib/App/Oozie/Serializer.pm  view on Meta::CPAN

);

has slurp => (
    is      => 'rw',
    isa     => Int,
    default => sub { 0 },
);

has __file => (
    is => 'rwp',
);

has '__object' => (
    is => 'rwp',
);

sub BUILD {
    my ($self, $args) = @_;
    my $s = VALID_SERIALIZERS->{ $args->{format} };

    if ( ! $s ) {
        my $default = 'dummy';
        warn sprintf '%s is an unknown serializer, falling back to %s',
                        $args->{format},
                        $default,
        ;
        $self->format( $default );
        ($s) = grep { m{::${default} \z}xmsi } values %{ +VALID_SERIALIZERS };
    }

    if ( ! $s ) {
        # Shouldn't happen, apart from a possible future code change
        die 'Failed to locate a serializer!';
    }

    $self->_set___object( load_plugin( $s )->new );

    return;
}

sub encode {
    my $self = shift;
    my $data = shift || die 'Nothing to encode!';

    die 'The data to encode needs to be a reference' if ! ref $data;

    $self->_assert_type( $data ) if $self->enforce_type;

    return $self->__object->encode( $data )
}

sub decode {
    my $self = shift;
    my $data = shift || die 'Nothing to decode!';

    die q{The data to decode can't be a reference!} if ref $data;

    my $is_file = $self->slurp && $data !~ m{ \n }xms && -e $data && -f _;

    my $rv = $self->__object->decode(
          $is_file            ? do { local(@ARGV, $/) = $data; <> }
        : $data eq 'meta.yml' ? die 'Only a file name (which does not exist) passed as meta data'
                 : $data
    );

    if ( $is_file ) {
       $self->_set___file( $data );
    }

    $self->_assert_type( $rv ) if $self->enforce_type;

    return $rv;
}

sub _assert_type {
    my $self  = shift;
    my $input = shift || die 'No data specified to enforce a type!';
    my $type  = $self->enforce_type || return;

    my $failed   = $type->validate_explain( $input, 'USER_INPUT' ) || return;
    my $full_msg = join "\n\n", @{ $failed };
    require Data::Dumper;
    my $d        = Data::Dumper->new( [ $input ], [ '*INPUT' ] );

    die sprintf <<'DID_NOT_PASS', $full_msg, $d->Dump;
The data structure does not match the type definition: %s

Input was decoded as (compare to the errors above):

%s
DID_NOT_PASS
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

App::Oozie::Serializer

=head1 VERSION

version 0.016

=head1 SYNOPSIS

    use App::Oozie::Serializer;
    my $s = App::Oozie::Serializer->new(
        # ...
        format => 'yaml',
    );
    my $d = $s->decode( $input );

=head1 DESCRIPTION

Internal serializer.

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.614 second using v1.00-cache-2.02-grep-82fe00e-cpan-f73e49a70403 )