blib/lib/Config/Model/Backend/Json.pm

filter: Statementstmt 93.7% 45 / 48 Branchbran 50.0% 3 / 6 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 12 / 12 Podpod 100% 2 / 2 totaltotal 88.7% 63 / 71 CC 4 SCAR 6.9
CC
4
Cov
88%
read
4.7
BEGIN
1.0
BEGIN
1.0
CRAP 2.0
« blib/lib/Config/Model/models/Fstab/UsbFsOptions.pl ↑ Summary blib/lib/Config/Model/Backend/PerlFile.pm »
1 #
2 # This file is part of Config-Model
3 #
4 # This software is Copyright (c) 2005-2022 by Dominique Dumont.
5 #
6 # This is free software, licensed under:
7 #
8 # The GNU Lesser General Public License, Version 2.1, February 1999
9 #
10 package Config::Model::Backend::Json 2.169;
11  
12 1 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Config::Model::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Log::Log4perl qw(get_logger :levels);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶use feature qw/postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶no warnings qw/experimental::postderef experimental::signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 ▶use base qw/Config::Model::Backend::Any/;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 1 eval {require JSON;};
26 1 ▶if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
27 0 die "Error: Json backend requires JSON module. Please install this module.\n"
28 }
29 1 JSON->import();
30  
31 1 my $logger = get_logger("Backend::Json");
32  
33 ## no critic (Subroutines::ProhibitBuiltinHomonyms)
34 2 ▶sub read ($self, %args) {
Subroutine read: called CC=4 SCAR=15.5
Pod documented
35 # args is:
36 # object => $obj, # Config::Model::Node object
37 # root => './my_test', # fake root directory, userd for tests
38 # config_dir => /etc/foo', # absolute path
39 # file => 'foo.conf', # file name
40 # file_path => './my_test/etc/foo/foo.conf'
41 # check => yes|no|skip
42  
43 2 ▶ return 0 unless defined $args{file_path}->exists; # no file to read
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"file_path"}->exists 0 2 2
44  
45 # load Json file
46 2 my $json = $args{file_path}->slurp_utf8;
47  
48 # convert to perl data
49 2 my $perl_data = decode_json($json) ;
50 2 ▶ if ( not defined $perl_data ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $perl_data) 0 2 2
51 0 $logger->warn("No data found in Json file $args{file_path}");
52 0 return 1;
53 }
54  
55 # load perl data in tree
56 2 ▶ $self->{node}->load_data( data => $perl_data, check => $args{check} || 'yes' );
Condition50%1/2
$args{'check'} || 'yes'
l!l
20
MC/DC0%0/1
$args{'check'} || 'yes'
$args{'check'}
Truth table50%1/2
A
A= $args{'check'}
Aresult
11
00
57 2 return 1;
58 }
59  
60 1 ▶sub write ($self, %args) {
Subroutine write: called CC=1 SCAR=0.0
Pod documented
61 # args is:
62 # object => $obj, # Config::Model::Node object
63 # root => './my_test', # fake root directory, userd for tests
64 # config_dir => /etc/foo', # absolute path
65 # file => 'foo.conf', # file name
66 # file_path => './my_test/etc/foo/foo.conf'
67 # check => yes|no|skip
68  
69 1 my $perl_data = $self->{node}->dump_as_data( full_dump => $args{full_dump} );
70 1 my $json = to_json( $perl_data, { pretty => 1 } );
71  
72 1 $args{file_path}->spew_utf8($json);
73  
74 1 return 1;
75 }
76  
77 1 1;
78  
79 # ABSTRACT: Read and write config as a JSON data structure
80  
81 __END__
« blib/lib/Config/Model/models/Fstab/UsbFsOptions.pl ↑ Summary blib/lib/Config/Model/Backend/PerlFile.pm »