File Coverage

blib/lib/YAML/PP/Schema/Merge.pm
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod 1 1 100.0
total 17 17 100.0


line stmt bran cond sub pod time code
1 2     2   164335 use strict;
  2         4  
  2         79  
2 2     2   10 use warnings;
  2         3  
  2         212  
3             package YAML::PP::Schema::Merge;
4              
5             our $VERSION = 'v0.39.0'; # VERSION
6              
7 2     2   780 use YAML::PP::Type::MergeKey;
  2         6  
  2         151  
8              
9             sub register {
10 1     1 1 4 my ($self, %args) = @_;
11 1         2 my $schema = $args{schema};
12              
13 1         3 $schema->add_resolver(
14             tag => 'tag:yaml.org,2002:merge',
15             match => [ equals => '<<' => YAML::PP::Type::MergeKey->new ],
16             );
17             }
18              
19             1;
20              
21             __END__
22              
23             =pod
24              
25             =encoding utf-8
26              
27             =head1 NAME
28              
29             YAML::PP::Schema::Merge - Enabling YAML merge keys for mappings
30              
31             =head1 SYNOPSIS
32              
33             use YAML::PP;
34             my $yp = YAML::PP->new( schema => [qw/ + Merge /] );
35              
36             my $yaml = <<'EOM';
37             ---
38             - &CENTER { x: 1, y: 2 }
39             - &LEFT { x: 0, y: 2 }
40             - &BIG { r: 10 }
41             - &SMALL { r: 1 }
42              
43             # All the following maps are equal:
44              
45             - # Explicit keys
46             x: 1
47             y: 2
48             r: 10
49             label: center/big
50              
51             - # Merge one map
52             << : *CENTER
53             r: 10
54             label: center/big
55              
56             - # Merge multiple maps
57             << : [ *CENTER, *BIG ]
58             label: center/big
59              
60             - # Override
61             << : [ *BIG, *LEFT, *SMALL ]
62             x: 1
63             label: center/big
64             EOM
65             my $data = $yp->load_string($yaml);
66             # $data->[4] == $data->[5] == $data->[6] == $data->[7]
67              
68             =head1 DESCRIPTION
69              
70             See L<https://yaml.org/type/merge.html> for the specification.
71              
72             Quote:
73              
74             "Specify one or more mappings to be merged with the current one.
75              
76             The C<< << >> merge key is used to indicate that all the keys of one or more
77             specified maps should be inserted into the current map. If the value associated
78             with the key is a single mapping node, each of its key/value pairs is inserted
79             into the current mapping, unless the key already exists in it. If the value
80             associated with the merge key is a sequence, then this sequence is expected to
81             contain mapping nodes and each of these nodes is merged in turn according to its
82             order in the sequence. Keys in mapping nodes earlier in the sequence override
83             keys specified in later mapping nodes."
84              
85             The implementation of this in a generic way is not trivial, because we also
86             have to handle duplicate keys, and YAML::PP allows you to write your own
87             handler for processing mappings.
88              
89             So the inner API of that is not stable at this point.
90              
91             Note that if you enable this schema, a plain scalar `<<` will be seen as
92             special anywhere in your document, so if you want a literal `<<`, you have
93             to put it in quotes.
94              
95             Note that the performed merge is not a "deep merge". Only top-level keys are
96             merged.
97              
98             =head1 METHODS
99              
100             =over
101              
102             =item register
103              
104             Called by YAML::PP::Schema
105              
106             =back
107              
108             =cut
109