Skip to content

Commit fb9c531

Browse files
committed
TextRender: support yaml multi doc rendering
1 parent 8a71d6c commit fb9c531

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

src/main/perl/TextRender.pm

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ The rendering module to use: either one of the following reserved values
125125
126126
=item json
127127
128-
JSON format (using C<JSON::XS>) (JSON true and false have to be resp. C<\1> and c<\0>)
128+
JSON format (using C<JSON::XS>) (JSON true and false have to be resp. C<\1> and C<\0>)
129129
130130
=item yaml
131131
@@ -135,6 +135,14 @@ C<$YAML_BOOL_PREFIX."false"> (There are known problems with creating hashrefs us
135135
C<< $YAML_BOOL->{yes} >> value for true; Perl seems to mess up the structure when creating
136136
the hashrefs))
137137
138+
=item yamlmulti
139+
140+
Multi-document YAML, the keys of the passed contents are sorted and
141+
the list of the corresponding values is used to generate the multi-document YAML.
142+
(The keys are only used for sorting)
143+
144+
See C<yaml> module for details on handling booleans.
145+
138146
=item properties
139147
140148
Java properties format (using C<Config::Properties>),
@@ -465,12 +473,21 @@ sub _yaml_replace_boolean_prefix
465473

466474
sub render_yaml
467475
{
468-
my ($self, $cfg) = @_;
476+
my ($self) = @_;
469477

470478
my $txt = YAML::XS::Dump($self->{contents});
471479
return $self->_yaml_replace_boolean_prefix($txt);
472480
}
473481

482+
sub render_yamlmulti
483+
{
484+
my ($self) = @_;
485+
486+
my @keys = sort keys %{$self->{contents}};
487+
my $txt = YAML::XS::Dump(@{$self->{contents}}{@keys});
488+
return $self->_yaml_replace_boolean_prefix($txt);
489+
}
490+
474491
# Warning: the rendered text has a header with localtime(),
475492
# so the contents will always appear changed.
476493
sub render_properties

src/test/perl/textrender.t

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,73 @@ $txt =~ s/\s//g;
524524
is("$txt", '---no:falseyes:true',
525525
"yaml module renders booleans true/false correctly when using prefixing");
526526

527+
=pod
528+
529+
=head3 yamlmulti
530+
531+
Test yaml/YAML::XS multi-documents
532+
533+
=cut
534+
535+
my $multicontents = {
536+
'doc0' => {
537+
'name_level0_doc0' => 'value_level0_doc0',
538+
'level1_doc0' => {
539+
'name_level1_doc0' => 'value_level1_doc0',
540+
}
541+
},
542+
'doc1' => {
543+
'name_level0_doc1' => 'value_level0_doc1',
544+
'level1_doc1' => {
545+
'name_level1_doc1' => 'value_level1_doc1',
546+
}
547+
}
548+
};
549+
550+
551+
my $multires = <<EOF;
552+
---
553+
level1_doc0:
554+
name_level1_doc0: value_level1_doc0
555+
name_level0_doc0: value_level0_doc0
556+
---
557+
level1_doc1:
558+
name_level1_doc1: value_level1_doc1
559+
name_level0_doc1: value_level0_doc1
560+
EOF
561+
$trd = CAF::TextRender->new('yamlmulti', $multicontents);
562+
ok($trd->load_module('YAML::XS'), "YAML::XS loaded");
563+
ok(! $trd->{method_is_tt}, "method_is_tt false for yamlmulti");
564+
is("$trd", $multires, "yamlmulti module rendered correctly");
565+
566+
# true/false tests
567+
$trd = CAF::TextRender->new('yamlmulti', {'a' => $YAML_BOOL}, eol=>0);
568+
$txt = "$trd";
569+
$txt =~ s/\s//g;
570+
is($txt, '---no:falseyes:true',
571+
"yamlmulti module renders booleans true/false correctly");
572+
573+
# but this goes wrong
574+
$trd = CAF::TextRender->new('yamlmulti', {
575+
'a' => {'yes' => $YAML_BOOL->{'yes'}},
576+
'b' => {'no' => $YAML_BOOL->{'no'}},
577+
}, eol=>0);
578+
579+
$txt = "$trd";
580+
$txt =~ s/\s//g;
581+
is("$txt", "---yes:1---no:''",
582+
"yamlmulti module renders booleans true/false incorrect when constructing hashref");
583+
584+
# so use the CAF::TextRender prefixing
585+
$trd = CAF::TextRender->new('yamlmulti', {
586+
'a' => {'yes' => $YAML_BOOL_PREFIX."true"},
587+
'b' => {'no' => $YAML_BOOL_PREFIX."false"},
588+
}, eol=>0);
589+
$txt = "$trd";
590+
$txt =~ s/\s//g;
591+
is("$txt", '---yes:true---no:false',
592+
"yamlmulti module renders booleans true/false correctly when using prefixing");
593+
527594

528595
=pod
529596

0 commit comments

Comments
 (0)