line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Config::Structured::Deserializer; |
2
|
|
|
|
|
|
|
$Config::Structured::Deserializer::VERSION = '2.002'; |
3
|
|
|
|
|
|
|
# ABSTRACT: Deserializes perl structures, JSON or YML data, from strings or files |
4
|
|
|
|
|
|
|
|
5
|
10
|
|
|
10
|
|
73
|
use strict; |
|
10
|
|
|
|
|
20
|
|
|
10
|
|
|
|
|
277
|
|
6
|
10
|
|
|
10
|
|
54
|
use warnings; |
|
10
|
|
|
|
|
18
|
|
|
10
|
|
|
|
|
261
|
|
7
|
|
|
|
|
|
|
|
8
|
10
|
|
|
10
|
|
53
|
use File::Basename; |
|
10
|
|
|
|
|
18
|
|
|
10
|
|
|
|
|
723
|
|
9
|
10
|
|
|
10
|
|
83
|
use IO::All; |
|
10
|
|
|
|
|
19
|
|
|
10
|
|
|
|
|
127
|
|
10
|
10
|
|
|
10
|
|
747
|
use Readonly; |
|
10
|
|
|
|
|
18
|
|
|
10
|
|
|
|
|
519
|
|
11
|
|
|
|
|
|
|
|
12
|
10
|
|
|
10
|
|
6068
|
use JSON qw(decode_json); |
|
10
|
|
|
|
|
93907
|
|
|
10
|
|
|
|
|
50
|
|
13
|
10
|
|
|
10
|
|
5208
|
use YAML::XS; |
|
10
|
|
|
|
|
22317
|
|
|
10
|
|
|
|
|
542
|
|
14
|
|
|
|
|
|
|
|
15
|
10
|
|
|
10
|
|
4871
|
use Syntax::Keyword::Try; |
|
10
|
|
|
|
|
7296
|
|
|
10
|
|
|
|
|
56
|
|
16
|
|
|
|
|
|
|
|
17
|
10
|
|
|
10
|
|
5059
|
use experimental qw(signatures); |
|
10
|
|
|
|
|
31476
|
|
|
10
|
|
|
|
|
194
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
Readonly::Hash my %FILE_TYPES => ( |
20
|
|
|
|
|
|
|
yml => 'yaml', |
21
|
|
|
|
|
|
|
yaml => 'yaml', |
22
|
|
|
|
|
|
|
json => 'json', |
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
Readonly::Scalar my $DEFAULT_DECODER => q{perl}; |
25
|
|
|
|
|
|
|
|
26
|
16
|
|
|
16
|
0
|
25
|
sub decoders() { |
|
16
|
|
|
|
|
27
|
|
27
|
|
|
|
|
|
|
return ( |
28
|
|
|
|
|
|
|
yaml => sub { |
29
|
17
|
|
|
17
|
|
1101
|
Load(shift()); |
30
|
|
|
|
|
|
|
}, |
31
|
|
|
|
|
|
|
json => sub { |
32
|
8
|
|
|
8
|
|
208
|
decode_json(shift()); |
33
|
|
|
|
|
|
|
}, |
34
|
|
|
|
|
|
|
perl => sub { |
35
|
17
|
|
|
17
|
|
1155
|
eval(shift()); |
36
|
|
|
|
|
|
|
}, |
37
|
16
|
|
|
|
|
147
|
); |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
16
|
|
|
16
|
0
|
28
|
sub is_filename($str) { |
|
16
|
|
|
|
|
25
|
|
|
16
|
|
|
|
|
22
|
|
41
|
16
|
100
|
|
|
|
84
|
return 0 if ($str =~ /\n/); |
42
|
6
|
|
|
|
|
224
|
return (-f $str); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
70
|
|
|
70
|
0
|
120
|
sub decode ($class, $v) { |
|
70
|
|
|
|
|
109
|
|
|
70
|
|
|
|
|
116
|
|
|
70
|
|
|
|
|
89
|
|
46
|
70
|
100
|
|
|
|
1411
|
return $v if (ref($v) eq 'HASH'); |
47
|
|
|
|
|
|
|
|
48
|
16
|
|
|
|
|
45
|
my %decoders = decoders(); |
49
|
16
|
|
|
|
|
41
|
my $hint = $DEFAULT_DECODER; |
50
|
16
|
100
|
|
|
|
40
|
if (is_filename($v)) { |
51
|
6
|
|
|
|
|
52
|
my ($fn, $dirs, $suffix) = fileparse($v, keys(%FILE_TYPES)); |
52
|
6
|
50
|
|
|
|
563
|
$hint = $FILE_TYPES{$suffix} if (defined($suffix)); |
53
|
6
|
|
|
|
|
69
|
$v = io->file($v)->slurp; |
54
|
|
|
|
|
|
|
} |
55
|
16
|
|
|
|
|
12618
|
do { |
56
|
26
|
|
66
|
|
|
188
|
my $n = $hint // (keys(%decoders))[0]; |
57
|
26
|
|
|
|
|
57
|
my $decoder = delete($decoders{$n}); |
58
|
|
|
|
|
|
|
try { |
59
|
|
|
|
|
|
|
my $structure = $decoder->($v); |
60
|
|
|
|
|
|
|
return $decoder->($v) if (ref($structure) eq 'HASH'); |
61
|
26
|
|
|
|
|
61
|
} catch { |
62
|
|
|
|
|
|
|
# ignore any errors and try the next decoder, or die out at the bottom |
63
|
|
|
|
|
|
|
}; |
64
|
|
|
|
|
|
|
} while (($hint) = keys(%decoders)); |
65
|
0
|
|
|
|
|
|
die("Config::Structured was unable to decode input"); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
1; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
__END__ |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=pod |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=encoding UTF-8 |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head1 NAME |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
Config::Structured::Deserializer - Deserializes perl structures, JSON or YML data, from strings or files |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head1 VERSION |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
version 2.002 |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head1 AUTHOR |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
Mark Tyrrell <mtyrrell@concertpharma.com> |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
This software is copyright (c) 2019 by Concert Pharmaceuticals, Inc. |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
93
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=cut |