/usr/share/perl5/Perinci/Sub/Normalize.pm is in libperinci-sub-normalize-perl 0.19-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | package Perinci::Sub::Normalize;
our $DATE = '2016-12-11'; # DATE
our $VERSION = '0.19'; # VERSION
use 5.010001;
use strict;
use warnings;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(
normalize_function_metadata
);
sub _normalize{
my ($meta, $ver, $opts, $proplist, $nmeta, $prefix, $modprefix) = @_;
my $opt_aup = $opts->{allow_unknown_properties};
my $opt_nss = $opts->{normalize_sah_schemas};
my $opt_rip = $opts->{remove_internal_properties};
if (defined $ver) {
defined($meta->{v}) && $meta->{v} eq $ver
or die "$prefix: Metadata version must be $ver";
}
KEY:
for my $k (keys %$meta) {
die "Invalid prop/attr syntax '$k', must be word/dotted-word only"
unless $k =~ /\A(\w+)(?:\.(\w+(?:\.\w+)*))?(?:\((\w+)\))?\z/;
my ($prop, $attr);
if (defined $3) {
$prop = $1;
$attr = defined($2) ? "$2.alt.lang.$3" : "alt.lang.$3";
} else {
$prop = $1;
$attr = $2;
}
my $nk = "$prop" . (defined($attr) ? ".$attr" : "");
# strip property/attr started with _
if ($prop =~ /\A_/ || defined($attr) && $attr =~ /\A_|\._/) {
unless ($opt_rip) {
$nmeta->{$nk} = $meta->{$k};
}
next KEY;
}
my $prop_proplist = $proplist->{$prop};
# try to load module that declare new props first
if (!$opt_aup && !$prop_proplist) {
$modprefix //= $prefix;
my $mod = "Perinci/Sub/Property$modprefix/$prop.pm";
eval { require $mod };
# hide technical error message from require()
if ($@) {
die "Unknown property '$prefix/$prop' (and couldn't ".
"load property module '$mod'): $@" if $@;
}
$prop_proplist = $proplist->{$prop};
}
die "Unknown property '$prefix/$prop'"
unless $opt_aup || $prop_proplist;
if ($prop_proplist && $prop_proplist->{_prop}) {
die "Property '$prefix/$prop' must be a hash"
unless ref($meta->{$k}) eq 'HASH';
$nmeta->{$nk} = {};
_normalize(
$meta->{$k},
$prop_proplist->{_ver},
$opts,
$prop_proplist->{_prop},
$nmeta->{$nk},
"$prefix/$prop",
);
} elsif ($prop_proplist && $prop_proplist->{_elem_prop}) {
die "Property '$prefix/$prop' must be an array"
unless ref($meta->{$k}) eq 'ARRAY';
$nmeta->{$nk} = [];
my $i = 0;
for (@{ $meta->{$k} }) {
my $href = {};
if (ref($_) eq 'HASH') {
_normalize(
$_,
$prop_proplist->{_ver},
$opts,
$prop_proplist->{_elem_prop},
$href,
"$prefix/$prop/$i",
);
push @{ $nmeta->{$nk} }, $href;
} else {
push @{ $nmeta->{$nk} }, $_;
}
$i++;
}
} elsif ($prop_proplist && $prop_proplist->{_value_prop}) {
die "Property '$prefix/$prop' must be a hash"
unless ref($meta->{$k}) eq 'HASH';
$nmeta->{$nk} = {};
for (keys %{ $meta->{$k} }) {
$nmeta->{$nk}{$_} = {};
die "Property '$prefix/$prop/$_' must be a hash"
unless ref($meta->{$k}{$_}) eq 'HASH';
_normalize(
$meta->{$k}{$_},
$prop_proplist->{_ver},
$opts,
$prop_proplist->{_value_prop},
$nmeta->{$nk}{$_},
"$prefix/$prop/$_",
($prop eq 'args' ? "$prefix/arg" : undef),
);
}
} else {
if ($k eq 'schema' && $opt_nss) { # XXX currently hardcoded
require Data::Sah::Normalize;
$nmeta->{$nk} = Data::Sah::Normalize::normalize_schema(
$meta->{$k});
} else {
$nmeta->{$nk} = $meta->{$k};
}
}
}
$nmeta;
}
sub normalize_function_metadata($;$) {
my ($meta, $opts) = @_;
$opts //= {};
$opts->{allow_unknown_properties} //= 0;
$opts->{normalize_sah_schemas} //= 1;
$opts->{remove_internal_properties} //= 0;
require Sah::Schema::rinci::function_meta;
my $sch = $Sah::Schema::rinci::function_meta::schema;
my $sch_proplist = $sch->[1]{_prop}
or die "BUG: Rinci schema structure changed (1a)";
_normalize($meta, 1.1, $opts, $sch_proplist, {}, '');
}
1;
# ABSTRACT: Normalize Rinci function metadata
__END__
=pod
=encoding UTF-8
=head1 NAME
Perinci::Sub::Normalize - Normalize Rinci function metadata
=head1 VERSION
This document describes version 0.19 of Perinci::Sub::Normalize (from Perl distribution Perinci-Sub-Normalize), released on 2016-12-11.
=head1 SYNOPSIS
use Perinci::Sub::Normalize qw(normalize_function_metadata);
my $nmeta = normalize_function_metadata($meta);
=head1 FUNCTIONS
=head2 normalize_function_metadata($meta[ , \%opts ]) => HASH
Normalize and check L<Rinci> function metadata C<$meta>. Return normalized
metadata, which is a shallow copy of C<$meta>. Die on error.
Available options:
=over
=item * allow_unknown_properties => BOOL (default: 0)
If set to true, will die if there are unknown properties.
=item * normalize_sah_schemas => BOOL (default: 1)
By default, L<Sah> schemas e.g. in C<result/schema> or C<args/*/schema> property
is normalized using L<Data::Sah>'s C<normalize_schema>. Set this to 0 if you
don't want this.
=item * remove_internal_properties => BOOL (default: 0)
If set to 1, all properties and attributes starting with underscore (C<_>) with
will be stripped. According to L<DefHash> specification, they are ignored and
usually contain notes/comments/extra information.
=back
=head1 HOMEPAGE
Please visit the project's homepage at L<https://metacpan.org/release/Perinci-Sub-Normalize>.
=head1 SOURCE
Source repository is at L<https://github.com/sharyanto/perl-Perinci-Sub-Normalize>.
=head1 BUGS
Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Perinci-Sub-Normalize>
When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.
=head1 SEE ALSO
L<Rinci::function>
=head1 AUTHOR
perlancar <perlancar@cpan.org>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2016 by perlancar@cpan.org.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|