Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
perlpunk committed Jan 23, 2025
1 parent 01fc199 commit e9bf638
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 6 deletions.
2 changes: 2 additions & 0 deletions lib/YAML/PP/Schema.pm
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ sub create_sequence {
}
}
}
croak "Unknown tag '$tag'. Use schema 'Catchall' to ignore unknown tags";
}

return ($data, $on_data);
Expand Down Expand Up @@ -413,6 +414,7 @@ sub create_mapping {
}
}
}
croak "Unknown tag '$tag'. Use schema 'Catchall' to ignore unknown tags";
}

return ($data, $on_data);
Expand Down
17 changes: 16 additions & 1 deletion lib/YAML/PP/Schema/Catchall.pm
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,22 @@ sub register {
return $value;
}],
implicit => 0,
# match => [ equals => null => undef ],
);
$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;
Expand Down
16 changes: 16 additions & 0 deletions lib/YAML/PP/Schema/Failsafe.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ sub register {
tag => 'tag:yaml.org,2002:str',
match => [ all => sub { $_[1]->{value} } ],
);
$schema->add_sequence_resolver(
tag => 'tag:yaml.org,2002:seq',
on_data => sub {
my ($constructor, $ref, $list) = @_;
push @$$ref, @$list;
},
);
$schema->add_mapping_resolver(
tag => 'tag:yaml.org,2002:map',
on_data => sub {
my ($constructor, $ref, $list) = @_;
for (my $i = 0; $i < @$list; $i += 2) {
$$ref->{ $list->[ $i ] } = $list->[ $i + 1 ];
}
},
);

return;
}
Expand Down
8 changes: 4 additions & 4 deletions lib/YAML/PP/Schema/Perl.pm
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ sub register {
$schema->add_sequence_resolver(
tag => qr{^$perl_regex/array:.+$},
on_create => $load_sequence,
) if $no_objects;
);# if $no_objects;

my $load_mapping = sub { return {} };
my $load_mapping_blessed = sub {
Expand All @@ -272,7 +272,7 @@ sub register {
$schema->add_mapping_resolver(
tag => qr{^$perl_regex/hash:.+$},
on_create => $load_mapping,
) if $no_objects;
); # if $no_objects;

# Ref
my $load_ref = sub {
Expand Down Expand Up @@ -309,7 +309,7 @@ sub register {
my ($constructor, $ref, $list) = @_;
$$$ref = $self->construct_ref($list);
},
) if $no_objects;
); # if $no_objects;

# Scalar ref
my $load_scalar_ref = sub {
Expand Down Expand Up @@ -346,7 +346,7 @@ sub register {
my ($constructor, $ref, $list) = @_;
$$$ref = $self->construct_scalar($list);
},
) if $no_objects;
); # if $no_objects;

$schema->add_representer(
scalarref => 1,
Expand Down
24 changes: 23 additions & 1 deletion t/37.schema-catchall.t
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ my $catch = YAML::PP->new(
schema => [qw/ JSON Catchall /],
);

my $yaml = <<"EOM";
my $yaml = <<'EOM';
- !foo null
EOM

Expand All @@ -33,4 +33,26 @@ 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;

0 comments on commit e9bf638

Please sign in to comment.