Skip to content

Commit

Permalink
Support builtin booleans by default
Browse files Browse the repository at this point in the history
  • Loading branch information
perlpunk committed Jan 28, 2024
1 parent b44fa34 commit e282727
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 13 deletions.
13 changes: 5 additions & 8 deletions lib/YAML/PP/Schema.pm
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,16 @@ sub new {
$false ||= \&_bool_booleanpm_false;
push @bool_class, 'boolean';
}
elsif ($b eq 'perl') {
$true ||= \&_bool_perl_true;
$false ||= \&_bool_perl_false;
}
elsif ($b eq 'perl_experimental') {
$true ||= \&_bool_perl_true;
$false ||= \&_bool_perl_false;
push @bool_class, 'perl_experimental';
elsif ($b eq 'perl' or $b eq 'perl_experimental') {
push @bool_class, 'perl';
}
else {
die "Invalid value for 'boolean': '$b'. Allowed: ('perl', 'boolean', 'JSON::PP')";
}
}
# Ensure booleans are resolved
$true ||= \&_bool_perl_true;
$false ||= \&_bool_perl_false;

my %representers = (
'undef' => undef,
Expand Down
4 changes: 2 additions & 2 deletions lib/YAML/PP/Schema/Core.pm
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ sub register {

if ($schema->bool_class) {
for my $class (@{ $schema->bool_class }) {
if ($class eq 'perl_experimental') {
if ($class eq 'perl') {
$schema->add_representer(
bool => $class,
bool => 1,
code => \&represent_bool,
);
next;
Expand Down
4 changes: 2 additions & 2 deletions lib/YAML/PP/Schema/JSON.pm
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ sub register {

if ($schema->bool_class) {
for my $class (@{ $schema->bool_class }) {
if ($class eq 'perl_experimental') {
if ($class eq 'perl') {
$schema->add_representer(
bool => $class,
bool => 1,
code => \&represent_bool,
);
next;
Expand Down
7 changes: 7 additions & 0 deletions lib/YAML/PP/Schema/YAML1_1.pm
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,13 @@ sub register {

if ($schema->bool_class) {
for my $class (@{ $schema->bool_class }) {
if ($class eq 'perl') {
$schema->add_representer(
bool => 1,
code => \&represent_bool,
);
next;
}
$schema->add_representer(
class_equals => $class,
code => \&represent_bool,
Expand Down
15 changes: 15 additions & 0 deletions t/22.dump-bool.t
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,21 @@ true1: true
EOM
cmp_ok($yaml, 'eq', $exp_json_pp, "perl_experimental dump");
}
SKIP: {
skip "perl version < v5.36", 1 unless $] >= 5.036000;
my $data = {
"true1" => !!1,
"false1" => !!0,
};
my $yppd = YAML::PP->new(boolean => '');
my $yaml = $yppd->dump_string($data);
my $exp_json_pp = <<'EOM';
---
false1: ''
true1: 1
EOM
cmp_ok($yaml, 'eq', $exp_json_pp, "no booleans dump");
}

SKIP: {
skip "perl version < v5.36", 3 unless $] >= 5.036000;
Expand Down
2 changes: 1 addition & 1 deletion t/51.directives.t
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ EOM

my %args= (
schema => [qw/ + /],
boolean => 'perl',
boolean => '',
version_directive => 1,
);

Expand Down

0 comments on commit e282727

Please sign in to comment.