line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Config::Structured::Deserializer; |
2
|
|
|
|
|
|
|
$Config::Structured::Deserializer::VERSION = '2.003'; |
3
|
|
|
|
|
|
|
# ABSTRACT: Deserializes perl structures, JSON or YML data, from strings or files |
4
|
|
|
|
|
|
|
|
5
|
11
|
|
|
11
|
|
408
|
use strict; |
|
11
|
|
|
|
|
28
|
|
|
11
|
|
|
|
|
339
|
|
6
|
11
|
|
|
11
|
|
66
|
use warnings; |
|
11
|
|
|
|
|
25
|
|
|
11
|
|
|
|
|
357
|
|
7
|
|
|
|
|
|
|
|
8
|
11
|
|
|
11
|
|
68
|
use File::Basename; |
|
11
|
|
|
|
|
36
|
|
|
11
|
|
|
|
|
939
|
|
9
|
11
|
|
|
11
|
|
84
|
use IO::All; |
|
11
|
|
|
|
|
25
|
|
|
11
|
|
|
|
|
176
|
|
10
|
11
|
|
|
11
|
|
1057
|
use Readonly; |
|
11
|
|
|
|
|
34
|
|
|
11
|
|
|
|
|
584
|
|
11
|
|
|
|
|
|
|
|
12
|
11
|
|
|
11
|
|
7414
|
use JSON qw(decode_json); |
|
11
|
|
|
|
|
116557
|
|
|
11
|
|
|
|
|
74
|
|
13
|
11
|
|
|
11
|
|
6219
|
use YAML::XS; |
|
11
|
|
|
|
|
27468
|
|
|
11
|
|
|
|
|
686
|
|
14
|
|
|
|
|
|
|
|
15
|
11
|
|
|
11
|
|
5672
|
use Syntax::Keyword::Try; |
|
11
|
|
|
|
|
22284
|
|
|
11
|
|
|
|
|
72
|
|
16
|
|
|
|
|
|
|
|
17
|
11
|
|
|
11
|
|
6067
|
use experimental qw(signatures); |
|
11
|
|
|
|
|
38667
|
|
|
11
|
|
|
|
|
265
|
|
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
|
18
|
|
|
18
|
0
|
37
|
sub decoders() { |
|
18
|
|
|
|
|
24
|
|
27
|
|
|
|
|
|
|
return ( |
28
|
|
|
|
|
|
|
yaml => sub { |
29
|
21
|
|
|
21
|
|
1386
|
Load(shift()); |
30
|
|
|
|
|
|
|
}, |
31
|
|
|
|
|
|
|
json => sub { |
32
|
8
|
|
|
8
|
|
208
|
decode_json(shift()); |
33
|
|
|
|
|
|
|
}, |
34
|
|
|
|
|
|
|
perl => sub { |
35
|
17
|
|
|
17
|
|
1115
|
eval(shift()); |
36
|
|
|
|
|
|
|
}, |
37
|
18
|
|
|
|
|
200
|
); |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
18
|
|
|
18
|
0
|
30
|
sub is_filename($str) { |
|
18
|
|
|
|
|
29
|
|
|
18
|
|
|
|
|
28
|
|
41
|
18
|
100
|
|
|
|
83
|
return 0 if ($str =~ /\n/); |
42
|
8
|
|
|
|
|
202
|
return (-f $str); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
74
|
|
|
74
|
0
|
157
|
sub decode ($class, $v) { |
|
74
|
|
|
|
|
129
|
|
|
74
|
|
|
|
|
132
|
|
|
74
|
|
|
|
|
114
|
|
46
|
74
|
100
|
|
|
|
1645
|
return $v if (ref($v) eq 'HASH'); |
47
|
|
|
|
|
|
|
|
48
|
18
|
|
|
|
|
48
|
my %decoders = decoders(); |
49
|
18
|
|
|
|
|
52
|
my $hint = $DEFAULT_DECODER; |
50
|
18
|
100
|
|
|
|
42
|
if (is_filename($v)) { |
51
|
8
|
|
|
|
|
62
|
my ($fn, $dirs, $suffix) = fileparse($v, keys(%FILE_TYPES)); |
52
|
8
|
50
|
|
|
|
688
|
$hint = $FILE_TYPES{$suffix} if (defined($suffix)); |
53
|
8
|
|
|
|
|
85
|
$v = io->file($v)->slurp; |
54
|
|
|
|
|
|
|
} |
55
|
18
|
|
|
|
|
22217
|
do { |
56
|
28
|
|
66
|
|
|
230
|
my $n = $hint // (keys(%decoders))[0]; |
57
|
28
|
|
|
|
|
62
|
my $decoder = delete($decoders{$n}); |
58
|
|
|
|
|
|
|
try { |
59
|
|
|
|
|
|
|
my $structure = $decoder->($v); |
60
|
|
|
|
|
|
|
return $decoder->($v) if (ref($structure) eq 'HASH'); |
61
|
28
|
|
|
|
|
77
|
} 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.003 |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head1 AUTHOR |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
Mark Tyrrell <mtyrrell@concertpharma.com> |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
This software is copyright (c) 2023 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 |