Formula/Config/JSON.pm

filter: Statementstmt 97.2% 71 / 73 Branchbran 85.2% 29 / 34 Conditioncond 75.0% 9 / 12 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 100% 13 / 13 Podpod 100% 2 / 2 totaltotal 90.7% 128 / 141 CC 25 SCAR 19.2
CC
25
Cov
92%
_serialize
13.5
_unpack
6.0
save
4.0
CRAP 6.8
« Formula/Type.pm ↑ Summary Formula/Token.pm »
1 # This code is part of Perl distribution Math-Formula version 0.18.
2 # The POD got stripped from this file by OODoc version 3.03.
3 # For contributors see file ChangeLog.
4  
5 # This software is copyright (c) 2023-2025 by Mark Overmeer.
6  
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10  
11 #oodist: *** DO NOT USE THIS VERSION FOR PRODUCTION ***
12 #oodist: This file contains OODoc-style documentation which will get stripped
13 #oodist: during its release in the distribution. You can use this file for
14 #oodist: testing, however the code of this development version may be broken!
15  
16 package Math::Formula::Config::JSON;{
17 1 our $VERSION = '0.18';
18 }
19  
20 1 ▶use parent 'Math::Formula::Config';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 1 ▶use Log::Report qw/math-formula/;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use File::Slurper qw/read_binary/;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use Cpanel::JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 1 my $json = Cpanel::JSON::XS->new->pretty->utf8->canonical(1);
31  
32 #--------------------
33  
34 #--------------------
35  
36 sub save($%)
37 2 ▶{ my ($self, $context, %args) = @_;
Subroutine save: called CC=4 SCAR=14.0
Pod documented
38 2 my $name = $context->name;
39  
40 2 my $index = $context->_index;
41 2 my $tree = $self->_set($index->{attributes});
42 2 $tree->{formulas} = $self->_set($index->{formulas});
43  
44 2 ▶ my $fn = $self->path_for($args{filename} || "$name.json");
Condition100%2/2
$args{'filename'} || "$name.json"
l!l
11
MC/DC100%1/1
$args{'filename'} || "$name.json"
$args{'filename'}
Truth table100%2/2
A
A= $args{'filename'}
Aresult
11
00
45 2 ▶ open my $fh, '>:raw', $fn
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:raw", $fn or &fault(&__x("Trying to save context '{name}' to {file}", "name", $name, "file", $fn)) 2 0 2
46 or fault __x"Trying to save context '{name}' to {file}", name => $name, file => $fn;
47  
48 2 $fh->print($json->encode($tree));
49 2 ▶ $fh->close
Branch: 1 branch
BranchTrueFalseTotal
$fh->close or &fault(&__x("Error on close while saving '{name}' to {file}", "name", $name, "file", $fn)) 2 0 2
50 or fault __x"Error on close while saving '{name}' to {file}", name => $name, file => $fn;
51 }
52  
53 sub _set($)
54 4 ▶{ my ($self, $set) = @_;
Subroutine _set: called CC=2 SCAR=6.9
55 4 my %data;
56 4 $data{$_ =~ s/^ctx_//r} = $self->_serialize($_, $set->{$_}) for keys %$set;
57 4 \%data;
58 }
59  
60 sub _serialize($$)
61 28 ▶{ my ($self, $name, $what) = @_;
Subroutine _serialize: called CC=13 SCAR=26.0
62 28 my %attrs;
63  
64 28 ▶ if(blessed $what && $what->isa('Math::Formula'))
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($what) and $what->isa("Math::Formula")) 18 10 28
Condition66%2/3
builtin::blessed($what) and $what->isa("Math::Formula")
!ll&&!rl&&r
01018
MC/DC50%1/2
builtin::blessed($what) and $what->isa("Math::Formula")
builtin::blessed($what) $what->isa("Math::Formula")
Truth table66%2/3
A and B
A= builtin::blessed($what)B= $what->isa("Math::Formula")
ABresult
0X0
100
111
65 18 ▶ { if(my $r = $what->returns) { $attrs{returns} = $r };
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $what->returns) 2 16 18
66 18 $what = $what->expression;
67 }
68  
69 28 my $v = '';
70 28 ▶ if(blessed $what && $what->isa('Math::Formula::Type'))
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::blessed($what) and $what->isa('Math::Formula::Type')) { } 24 4 28
elsif (ref $what eq 'CODE') { } 0 4 4
elsif (length $what) { } 4 0 4
Condition66%2/3
builtin::blessed($what) and $what->isa("Math::Formula::Type")
!ll&&!rl&&r
4024
MC/DC50%1/2
builtin::blessed($what) and $what->isa("Math::Formula::Type")
builtin::blessed($what) $what->isa("Math::Formula::Type")
Truth table66%2/3
A and B
A= builtin::blessed($what)B= $what->isa("Math::Formula::Type")
ABresult
0X0
100
111
71 { # strings without quote
72 24 ▶ $v = $what->isa('MF::STRING') ? $what->value
Branch: 4 branches
BranchTrueFalseTotal
$what->isa('MF::STRING') ? : 12 12 24
$what->isa('MF::BOOLEAN') ? : 4 8 12
$what->value ? : 2 2 4
$what->isa('MF::FLOAT') ? : 4 4 8
73 : $what->isa('MF::BOOLEAN') ? ($what->value ? Cpanel::JSON::XS::true : Cpanel::JSON::XS::false)
74 : $what->isa('MF::FLOAT') ? $what->value # otherwise JSON writes a string
75 : $what->token;
76 }
77 elsif(ref $what eq 'CODE')
78 0 { warning __x"cannot (yet) save CODE, skipped '{name}'", name => $name;
79 0 return undef;
80 }
81 elsif(length $what)
82 4 { $v = '=' . $what;
83 }
84  
85 28 ▶ if(keys %attrs)
Branch: 1 branch
BranchTrueFalseTotal
if (keys %attrs) 2 26 28
86 2 { $v .= '; ' . (join ', ', map "$_='$attrs{$_}'", sort keys %attrs);
87 }
88  
89 28 return $v;
90 }
91  
92  
93  
94 sub load($%)
95 1 ▶{ my ($self, $name, %args) = @_;
Subroutine load: called CC=2 SCAR=7.0
Pod documented
96 1 ▶ my $fn = $self->path_for($args{filename} || "$name.json");
Condition50%1/2
$args{'filename'} || "$name.json"
l!l
01
MC/DC0%0/1
$args{'filename'} || "$name.json"
$args{'filename'}
Truth table50%1/2
A
A= $args{'filename'}
Aresult
11
00
97  
98 1 my $tree = $json->decode(read_binary $fn);
99 1 my $formulas = delete $tree->{formulas};
100  
101 1 my $attrs = $self->_set_decode($tree);
102 1 Math::Formula::Context->new(name => $name, %$attrs,
103 formulas => $self->_set_decode($formulas),
104 );
105 }
106  
107 sub _set_decode($)
108 2 ▶{ my ($self, $set) = @_;
Subroutine _set_decode: called CC=3 SCAR=11.1
109 2 ▶ $set or return {};
Branch: 1 branch
BranchTrueFalseTotal
$set or return {} 2 0 2
110  
111 2 my %forms;
112 2 $forms{$_} = $self->_unpack($_, $set->{$_}) for keys %$set;
113 2 \%forms;
114 }
115  
116 sub _unpack($$)
117 14 ▶{ my ($self, $name, $encoded) = @_;
Subroutine _unpack: called CC=6 SCAR=17.9
118 14 my $dummy = Math::Formula->new('dummy', '7');
119  
120 14 ▶ if(ref $encoded eq 'JSON::PP::Boolean')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $encoded eq "JSON::PP::Boolean") 2 12 14
121 2 { return MF::BOOLEAN->new(undef, $encoded);
122 }
123  
124 12 ▶ if($encoded =~ m/^\=(.*?)(?:;\s*(.*))?$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($encoded =~ /^\=(.*?)(?:;\s*(.*))?$/) 2 10 12
125 2 ▶ { my ($expr, $attrs) = ($1, $2 // '');
Condition100%2/2
$2 // ""
l!l
11
MC/DC100%1/1
$2 // ""
$2
Truth table100%2/2
A
A= $2
Aresult
11
00
126 2 my %attrs = $attrs =~ m/(\w+)\='([^']+)'/g;
127 2 return Math::Formula->new($name, $expr =~ s/\\"/"/gr, %attrs);
128 }
129  
130 # No JSON implementation parses floats and ints cleanly into SV
131 # So, we need to check it by hand. Gladly, ints are converted
132 # to strings again when that was the intention.
133  
134 10 ▶ $encoded =~ qr/^[0-9]+$/ ? MF::INTEGER->new(undef, $encoded + 0)
Branch: 2 branches
BranchTrueFalseTotal
$encoded =~ qr/^[0-9]+$/ ? : 1 9 10
$encoded =~ qr/^[0-9][0-9.e+\-]+$/ ? : 3 6 9
135 : $encoded =~ qr/^[0-9][0-9.e+\-]+$/ ? MF::FLOAT->new(undef, $encoded + 0.0)
136 : MF::STRING->new(undef, $encoded);
137 }
138  
139 #--------------------
140  
141 1 1;
« Formula/Type.pm ↑ Summary Formula/Token.pm »