| 1 | package MOP4Import::Util::JSON_TYPE; | ||||||||||||||||||||||||||||||||||||
| 2 | 22 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 3 | 22 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||
| 5 | 22 | ▶ | use constant DEBUG => $ENV{DEBUG_MOP4IMPORT}; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||
| 7 | 22 | ▶ | use MOP4Import::Util qw/globref define_constant terse_dump/; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||
| 9 | 22 | ▶ | use MOP4Import::Opts qw/Opts/; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||
| 11 | 22 | ▶ | use Cpanel::JSON::XS::Type; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||
| 13 | 22 | our %JSON_TYPES; # It is too early to hide this. | |||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||
| 15 | BEGIN { | ||||||||||||||||||||||||||||||||||||
| 16 | 22 | ▶ | my $CLS = 'Cpanel::JSON::XS::Type'; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||
| 18 | 22 | foreach my $origTypeName (qw(int float string bool null)) { | |||||||||||||||||||||||||||||||||||
| 19 | 110 | foreach my $suffix ('', '_or_null') { | |||||||||||||||||||||||||||||||||||
| 20 | # Ignore null_or_null | ||||||||||||||||||||||||||||||||||||
| 21 | 220 | ▶ | next if $origTypeName eq 'null' and $suffix ne ''; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$origTypeName eq "null" and $suffix ne ""
MC/DC100%2/2
$origTypeName eq "null" and $suffix ne ""
$origTypeName eq "null"
$suffix ne ""
Truth table100%3/3
A and B
A= $origTypeName eq "null"B= $suffix ne ""
| |||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||
| 23 | 198 | my $typeName = $origTypeName . $suffix; | |||||||||||||||||||||||||||||||||||
| 24 | 198 | my $lowerName = "JSON_TYPE_".$typeName; | |||||||||||||||||||||||||||||||||||
| 25 | 198 | my $upperName = "JSON_TYPE_".uc($typeName); | |||||||||||||||||||||||||||||||||||
| 26 | 198 | my $value = $CLS->$upperName; | |||||||||||||||||||||||||||||||||||
| 27 | 198 | define_constant(join("::", __PACKAGE__, $lowerName), $value); | |||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||
| 29 | # Make sure underlying typecode 1, 2, 3... can be resolved too. | ||||||||||||||||||||||||||||||||||||
| 30 | 198 | $JSON_TYPES{$value} = $value; | |||||||||||||||||||||||||||||||||||
| 31 | 198 | $JSON_TYPES{$typeName} = $value; | |||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||
| 35 | 22 | foreach my $keyword (qw(hashof arrayof anyof null_or_anyof)) { | |||||||||||||||||||||||||||||||||||
| 36 | 88 | my $longName = "json_type_$keyword"; | |||||||||||||||||||||||||||||||||||
| 37 | 88 | my $sub = $CLS->can($longName); | |||||||||||||||||||||||||||||||||||
| 38 | 88 | *{globref(__PACKAGE__, $keyword)} = sub { | |||||||||||||||||||||||||||||||||||
| 39 | 1 | ▶ | shift; $sub->(@_); | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||
| 41 | 88 | } | |||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||
| 44 | sub lookup_json_type { | ||||||||||||||||||||||||||||||||||||
| 45 | 621 | ▶ | my ($pack, $typeName) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
lookup_json_type: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 46 | 621 | $JSON_TYPES{$typeName}; | |||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||
| 49 | sub declare_json_type_record { | ||||||||||||||||||||||||||||||||||||
| 50 | 508 | ▶ | my ($pack, $typeName) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
declare_json_type_record: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 51 | 508 | ▶ | $JSON_TYPES{$typeName} //= +{}; | ||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$MOP4Import::Util::JSON_TYPE::JSON_TYPES{$typeName} //= {}
MC/DC100%1/1
$MOP4Import::Util::JSON_TYPE::JSON_TYPES{$typeName} //= {}
$MOP4Import::Util::JSON_TYPE::JSON_TYPES{$typeName}
Truth table100%2/2
A
A= $MOP4Import::Util::JSON_TYPE::JSON_TYPES{$typeName}
| |||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||
| 54 | sub inherit_json_type { | ||||||||||||||||||||||||||||||||||||
| 55 | 221 | ▶ | my ($pack, $typeName, $superType) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
inherit_json_type: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 56 | 221 | ▶ | my $superSpec = $pack->lookup_json_type($superType) | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 57 | or return; | ||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||
| 59 | 136 | my $thisSpec = $pack->declare_json_type_record($typeName); | |||||||||||||||||||||||||||||||||||
| 60 | 136 | print STDERR " json_type $typeName inherits following items from $superType: " | |||||||||||||||||||||||||||||||||||
| 61 | , join(", ", sort keys %$superSpec), "\n" if DEBUG; | ||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||
| 63 | 136 | foreach my $key (keys %$superSpec) { | |||||||||||||||||||||||||||||||||||
| 64 | 140 | $thisSpec->{$key} = $superSpec->{$key}; | |||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||
| 68 | sub resolve_json_type_name { | ||||||||||||||||||||||||||||||||||||
| 69 | 191 | ▶ | (my $pack, my Opts $opts, my $typeName) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
resolve_json_type_name: called CC=9 SCAR=23.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 70 | 191 | ▶ | Carp::confess "typename is undef!" unless defined $typeName; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 71 | 191 | ▶ | Carp::confess "typename should be a string" if ref $typeName; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||
| 73 | 191 | print STDERR " resolving typeSpec $typeName" | |||||||||||||||||||||||||||||||||||
| 74 | if DEBUG and DEBUG >= 2; | ||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||
| 76 | 191 | ▶ | if ($typeName eq '') { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 77 | 0 | Carp::confess "typename is empty string!" | |||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||
| 80 | 191 | ▶ | if ($typeName =~ /::/ and defined $JSON_TYPES{$typeName}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition66%2/3
$typeName =~ /::/ and defined $MOP4Import::Util::JSON_TYPE::JSON_TYPES{$typeName}
MC/DC50%1/2
$typeName =~ /::/ and defined $MOP4Import::Util::JSON_TYPE::JSON_TYPES{$typeName}
$typeName =~ /::/
defined $MOP4Import::Util::JSON_TYPE::JSON_TYPES{$typeName}
Truth table66%2/3
A and B
A= $typeName =~ /::/B= defined $MOP4Import::Util::JSON_TYPE::JSON_TYPES{$typeName}
| |||||||||||||||||||||||||||||||||||||
| 81 | 1 | $typeName | |||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||
| 83 | elsif ($typeName =~ /^[a-z]/) { | ||||||||||||||||||||||||||||||||||||
| 84 | Carp::croak "Unknown builtin json type: $typeName" | ||||||||||||||||||||||||||||||||||||
| 85 | 187 | ▶ | unless $JSON_TYPES{$typeName}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 86 | 187 | $typeName; | |||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||
| 88 | elsif (my $sub = $opts->{destpkg}->can($typeName)) { | ||||||||||||||||||||||||||||||||||||
| 89 | 3 | my $value = $sub->(); | |||||||||||||||||||||||||||||||||||
| 90 | 3 | print STDERR ", $typeName is resolved to $value in $opts->{destpkg}" | |||||||||||||||||||||||||||||||||||
| 91 | if DEBUG; | ||||||||||||||||||||||||||||||||||||
| 92 | 3 | $value | |||||||||||||||||||||||||||||||||||
| 93 | } else { | ||||||||||||||||||||||||||||||||||||
| 94 | 0 | print STDERR ", $typeName is used as-is in $opts->{destpkg}" | |||||||||||||||||||||||||||||||||||
| 95 | if DEBUG; | ||||||||||||||||||||||||||||||||||||
| 96 | 0 | $typeName | |||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||
| 100 | sub register_json_type_of_field { | ||||||||||||||||||||||||||||||||||||
| 101 | 318 | ▶ | (my $pack, my Opts $opts, my ($destpkg, $fieldName, $jsonType)) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
register_json_type_of_field: called CC=4 SCAR=14.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 102 | 318 | ▶ | my $typeRec = $JSON_TYPES{$destpkg} //= +{}; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$MOP4Import::Util::JSON_TYPE::JSON_TYPES{$destpkg} //= {}
MC/DC0%0/1
$MOP4Import::Util::JSON_TYPE::JSON_TYPES{$destpkg} //= {}
$MOP4Import::Util::JSON_TYPE::JSON_TYPES{$destpkg}
Truth table50%1/2
A
A= $MOP4Import::Util::JSON_TYPE::JSON_TYPES{$destpkg}
| |||||||||||||||||||||||||||||||||||||
| 103 | 318 | ▶ | unless (ref $typeRec eq 'HASH') { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | Carp::croak "Can't set json_type for $destpkg\->{$fieldName} because it was already declared as type: @{[$typeRec // '']}" | ||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$typeRec // ""
MC/DC0%0/1
$typeRec // ""
$typeRec
Truth table0%0/2
A
A= $typeRec
| |||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||
| 107 | 318 | push @{$opts->{delayed_tasks}}, sub { | |||||||||||||||||||||||||||||||||||
| 108 | 191 | ▶ | print STDERR " $fieldName json_type: spec=", terse_dump($jsonType) | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||
| 109 | if DEBUG; | ||||||||||||||||||||||||||||||||||||
| 110 | 191 | print STDERR " Current \%JSON_TYPES=", terse_dump(\%JSON_TYPES) | |||||||||||||||||||||||||||||||||||
| 111 | if DEBUG and DEBUG >= 4; | ||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||
| 113 | # Note about reference weakining: | ||||||||||||||||||||||||||||||||||||
| 114 | # All user defined types should be registered in %JSON_TYPES. | ||||||||||||||||||||||||||||||||||||
| 115 | # All references to them should be weakened. | ||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||
| 117 | 191 | ▶ | if (not ref $jsonType | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not ref $jsonType and my $found = $pack->resolve_json_type_name($opts, $jsonType)
MC/DC50%1/2
not ref $jsonType and my $found = $pack->resolve_json_type_name($opts, $jsonType)
not ref $jsonType
my $found = $pack->resolve_json_type_name($opts, $jsonType)
Truth table66%2/3
not(A) and B
A= not ref $jsonTypeB= my $found = $pack->resolve_json_type_name($opts, $jsonType)
| |||||||||||||||||||||||||||||||||||||
| 118 | and my $found = $pack->resolve_json_type_name($opts, $jsonType)) { | ||||||||||||||||||||||||||||||||||||
| 119 | 190 | $typeRec->{$fieldName} = $JSON_TYPES{$found}; | |||||||||||||||||||||||||||||||||||
| 120 | Scalar::Util::weaken($typeRec->{$fieldName}) | ||||||||||||||||||||||||||||||||||||
| 121 | 190 | ▶ | if ref $typeRec->{$fieldName}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 122 | } else { | ||||||||||||||||||||||||||||||||||||
| 123 | 1 | $typeRec->{$fieldName} = $pack->build_json_type($opts, $jsonType) | |||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||
| 126 | 191 | print STDERR "\n" if DEBUG; | |||||||||||||||||||||||||||||||||||
| 127 | 318 | }; | |||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||
| 131 | sub build_json_type { | ||||||||||||||||||||||||||||||||||||
| 132 | 2 | ▶ | (my $pack, my Opts $opts, my $typeSpec) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
build_json_type: called CC=8 SCAR=27.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 133 | 2 | ▶ | if (not defined $typeSpec) { | ||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||
| 134 | 0 | Carp::croak "json_type is undef!"; | |||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||
| 136 | elsif (not ref $typeSpec) { | ||||||||||||||||||||||||||||||||||||
| 137 | 1 | ▶ | if (my $found = $pack->resolve_json_type_name($opts, $typeSpec)) { | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 138 | # Note: weakening here does not take effect. | ||||||||||||||||||||||||||||||||||||
| 139 | 1 | return $JSON_TYPES{$found}; | |||||||||||||||||||||||||||||||||||
| 140 | } elsif (my $sub = $pack->can(my $longName = "JSON_TYPE_".$typeSpec)) { | ||||||||||||||||||||||||||||||||||||
| 141 | 0 | return $sub->(); | |||||||||||||||||||||||||||||||||||
| 142 | } else { | ||||||||||||||||||||||||||||||||||||
| 143 | 0 | Carp::croak "Unknown JSON_TYPE name: $typeSpec"; | |||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||
| 146 | elsif (ref $typeSpec eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||
| 147 | 1 | my ($keyword, @args) = @$typeSpec; | |||||||||||||||||||||||||||||||||||
| 148 | 1 | print STDERR "\n build_json_type(", terse_dump($keyword, @args), ") => " | |||||||||||||||||||||||||||||||||||
| 149 | if DEBUG; | ||||||||||||||||||||||||||||||||||||
| 150 | 1 | my @actual = map {$pack->build_json_type($opts, $_)} @args; | |||||||||||||||||||||||||||||||||||
| 151 | 1 | print STDERR "\n >> (", terse_dump($keyword, @actual), ")\n" | |||||||||||||||||||||||||||||||||||
| 152 | if DEBUG; | ||||||||||||||||||||||||||||||||||||
| 153 | 1 | $pack->$keyword(@actual); | |||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||
| 155 | elsif (ref $typeSpec eq 'HASH') { | ||||||||||||||||||||||||||||||||||||
| 156 | 0 | my %spec; | |||||||||||||||||||||||||||||||||||
| 157 | 0 | foreach my $key (keys %$typeSpec) { | |||||||||||||||||||||||||||||||||||
| 158 | 0 | $spec{$key} = $pack->build_json_type($opts, $typeSpec->{$key}); | |||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||
| 160 | 0 | \%spec; | |||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||
| 164 | 22 | 1; | |||||||||||||||||||||||||||||||||||