blib/lib/File/SOPS/Metadata/Flat.pm

filter: Statementstmt 100% 97 / 97 Branchbran 81.5% 62 / 76 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 12 / 12 Podpod 100% 5 / 5 totaltotal 93.0% 186 / 200 CC 47 SCAR 22.1
CC
47
Cov
92%
unflatten
17.3
_flatten_node
9.0
_assert_complete_lists
7.0
CRAP 9.2
« blib/lib/File/SOPS/Format/INI.pm ↑ Summary blib/lib/File/SOPS/Encrypted.pm »
1 package File::SOPS::Metadata::Flat;
2 # ABSTRACT: the flat sops_age__list_0__map_enc metadata encoding of the ENV and INI formats
3 85 our $VERSION = '0.005';
4 85 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 85 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 85 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 85 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # The two path separators, measured against sops 3.13.3 rather than read out
10 # of the Go source. Every descent into the metadata tree appends one of them:
11 # a map key gets `__map_` . $key, a list index gets `__list_` . $index. The
12 # root field name carries no separator at all. They compose to any depth --
13 # `key_groups__list_0__map_age__list_0__map_enc` is a real document sops
14 # writes for a key-group file, and it is five levels.
15 85 our $MAP_SEPARATOR = '__map_';
16 85 our $LIST_SEPARATOR = '__list_';
17  
18 85 has prefix => (is => 'ro', default => '');
19  
20  
21  
22 428 ▶sub is_metadata_key {
Subroutine is_metadata_key: called CC=3 SCAR=11.0
Pod documented
23 428 my ($self, $key) = @_;
24 428 my $prefix = $self->prefix;
25 428 ▶ return 1 unless length $prefix;
Branch: 1 branch
BranchTrueFalseTotal
unless length $prefix 101 327 428
26 327 ▶ return index($key, $prefix) == 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
index($key, $prefix) == 0 ? : 207 120 327
27 }
28  
29  
30 244 ▶sub escape_value {
Subroutine escape_value: called CC=6 SCAR=17.9
Pod documented
31 244 my ($self, $value) = @_;
32  
33 244 ▶ return '' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 244 244
34  
35 # A metadata section carries JSON::PP::Boolean for mac_only_encrypted, and
36 # sops writes it as a bare lowercase `true`. Perl's stringification of that
37 # object is 1/0, so it has to be asked rather than interpolated.
38 244 ▶ if (blessed $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value)) 3 241 244
39 3 ▶ croak "cannot write a " . ref($value) . " object into the flat "
Branch: 1 branch
BranchTrueFalseTotal
unless $value->isa("JSON::PP::Boolean") 1 2 3
40 . "metadata encoding; only plain scalars and JSON::PP::Boolean "
41 . "have a form sops writes there"
42 unless $value->isa('JSON::PP::Boolean');
43 2 ▶ return $value ? 'true' : 'false';
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 1 1 2
44 }
45  
46 241 ▶ croak "cannot write a " . ref($value) . " reference as a flat metadata "
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 1 240 241
47 . "leaf; flatten() descends into HASH and ARRAY and every other "
48 . "reference is a value it has no encoding for"
49 if ref $value;
50  
51 240 my $escaped = $value;
52 240 $escaped =~ s/\n/\\n/g;
53 240 return $escaped;
54 }
55  
56  
57 338 ▶sub unescape_value {
Subroutine unescape_value: called CC=2 SCAR=7.0
Pod documented
58 338 my ($self, $value) = @_;
59  
60 338 ▶ return '' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 338 338
61  
62 338 my $unescaped = $value;
63 338 $unescaped =~ s/\\n/\n/g;
64 338 return $unescaped;
65 }
66  
67  
68 34 ▶sub flatten {
Subroutine flatten: called CC=4 SCAR=14.6
Pod documented
69 34 my ($self, $hash) = @_;
70  
71 34 ▶ croak "flatten needs the HashRef that File::SOPS::Metadata->to_hash "
Branch: 3 branches
BranchTrueFalseTotal
unless ref $hash eq "HASH" 1 33 34
ref $hash ? : 1 0 1
defined $hash ? : 0 0 0
72 . "returns, got " . (ref($hash) ? ref($hash) . " reference"
73 : defined $hash ? "a scalar" : "undef")
74 unless ref $hash eq 'HASH';
75  
76 33 my @pairs;
77 33 $self->_flatten_node($hash, undef, \@pairs);
78 31 return @pairs;
79 }
80  
81 # $path is undef exactly at the root, which is how a top-level field name comes
82 # out bare (`age`) while everything below it carries a separator
83 # (`age__list_0__map_enc`). A length check would do the same job for every real
84 # metadata section and the wrong one for a field named with the empty string.
85 407 ▶sub _flatten_node {
Subroutine _flatten_node: called CC=9 SCAR=22.0
86 407 my ($self, $node, $path, $pairs) = @_;
87  
88 407 ▶ my $ref = blessed($node) ? '' : ref $node;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($node) ? : 2 405 407
89  
90 407 ▶ if ($ref eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "HASH") 75 332 407
91 75 for my $key (sort keys %$node) {
92 $self->_flatten_node(
93 333 ▶ $node->{$key},
Branch: 1 branch
BranchTrueFalseTotal
defined $path ? : 69 264 333
94 defined $path ? $path . $MAP_SEPARATOR . $key : $key,
95 $pairs,
96 );
97 }
98 73 return;
99 }
100  
101 332 ▶ if ($ref eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "ARRAY") 160 172 332
102 # An empty list emits nothing, because the loop does not run. That is
103 # the behaviour sops has and the one the format needs -- see
104 # "Empty lists vanish, and must" above.
105 160 for my $i (0 .. $#$node) {
106 41 ▶ $self->_flatten_node(
Branch: 1 branch
BranchTrueFalseTotal
defined $path ? : 41 0 41
107 $node->[$i],
108 (defined $path ? $path : '') . $LIST_SEPARATOR . $i,
109 $pairs,
110 );
111 }
112 160 return;
113 }
114  
115 172 ▶ croak "flatten reached a leaf with no key path at all; the top level of a "
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 172 172
116 . "metadata section is a mapping and its leaves are named"
117 unless defined $path;
118  
119 172 push @$pairs, [ $self->prefix . $path, $self->escape_value($node) ];
120 170 return;
121 }
122  
123  
124 36 ▶sub unflatten {
Subroutine unflatten: called CC=17 SCAR=28.5
Pod documented
125 36 my ($self, $flat) = @_;
126  
127 36 ▶ croak "unflatten needs a HashRef of flat key => value, got "
Branch: 3 branches
BranchTrueFalseTotal
unless ref $flat eq "HASH" 1 35 36
ref $flat ? : 0 1 1
defined $flat ? : 1 0 1
128 . (ref($flat) ? ref($flat) . " reference"
129 : defined $flat ? "a scalar" : "undef")
130 unless ref $flat eq 'HASH';
131  
132 35 my $root = {};
133  
134 35 for my $key (sort keys %$flat) {
135 166 ▶ next unless $self->is_metadata_key($key);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_metadata_key($key) 1 165 166
136  
137 165 my $slot = \$root;
138 165 for my $step ($self->_split_path($key)) {
139 278 my ($kind, $name) = @$step;
140 278 my $container = $$slot;
141  
142 278 ▶ if ($kind eq 'map') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq 'map') { } 222 56 278
143 222 ▶ croak "flat metadata key '$key' needs a mapping at '$name', "
Branch: 2 branches
BranchTrueFalseTotal
if defined $container and ref $container ne "HASH" 1 221 222
ref $container ? : 0 1 1
Condition100%3/3
defined $container and ref $container ne "HASH"
!ll&&!rl&&r
301911
MC/DC100%2/2
defined $container and ref $container ne "HASH"
defined $container ref $container ne "HASH"
Truth table100%3/3
A and B
A= defined $containerB= ref $container ne "HASH"
ABresult
0X0
100
111
144 . "where another key has already put "
145 . (ref $container ? 'a ' . lc(ref $container) : 'a value')
146 if defined $container && ref $container ne 'HASH';
147 221 ▶ $$slot = $container = {} unless defined $container;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $container 30 191 221
148 221 $slot = \$container->{$name};
149 }
150 else {
151 56 ▶ croak "flat metadata key '$key' needs a list at index $name, "
Branch: 2 branches
BranchTrueFalseTotal
if defined $container and ref $container ne "ARRAY" 1 55 56
ref $container ? : 0 1 1
Condition100%3/3
defined $container and ref $container ne "ARRAY"
!ll&&!rl&&r
27281
MC/DC100%2/2
defined $container and ref $container ne "ARRAY"
defined $container ref $container ne "ARRAY"
Truth table100%3/3
A and B
A= defined $containerB= ref $container ne "ARRAY"
ABresult
0X0
100
111
152 . "where another key has already put "
153 . (ref $container ? 'a ' . lc(ref $container) : 'a value')
154 if defined $container && ref $container ne 'ARRAY';
155 55 ▶ $$slot = $container = [] unless defined $container;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $container 27 28 55
156 55 $slot = \$container->[$name];
157 }
158 }
159  
160 162 ▶ croak "flat metadata key '$key' is set twice" if defined $$slot;
Branch: 1 branch
BranchTrueFalseTotal
if defined $$slot 0 162 162
161 162 $$slot = $self->unescape_value($flat->{$key});
162 }
163  
164 32 _assert_complete_lists($root, '');
165  
166 31 return $root;
167 }
168  
169 165 ▶sub _split_path {
Subroutine _split_path: called CC=6 SCAR=18.0
170 165 my ($self, $key) = @_;
171  
172 165 my $bare = substr($key, length $self->prefix);
173  
174 165 ▶ croak "flat metadata key '$key' has nothing after the prefix '"
Branch: 1 branch
BranchTrueFalseTotal
unless length $bare 0 165 165
175 . $self->prefix . "'"
176 unless length $bare;
177  
178 # A capturing split hands back the separators interleaved with the names,
179 # so the result is always name, sep, name, sep, name...
180 165 my @parts = split /(\Q$MAP_SEPARATOR\E|\Q$LIST_SEPARATOR\E)/, $bare, -1;
181  
182 165 my @path = ([ 'map', shift @parts ]);
183  
184 165 ▶ while (@parts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts) 116 164 280
185 116 my $separator = shift @parts;
186 116 my $name = shift @parts;
187 116 ▶ $name = '' unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 116 116
188  
189 116 ▶ if ($separator eq $LIST_SEPARATOR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($separator eq $File::SOPS::Metadata::Flat::LIST_SEPARATOR) { } 57 59 116
190 57 ▶ croak "flat metadata key '$key' has '$LIST_SEPARATOR$name' where a "
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /\A[0-9]+\z/ 1 56 57
191 . "list index has to be a non-negative integer"
192 unless $name =~ /\A[0-9]+\z/;
193 56 push @path, [ 'list', 0 + $name ];
194 }
195 else {
196 59 push @path, [ 'map', $name ];
197 }
198 }
199  
200 164 return @path;
201 }
202  
203 # sops refuses a list whose indices do not run from 0 without gaps -- renumber
204 # age__list_1__ to age__list_2__ in a file it wrote and it stops with
205 # "Error while unflattening: Incomplete list", exit 1. Autovivifying index 2
206 # leaves index 1 undef, so a hole is exactly what an undef element is here:
207 # nothing else in this class ever stores one.
208 246 ▶sub _assert_complete_lists {
Subroutine _assert_complete_lists: called CC=7 SCAR=19.5
209 246 my ($node, $path) = @_;
210  
211 246 ▶ if (ref $node eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node eq "HASH") 60 186 246
212 _assert_complete_lists($node->{$_}, length $path ? "$path.$_" : $_)
213 60 ▶ for sort keys %$node;
Branch: 1 branch
BranchTrueFalseTotal
length $path ? : 54 132 186
214 59 return;
215 }
216  
217 186 ▶ if (ref $node eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node eq "ARRAY") 27 159 186
218 27 for my $i (0 .. $#$node) {
219 29 ▶ croak "incomplete list at '$path': index $i is missing, and the "
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node->[$i] 1 28 29
220 . "flat metadata encoding has no way to write a list with a "
221 . "hole in it. sops refuses the same document with "
222 . "'Error while unflattening: Incomplete list'"
223 unless defined $node->[$i];
224 28 _assert_complete_lists($node->[$i], "$path\[$i]");
225 }
226 26 return;
227 }
228  
229 159 return;
230 }
231  
232  
233 85 1;
234  
235 __END__
« blib/lib/File/SOPS/Format/INI.pm ↑ Summary blib/lib/File/SOPS/Encrypted.pm »