line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
35
|
|
|
35
|
|
288
|
use strict; |
|
35
|
|
|
|
|
89
|
|
|
35
|
|
|
|
|
1094
|
|
2
|
35
|
|
|
35
|
|
179
|
use warnings; |
|
35
|
|
|
|
|
89
|
|
|
35
|
|
|
|
|
2007
|
|
3
|
|
|
|
|
|
|
package YAML::PP::Representer; |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.036'; # VERSION |
6
|
|
|
|
|
|
|
|
7
|
35
|
|
|
35
|
|
232
|
use Scalar::Util qw/ reftype blessed refaddr /; |
|
35
|
|
|
|
|
76
|
|
|
35
|
|
|
|
|
2640
|
|
8
|
|
|
|
|
|
|
|
9
|
35
|
|
|
|
|
2680
|
use YAML::PP::Common qw/ |
10
|
|
|
|
|
|
|
YAML_PLAIN_SCALAR_STYLE YAML_SINGLE_QUOTED_SCALAR_STYLE |
11
|
|
|
|
|
|
|
YAML_DOUBLE_QUOTED_SCALAR_STYLE |
12
|
|
|
|
|
|
|
YAML_ANY_SCALAR_STYLE |
13
|
|
|
|
|
|
|
YAML_LITERAL_SCALAR_STYLE YAML_FOLDED_SCALAR_STYLE |
14
|
|
|
|
|
|
|
YAML_FLOW_SEQUENCE_STYLE YAML_FLOW_MAPPING_STYLE |
15
|
|
|
|
|
|
|
YAML_BLOCK_MAPPING_STYLE YAML_BLOCK_SEQUENCE_STYLE |
16
|
|
|
|
|
|
|
PRESERVE_ORDER PRESERVE_SCALAR_STYLE PRESERVE_FLOW_STYLE PRESERVE_ALIAS |
17
|
35
|
|
|
35
|
|
265
|
/; |
|
35
|
|
|
|
|
87
|
|
18
|
35
|
|
|
35
|
|
256
|
use B; |
|
35
|
|
|
|
|
83
|
|
|
35
|
|
|
|
|
56152
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub new { |
21
|
184
|
|
|
184
|
0
|
631
|
my ($class, %args) = @_; |
22
|
184
|
|
100
|
|
|
714
|
my $preserve = delete $args{preserve} || 0; |
23
|
184
|
100
|
|
|
|
493
|
if ($preserve == 1) { |
24
|
1
|
|
|
|
|
13
|
$preserve = PRESERVE_ORDER | PRESERVE_SCALAR_STYLE | PRESERVE_FLOW_STYLE | PRESERVE_ALIAS; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
my $self = bless { |
27
|
|
|
|
|
|
|
schema => delete $args{schema}, |
28
|
184
|
|
|
|
|
613
|
preserve => $preserve, |
29
|
|
|
|
|
|
|
}, $class; |
30
|
184
|
50
|
|
|
|
517
|
if (keys %args) { |
31
|
0
|
|
|
|
|
0
|
die "Unexpected arguments: " . join ', ', sort keys %args; |
32
|
|
|
|
|
|
|
} |
33
|
184
|
|
|
|
|
1273
|
return $self; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub clone { |
37
|
9
|
|
|
9
|
0
|
16
|
my ($self) = @_; |
38
|
|
|
|
|
|
|
my $clone = { |
39
|
|
|
|
|
|
|
schema => $self->schema, |
40
|
|
|
|
|
|
|
preserve => $self->{preserve}, |
41
|
9
|
|
|
|
|
28
|
}; |
42
|
9
|
|
|
|
|
35
|
return bless $clone, ref $self; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
2430
|
|
|
2430
|
0
|
7220
|
sub schema { return $_[0]->{schema} } |
46
|
205
|
|
|
205
|
0
|
494
|
sub preserve_order { return $_[0]->{preserve} & PRESERVE_ORDER } |
47
|
2289
|
|
|
2289
|
0
|
3566
|
sub preserve_scalar_style { return $_[0]->{preserve} & PRESERVE_SCALAR_STYLE } |
48
|
333
|
|
|
333
|
0
|
984
|
sub preserve_flow_style { return $_[0]->{preserve} & PRESERVE_FLOW_STYLE } |
49
|
2358
|
|
|
2358
|
0
|
4538
|
sub preserve_alias { return $_[0]->{preserve} & PRESERVE_ALIAS } |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub represent_node { |
52
|
2289
|
|
|
2289
|
0
|
4091
|
my ($self, $node) = @_; |
53
|
|
|
|
|
|
|
|
54
|
2289
|
|
|
|
|
4270
|
my $preserve_alias = $self->preserve_alias; |
55
|
2289
|
|
|
|
|
4302
|
my $preserve_style = $self->preserve_scalar_style; |
56
|
2289
|
100
|
100
|
|
|
8449
|
if ($preserve_style or $preserve_alias) { |
57
|
131
|
100
|
|
|
|
295
|
if (ref $node->{value} eq 'YAML::PP::Preserve::Scalar') { |
58
|
87
|
|
|
|
|
254
|
my $value = $node->{value}->value; |
59
|
87
|
100
|
|
|
|
171
|
if ($preserve_style) { |
60
|
21
|
|
|
|
|
49
|
$node->{style} = $node->{value}->style; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
# $node->{tag} = $node->{value}->tag; |
63
|
87
|
|
|
|
|
175
|
$node->{value} = $value; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
} |
66
|
2289
|
|
|
|
|
5674
|
$node->{reftype} = reftype($node->{value}); |
67
|
2289
|
100
|
100
|
|
|
10247
|
if (not $node->{reftype} and reftype(\$node->{value}) eq 'GLOB') { |
68
|
6
|
|
|
|
|
23
|
$node->{reftype} = 'GLOB'; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
2289
|
100
|
|
|
|
5242
|
if ($node->{reftype}) { |
72
|
415
|
|
|
|
|
965
|
$self->_represent_noderef($node); |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
else { |
75
|
1874
|
|
|
|
|
4006
|
$self->_represent_node_nonref($node); |
76
|
|
|
|
|
|
|
} |
77
|
2289
|
|
100
|
|
|
8515
|
$node->{reftype} = (reftype $node->{data}) || ''; |
78
|
|
|
|
|
|
|
|
79
|
2289
|
100
|
100
|
|
|
5694
|
if ($node->{reftype} eq 'HASH' and my $tied = tied(%{ $node->{data} })) { |
|
205
|
|
|
|
|
747
|
|
80
|
34
|
|
|
|
|
66
|
my $representers = $self->schema->representers; |
81
|
34
|
|
|
|
|
84
|
$tied = ref $tied; |
82
|
34
|
50
|
|
|
|
94
|
if (my $def = $representers->{tied_equals}->{ $tied }) { |
83
|
0
|
|
|
|
|
0
|
my $code = $def->{code}; |
84
|
0
|
|
|
|
|
0
|
my $done = $code->($self, $node); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
2289
|
100
|
|
|
|
6772
|
if ($node->{reftype} eq 'HASH') { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
89
|
205
|
50
|
|
|
|
532
|
unless (defined $node->{items}) { |
90
|
|
|
|
|
|
|
# by default we sort hash keys |
91
|
205
|
|
|
|
|
318
|
my @keys; |
92
|
205
|
100
|
|
|
|
475
|
if ($self->preserve_order) { |
93
|
24
|
|
|
|
|
33
|
@keys = keys %{ $node->{data} }; |
|
24
|
|
|
|
|
91
|
|
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
else { |
96
|
181
|
|
|
|
|
289
|
@keys = sort keys %{ $node->{data} }; |
|
181
|
|
|
|
|
800
|
|
97
|
|
|
|
|
|
|
} |
98
|
205
|
|
|
|
|
513
|
for my $key (@keys) { |
99
|
417
|
|
|
|
|
572
|
push @{ $node->{items} }, $key, $node->{data}->{ $key }; |
|
417
|
|
|
|
|
1273
|
|
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
} |
102
|
205
|
|
|
|
|
336
|
my %args; |
103
|
205
|
100
|
66
|
|
|
448
|
if ($self->preserve_flow_style and reftype $node->{value} eq 'HASH') { |
104
|
19
|
100
|
|
|
|
28
|
if (my $tied = tied %{ $node->{value} } ) { |
|
19
|
|
|
|
|
52
|
|
105
|
18
|
|
|
|
|
38
|
$args{style} = $tied->{style}; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
} |
108
|
205
|
|
|
|
|
874
|
return [ mapping => $node, %args ]; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
elsif ($node->{reftype} eq 'ARRAY') { |
111
|
128
|
50
|
|
|
|
331
|
unless (defined $node->{items}) { |
112
|
128
|
|
|
|
|
202
|
@{ $node->{items} } = @{ $node->{data} }; |
|
128
|
|
|
|
|
365
|
|
|
128
|
|
|
|
|
266
|
|
113
|
|
|
|
|
|
|
} |
114
|
128
|
|
|
|
|
236
|
my %args; |
115
|
128
|
100
|
66
|
|
|
264
|
if ($self->preserve_flow_style and reftype $node->{value} eq 'ARRAY') { |
116
|
10
|
50
|
|
|
|
14
|
if (my $tied = tied @{ $node->{value} } ) { |
|
10
|
|
|
|
|
49
|
|
117
|
10
|
|
|
|
|
24
|
$args{style} = $tied->{style}; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
} |
120
|
128
|
|
|
|
|
531
|
return [ sequence => $node, %args ]; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
elsif ($node->{reftype}) { |
123
|
1
|
|
|
|
|
14
|
die "Cannot handle reftype '$node->{reftype}' (you might want to enable YAML::PP::Schema::Perl)"; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
else { |
126
|
1955
|
100
|
|
|
|
3967
|
unless (defined $node->{items}) { |
127
|
1892
|
|
|
|
|
4398
|
$node->{items} = [$node->{data}]; |
128
|
|
|
|
|
|
|
} |
129
|
1955
|
|
|
|
|
5990
|
return [ scalar => $node ]; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
my $bool_code = <<'EOM'; |
135
|
|
|
|
|
|
|
sub { |
136
|
|
|
|
|
|
|
my ($x) = @_; |
137
|
|
|
|
|
|
|
use experimental qw/ builtin /; |
138
|
|
|
|
|
|
|
builtin::is_bool($x); |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
EOM |
141
|
|
|
|
|
|
|
my $is_bool; |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub _represent_node_nonref { |
144
|
1874
|
|
|
1874
|
|
3347
|
my ($self, $node) = @_; |
145
|
1874
|
|
|
|
|
3832
|
my $representers = $self->schema->representers; |
146
|
|
|
|
|
|
|
|
147
|
1874
|
100
|
|
|
|
4468
|
if (not defined $node->{value}) { |
148
|
60
|
50
|
|
|
|
173
|
if (my $undef = $representers->{undef}) { |
149
|
60
|
50
|
|
|
|
175
|
return 1 if $undef->($self, $node); |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
else { |
152
|
0
|
|
|
|
|
0
|
$node->{style} = YAML_SINGLE_QUOTED_SCALAR_STYLE; |
153
|
0
|
|
|
|
|
0
|
$node->{data} = ''; |
154
|
0
|
|
|
|
|
0
|
return 1; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
} |
157
|
1814
|
50
|
33
|
|
|
4428
|
if ($] >= 5.036000 and my $rep = $representers->{bool}) { |
158
|
0
|
|
0
|
|
|
0
|
$is_bool ||= eval $bool_code; |
159
|
0
|
0
|
|
|
|
0
|
if ($is_bool->($node->{value})) { |
160
|
0
|
|
|
|
|
0
|
return $rep->{code}->($self, $node); |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
} |
163
|
1814
|
|
|
|
|
2634
|
for my $rep (@{ $representers->{flags} }) { |
|
1814
|
|
|
|
|
4392
|
|
164
|
2874
|
|
|
|
|
4527
|
my $check_flags = $rep->{flags}; |
165
|
2874
|
|
|
|
|
9685
|
my $flags = B::svref_2object(\$node->{value})->FLAGS; |
166
|
2874
|
100
|
|
|
|
7605
|
if ($flags & $check_flags) { |
167
|
429
|
100
|
|
|
|
1372
|
return 1 if $rep->{code}->($self, $node); |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
} |
171
|
1394
|
100
|
|
|
|
4706
|
if (my $rep = $representers->{equals}->{ $node->{value} }) { |
172
|
109
|
50
|
|
|
|
481
|
return 1 if $rep->{code}->($self, $node); |
173
|
|
|
|
|
|
|
} |
174
|
1285
|
|
|
|
|
1751
|
for my $rep (@{ $representers->{regex} }) { |
|
1285
|
|
|
|
|
2673
|
|
175
|
1081
|
100
|
|
|
|
9979
|
if ($node->{value} =~ $rep->{regex}) { |
176
|
98
|
100
|
|
|
|
377
|
return 1 if $rep->{code}->($self, $node); |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
} |
179
|
1193
|
50
|
|
|
|
3144
|
unless (defined $node->{data}) { |
180
|
1193
|
|
|
|
|
2481
|
$node->{data} = $node->{value}; |
181
|
|
|
|
|
|
|
} |
182
|
1193
|
100
|
|
|
|
2595
|
unless (defined $node->{style}) { |
183
|
1177
|
|
|
|
|
2001
|
$node->{style} = YAML_ANY_SCALAR_STYLE; |
184
|
1177
|
|
|
|
|
2405
|
$node->{style} = ""; |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
sub _represent_noderef { |
189
|
415
|
|
|
415
|
|
802
|
my ($self, $node) = @_; |
190
|
415
|
|
|
|
|
843
|
my $representers = $self->schema->representers; |
191
|
|
|
|
|
|
|
|
192
|
415
|
100
|
|
|
|
1387
|
if (my $classname = blessed($node->{value})) { |
193
|
111
|
100
|
|
|
|
375
|
if (my $def = $representers->{class_equals}->{ $classname }) { |
194
|
65
|
|
|
|
|
121
|
my $code = $def->{code}; |
195
|
65
|
50
|
|
|
|
185
|
return 1 if $code->($self, $node); |
196
|
|
|
|
|
|
|
} |
197
|
46
|
|
|
|
|
92
|
for my $matches (@{ $representers->{class_matches} }) { |
|
46
|
|
|
|
|
120
|
|
198
|
43
|
|
|
|
|
100
|
my ($re, $code) = @$matches; |
199
|
43
|
50
|
33
|
|
|
189
|
if (ref $re and $classname =~ $re or $re) { |
|
|
|
33
|
|
|
|
|
200
|
43
|
100
|
|
|
|
168
|
return 1 if $code->($self, $node); |
201
|
|
|
|
|
|
|
} |
202
|
|
|
|
|
|
|
} |
203
|
4
|
|
|
|
|
7
|
for my $isa (@{ $representers->{class_isa} }) { |
|
4
|
|
|
|
|
10
|
|
204
|
3
|
|
|
|
|
7
|
my ($class_name, $code) = @$isa; |
205
|
3
|
100
|
|
|
|
18
|
if ($node->{ value }->isa($class_name)) { |
206
|
2
|
50
|
|
|
|
6
|
return 1 if $code->($self, $node); |
207
|
|
|
|
|
|
|
} |
208
|
|
|
|
|
|
|
} |
209
|
|
|
|
|
|
|
} |
210
|
306
|
100
|
100
|
|
|
870
|
if ($node->{reftype} eq 'SCALAR' and my $scalarref = $representers->{scalarref}) { |
211
|
4
|
|
|
|
|
29
|
my $code = $scalarref->{code}; |
212
|
4
|
50
|
|
|
|
28
|
return 1 if $code->($self, $node); |
213
|
|
|
|
|
|
|
} |
214
|
302
|
100
|
66
|
|
|
817
|
if ($node->{reftype} eq 'REF' and my $refref = $representers->{refref}) { |
215
|
4
|
|
|
|
|
17
|
my $code = $refref->{code}; |
216
|
4
|
50
|
|
|
|
27
|
return 1 if $code->($self, $node); |
217
|
|
|
|
|
|
|
} |
218
|
298
|
100
|
66
|
|
|
722
|
if ($node->{reftype} eq 'CODE' and my $coderef = $representers->{coderef}) { |
219
|
5
|
|
|
|
|
11
|
my $code = $coderef->{code}; |
220
|
5
|
50
|
|
|
|
24
|
return 1 if $code->($self, $node); |
221
|
|
|
|
|
|
|
} |
222
|
293
|
100
|
66
|
|
|
705
|
if ($node->{reftype} eq 'GLOB' and my $glob = $representers->{glob}) { |
223
|
6
|
|
|
|
|
9
|
my $code = $glob->{code}; |
224
|
6
|
50
|
|
|
|
19
|
return 1 if $code->($self, $node); |
225
|
|
|
|
|
|
|
} |
226
|
287
|
|
|
|
|
664
|
$node->{data} = $node->{value}; |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
} |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
1; |