-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Catchall schema to allow them * Add --catchall option
- Loading branch information
Showing
9 changed files
with
191 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use strict; | ||
use warnings; | ||
package YAML::PP::Schema::Catchall; | ||
|
||
our $VERSION = '0.000'; # VERSION | ||
|
||
use Carp qw/ croak /; | ||
|
||
use YAML::PP::Common qw/ YAML_PLAIN_SCALAR_STYLE YAML_SINGLE_QUOTED_SCALAR_STYLE /; | ||
|
||
sub register { | ||
my ($self, %args) = @_; | ||
my $schema = $args{schema}; | ||
my $options = $args{options}; | ||
my $empty_null = 0; | ||
for my $opt (@$options) { | ||
if ($opt eq 'empty=str') { | ||
} | ||
elsif ($opt eq 'empty=null') { | ||
$empty_null = 1; | ||
} | ||
else { | ||
croak "Invalid option for JSON Schema: '$opt'"; | ||
} | ||
} | ||
|
||
$schema->add_resolver( | ||
tag => qr{^(?:!|tag:)}, | ||
match => [ all => sub { | ||
my ($constructor, $event) = @_; | ||
my $value = $event->{value}; | ||
return $value; | ||
}], | ||
implicit => 0, | ||
); | ||
$schema->add_sequence_resolver( | ||
tag => qr{^(?:!|tag:)}, | ||
on_data => sub { | ||
my ($constructor, $ref, $list) = @_; | ||
push @$$ref, @$list; | ||
}, | ||
); | ||
$schema->add_mapping_resolver( | ||
tag => qr{^(?:!|tag:)}, | ||
on_data => sub { | ||
my ($constructor, $ref, $list) = @_; | ||
for (my $i = 0; $i < @$list; $i += 2) { | ||
$$ref->{ $list->[ $i ] } = $list->[ $i + 1 ]; | ||
} | ||
}, | ||
); | ||
|
||
return; | ||
} | ||
|
||
1; | ||
|
||
__END__ | ||
=pod | ||
=encoding utf-8 | ||
=head1 NAME | ||
YAML::PP::Schema::JSON - YAML 1.2 JSON Schema | ||
=head1 SYNOPSIS | ||
my $yp = YAML::PP->new( schema => ['JSON'] ); | ||
my $yp = YAML::PP->new( schema => [qw/ JSON empty=str /] ); | ||
my $yp = YAML::PP->new( schema => [qw/ JSON empty=null /] ); | ||
=head1 DESCRIPTION | ||
=cut |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#!/usr/bin/env perl | ||
use strict; | ||
use warnings; | ||
use Test::More; | ||
use FindBin '$Bin'; | ||
use Data::Dumper; | ||
use Scalar::Util (); | ||
use YAML::PP; | ||
|
||
my $yp = YAML::PP->new( | ||
schema => [qw/ JSON /], | ||
); | ||
my $catch = YAML::PP->new( | ||
schema => [qw/ JSON Catchall /], | ||
); | ||
|
||
my $yaml = <<'EOM'; | ||
- !foo null | ||
EOM | ||
|
||
my $data = eval { $yp->load_string($yaml) }; | ||
my $err = $@; | ||
like $err, qr{Unknown tag '!foo'. Use schema 'Catchall'}, "unknoen tags are fatal by default"; | ||
|
||
$data = $catch->load_string($yaml); | ||
is $data->[0], 'null', "Catchall loads unknown tag as string"; | ||
|
||
$yaml = "! 023"; | ||
|
||
$data = $yp->load_string($yaml); | ||
is $data, '023', "Tag '!' still works without catchall"; | ||
|
||
$data = $catch->load_string($yaml); | ||
is $data, '023', "Tag '!' still works with catchall"; | ||
|
||
$yaml = <<'EOM'; | ||
!foo | ||
- a | ||
EOM | ||
$data = eval { $yp->load_string($yaml) }; | ||
$err = $@; | ||
like $err, qr{Unknown tag '!foo'. Use schema 'Catchall'}, "unknoen tags are fatal by default"; | ||
|
||
$data = $catch->load_string($yaml); | ||
is $data->[0], 'a', "Catchall loads unknown tag on a sequence"; | ||
|
||
$yaml = <<'EOM'; | ||
!foo | ||
a: b | ||
EOM | ||
$data = eval { $yp->load_string($yaml) }; | ||
$err = $@; | ||
like $err, qr{Unknown tag '!foo'. Use schema 'Catchall'}, "unknoen tags are fatal by default"; | ||
|
||
$data = $catch->load_string($yaml); | ||
is $data->{a}, 'b', "Catchall loads unknown tag on a mapping"; | ||
|
||
done_testing; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters