blib/lib/Google/Protobuf/Loader.pm

filter: Statementstmt 90.4% 19 / 21 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.8% 26 / 28 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package Google::Protobuf::Loader;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use File::Spec::Functions 'catfile';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 0 ▶use Google::ProtocolBuffers::Dynamic;
Subroutine BEGIN: called CC=1 SCAR=2.6
11  
12 our $VERSION = '0.01';
13  
14 # use Proto::Foo::Bar will search for lib/Foo/Bar.proto
15 # package "foo.bar_baz" in proto will give Proto::Foo::BarBaz in Perl
16 # import "Foo/Bar.proto" will search for lib/Foo/Bar.proto
17 #
18 # There is currently no options but here are possible options for the future:
19 #
20 # - Regex to match use statements that are intercepted by this module.
21 # - Whether the module is pushed or unshifted on @INC.
22 # - The name of the package prefix being used (if any).
23 # - Capitalization rules for the proto to Perl package translation
24 # - Support for non UTF-8 encoding in the input file.
25 # - Custom search path ignoring @INC (or in addition to @INC).
26  
27 my %package_options;
28  
29 my $IS_USED_HINT_KEY = 'Google::Protobuf::Loader/is_used';
30  
31 sub import { ## no critic (RequireArgUnpacking)
32 my (undef) = shift @_; # This is the package being imported, so our self.
33  
34 my $calling_pkg_name = caller(0);
35 $^H{$IS_USED_HINT_KEY} = 1;
36  
37 while (defined (my $arg = shift)) {
38 if ($arg eq 'map_options') {
39 $package_options{$calling_pkg_name} = shift;
40 } else {
41 croak "Unknown parameter: $arg";
42 }
43 }
44 push @INC, \&use_proto_file_hook;
45 return;
46 }
47  
48 # Search for the given proto file if the @INC directories and load it if found.
49 sub use_proto_file_hook {
50 # The first argument is ourselves, this is the calling convention for
51 # references added to @INC.
52 my (undef, $module_name) = @_;
53 return unless $^H{$IS_USED_HINT_KEY};
54 return unless $module_name =~ s{^Proto/(.+)\.pm$}{$1.proto};
55 my $calling_pkg_name = caller(0);
56 return search_and_include($module_name, $calling_pkg_name);
57 }
58  
59 my %SEARCH_PROTO;
60 my %INC_PROTO;
61  
62 sub search_and_include {
63 my ($file_name, $calling_pkg_name) = @_;
64 return \'1;' if $INC_PROTO{$file_name};
65 croak "Infinite loop while loading ${file_name}" if exists $SEARCH_PROTO{$file_name};
66 $SEARCH_PROTO{$file_name} = 1;
67 for my $inc (@INC) {
68 next if (!defined $inc || ref $inc);
69 my $test_file_path = catfile($inc, $file_name);
70 next unless -f $test_file_path;
71 load_proto_file($test_file_path, $file_name, $calling_pkg_name);
72 $INC_PROTO{$file_name} = 1;
73 delete $SEARCH_PROTO{$file_name};
74 return \'1;';
75 }
76 return;
77 }
78  
79 # Load one proto file, following the convention of the @INC processing. See the
80 # reference in: https://perldoc.perl.org/functions/require
81 #
82 # Succeeds (and returns nothing) or dies.
83 my $dyn_pb = Google::ProtocolBuffers::Dynamic->new();
84  
85 sub load_proto_file {
86 my ($full_file_name, $rel_file_name, $calling_pkg_name) = @_;
87 my $content = read_file($full_file_name);
88 # Unfortunately, the Google::ProtocolBuffers::Dynamic module does not support
89 # using the root package.
90 if ($content !~ m/^\s* package \s* ([a-zA-Z0-9._]+) \s* ;/mx) {
91 croak "No package definition in '${rel_file_name}'";
92 }
93 my $package = $1;
94 while ($content =~ m{^\s* import \s* " ([a-zA-Z0-9._/]+) " \s* ;}mgx) {
95 search_and_include($1, $calling_pkg_name);
96 }
97 $dyn_pb->load_string($rel_file_name, $content);
98 my $prefix = $package =~ s/(^|\.|_)(.)/($1 eq '.' ? '::' : '').uc($2)/egr;
99 my %options;
100 if (exists $package_options{$calling_pkg_name}) {
101 %options = (options => $package_options{$calling_pkg_name});
102 }
103 $dyn_pb->map({package => $package, prefix => "Proto::${prefix}", %options});
104 return;
105 }
106  
107 sub read_file {
108 my ($file) = @_;
109 open my $fh, '<:encoding(UTF-8)', $file
110 or croak "Cannot open file '${file}' for reading: ${ERRNO}\n";
111 my $data;
112 {
113 local $RS = undef;
114 $data = <$fh>;
115 }
116 close $fh or croak "Cannot close file '${file}' after read: ${ERRNO}\n";
117 return $data;
118 }
119  
120 1;
121  
122 __END__
↑ Summary