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