lib/Myriad/Class.pm

filter: Statementstmt 96.2% 155 / 161 Branchbran 75.0% 9 / 12 Conditioncond 71.4% 10 / 14 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 97.1% 34 / 35 Podpod n/a totaltotal 90.9% 210 / 231 CC 16 SCAR 17.1
CC
16
Cov
93%
import
15.5
__ANON__
4.5
BEGIN
1.0
CRAP 5.5
« lib/Myriad/RPC.pm ↑ Summary lib/Myriad/Role.pm »
1 package Myriad::Class;
2  
3 70 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 70 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 70 our $VERSION = '1.001'; # VERSION
7 70 our $AUTHORITY = 'cpan:DERIV'; # AUTHORITY
8  
9 70 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =encoding utf8
12  
13 =head1 NAME
14  
15 Myriad::Class - common pragmata for L<Myriad> core modules
16  
17 =head1 SYNOPSIS
18  
19 package Example::Class;
20 use Myriad::Class;
21  
22 async method startup {
23 $log->infof('Starting %s', __PACKAGE__);
24 }
25  
26 1;
27  
28 =head1 DESCRIPTION
29  
30 Since L<Myriad> is a framework, by default it attempts to enforce a common
31 standard on all microservice modules. This same standard is used in most of
32 the modules which comprise L<Myriad> itself.
33  
34 The following Perl language features and modules are applied:
35  
36 =over 4
37  
38 =item * L<strict>
39  
40 =item * L<warnings>
41  
42 =item * L<utf8>
43  
44 =item * L<perlsub/signatures>
45  
46 =item * no L<indirect>
47  
48 =item * no L<multidimensional>
49  
50 =item * no L<bareword::filehandles>
51  
52 =item * L<Syntax::Keyword::Try>
53  
54 =item * L<Syntax::Keyword::Dynamically>
55  
56 =item * L<Syntax::Keyword::Defer>
57  
58 =item * L<Future::AsyncAwait>
59  
60 =item * provides L<Scalar::Util/blessed>, L<Scalar::Util/weaken>, L<Scalar::Util/refaddr>
61  
62 =item * provides L<List::Util/min>, L<List::Util/max>, L<List::Util/sum0>
63  
64 =item * provides L<List::Keywords/any>, L<List::Keywords/all>
65  
66 =item * provides L<JSON::MaybeUTF8/encode_json_text>, L<JSON::MaybeUTF8/encode_json_utf8>,
67 L<JSON::MaybeUTF8/decode_json_text>, L<JSON::MaybeUTF8/decode_json_utf8>, L<JSON::MaybeUTF8/format_json_text>
68  
69 =item * provides L<Unicode::UTF8/encode_utf8>, L<Unicode::UTF8/decode_utf8>
70  
71 =back
72  
73 In addition, the following core L<feature>s are enabled:
74  
75 =over 4
76  
77 =item * L<bitwise|feature>
78  
79 =item * L<current_sub|feature>
80  
81 =item * L<evalbytes|feature>
82  
83 =item * L<fc|feature>
84  
85 =item * L<postderef_qq|feature>
86  
87 =item * L<state|feature>
88  
89 =item * L<unicode_eval|feature>
90  
91 =item * L<unicode_strings|feature>
92  
93 =back
94  
95 The calling package will be marked as an L<Object::Pad> class, providing the
96 L<Object::Pad/method>, L<Object::Pad/has> and C<async method> keywords.
97  
98 This also makes available a L<Log::Any> instance in the C<$log> package variable,
99 and for L<OpenTracing::Any> support you get C<$tracer> as an L<OpenTracing::Tracer>
100 instance.
101  
102 It's very likely that future versions will bring in new functionality or
103 enable/disable a different featureset. This behaviour will be controlled through
104 version tags:
105  
106 use Myriad::Class qw(:v1);
107  
108 with the default being C<:v1>.
109  
110 =cut
111  
112 70 ▶use Object::Pad;
Subroutine BEGIN: called CC=1 SCAR=0.0
113 70 ▶use Object::Pad qw(:experimental(mop));
Subroutine BEGIN: called CC=1 SCAR=0.0
114 70 ▶no indirect qw(fatal);
Subroutine BEGIN: called CC=1 SCAR=0.0
115 70 ▶no multidimensional;
Subroutine BEGIN: called CC=1 SCAR=0.0
116 70 ▶no bareword::filehandles;
Subroutine BEGIN: called CC=1 SCAR=0.0
117 70 ▶use mro;
Subroutine BEGIN: called CC=1 SCAR=0.0
118 70 ▶use experimental qw(signatures);
Subroutine BEGIN: called CC=1 SCAR=0.0
119 70 ▶use curry;
Subroutine BEGIN: called CC=1 SCAR=0.0
120 70 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
121 70 ▶use Syntax::Keyword::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
122 70 ▶use Syntax::Keyword::Dynamically;
Subroutine BEGIN: called CC=1 SCAR=0.0
123 70 ▶use Syntax::Keyword::Defer;
Subroutine BEGIN: called CC=1 SCAR=0.0
124 70 ▶use Syntax::Keyword::Match;
Subroutine BEGIN: called CC=1 SCAR=0.0
125 70 ▶use Syntax::Operator::Equ;
Subroutine BEGIN: called CC=1 SCAR=0.0
126 70 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
127 70 ▶use List::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
128 70 ▶use List::Keywords;
Subroutine BEGIN: called CC=1 SCAR=0.0
129 70 ▶use Future::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
130 70 ▶use Module::Load ();
Subroutine BEGIN: called CC=1 SCAR=0.0
131  
132 70 ▶use JSON::MaybeUTF8;
Subroutine BEGIN: called CC=1 SCAR=0.0
133 70 ▶use Unicode::UTF8;
Subroutine BEGIN: called CC=1 SCAR=0.0
134  
135 70 ▶use Heap;
Subroutine BEGIN: called CC=1 SCAR=0.0
136 70 ▶use IO::Async::Notifier;
Subroutine BEGIN: called CC=1 SCAR=0.0
137  
138 70 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
139 70 ▶use OpenTracing::Any qw($tracer);
Subroutine BEGIN: called CC=1 SCAR=0.0
140 70 ▶use Metrics::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
141  
142 243 ▶sub import {
Subroutine import: called CC=15 SCAR=27.4
143 243 my $called_on = shift;
144  
145 # Unused, but we'll support it for now.
146 243 my $version = 1;
147 243 ▶ if(@_ and $_[0] =~ /^:v([0-9]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and $_[0] =~ /^:v([0-9]+)/) 0 243 243
Condition66%2/3
@_ and $_[0] =~ /^:v([0-9]+)/
!ll&&!rl&&r
811620
MC/DC0%0/2
@_ and $_[0] =~ /^:v([0-9]+)/
@_ $_[0] =~ /^:v([0-9]+)/
Truth table66%2/3
A and B
A= @_B= $_[0] =~ /^:v([0-9]+)/
ABresult
0X0
100
111
148 0 $version = $1;
149 0 shift;
150 }
151 243 my %args = @_;
152  
153 243 my $class = __PACKAGE__;
154 243 ▶ my $pkg = $args{target} // caller(0);
Condition66%2/3
$args{"target"} // caller 0
l!l&&r!l&&!r
292140
MC/DC0%0/2
$args{"target"} // caller 0
$args{"target"} caller 0
Truth table66%2/3
A // B
A= $args{"target"}B= caller 0
ABresult
1X1
011
000
155  
156 # Apply core syntax and rules
157 243 strict->import;
158 243 warnings->import;
159 243 utf8->import;
160  
161 # We want mostly the 5.26 featureset, but since that includes `say` and `switch`
162 # we need to customise the list somewhat
163 243 feature->import(qw(
164 bitwise
165 current_sub
166 evalbytes
167 fc
168 postderef_qq
169 state
170 unicode_eval
171 unicode_strings
172 ));
173  
174 # Indirect syntax is problematic due to `unknown_sub { ... }` compiling and running
175 # the block without complaint, and only failing at runtime *after* the code has
176 # executed once - particularly unfortunate with try/catch
177 243 indirect->unimport(qw(fatal));
178 # Multidimensional array access - $x{3,4} - is usually a sign that someone wanted
179 # `@x{3,4}` or similar instead, so we disable this entirely
180 243 multidimensional->unimport;
181 # Plain STDIN/STDOUT/STDERR are still allowed, although hopefully never used by
182 # service code - new filehandles need to be lexical.
183 243 bareword::filehandles->unimport;
184  
185 # This one's needed for nested scope, e.g. { package XX; use Myriad::Service; method xxx (%args) ... }
186 243 experimental->import('signatures');
187  
188 # We don't really care about diamond inheritance, since microservices are expected
189 # to have minimal inheritance in the first place, but might as well have a standard
190 # decision to avoid surprises in future
191 243 mro::set_mro($pkg => 'c3');
192  
193 # Helper functions which are used often enough to be valuable as a default
194 243 Scalar::Util->export($pkg => qw(refaddr blessed weaken));
195 243 List::Util->export($pkg => qw(min max sum0));
196 {
197 70 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
198 243 *{$pkg . '::' . $_} = JSON::MaybeUTF8->can($_) for qw(
199 encode_json_text
200 encode_json_utf8
201 decode_json_text
202 decode_json_utf8
203 format_json_text
204 );
205 243 *{$pkg . '::' . $_} = Unicode::UTF8->can($_) for qw(
206 encode_utf8
207 decode_utf8
208 );
209 }
210 {
211 70 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
212 243 *{$pkg . '::' . $_} = Future::Utils->can($_) for qw(
213 fmap_void
214 fmap_concat
215 fmap_scalar
216 fmap0
217 fmap1
218 );
219 }
220  
221 {
222 70 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
223 # trim() might appear in core perl at some point, so let's reserve the
224 # word and include a basic implementation first. Avoiding Text::Trim
225 # here because it sometimes returns an empty list, which would be
226 # dangerous - my %hash = (key => trim($value)) for example.
227 243 ▶ *{$pkg . '::trim'} = sub ($txt) {
Subroutine __ANON__: not called CC=2 SCAR=15.1
228 0 ▶ return undef unless defined $txt;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $txt 0 0 0
229 0 $txt =~ s{^\s+}{};
230 0 $txt =~ s{\s+$}{};
231 0 return $txt;
232 243 };
233 }
234  
235 # Some well-designed modules provide direct support for import target
236 243 Syntax::Keyword::Try->import_into($pkg, try => ':experimental(typed)');
237 243 Syntax::Keyword::Dynamically->import_into($pkg);
238 243 Syntax::Keyword::Defer->import_into($pkg);
239 243 Syntax::Operator::Equ->import_into($pkg);
240 243 Future::AsyncAwait->import_into($pkg, ':experimental(cancel)');
241 243 Metrics::Any->import_into($pkg, '$metrics');
242  
243 # Others use lexical hints
244 243 List::Keywords->import(qw(any all));
245 243 Syntax::Keyword::Match->import(qw(match));
246  
247 {
248 70 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
249 # Essentially the same as importing Log::Any qw($log) for now,
250 # but we may want to customise this with some additional attributes.
251 # Note that we have to store a ref to the returned value, don't
252 # drop that backslash...
253 243 *{$pkg . '::log'} = \Log::Any->get_logger(
254 category => $pkg
255 );
256 243 *{$pkg . '::tracer'} = \(OpenTracing->global_tracer);
257 }
258  
259 243 ▶ if(my $class = $args{class} // $pkg) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $class = $args{"class"} // $pkg) 186 57 243
Condition66%2/3
$args{"class"} // $pkg
l!l&&r!l&&!r
571860
MC/DC0%0/2
$args{"class"} // $pkg
$args{"class"} $pkg
Truth table66%2/3
A // B
A= $args{"class"}B= $pkg
ABresult
1X1
011
000
260 # For history here, see this:
261 # https://rt.cpan.org/Ticket/Display.html?id=132337
262 # We do this first to get the keywords...
263 186 Object::Pad->import_into($pkg);
264 # ... and then _again_ to disable the experimental warnings
265 186 Object::Pad->import_into($pkg, qw(:experimental));
266 186 ▶ my $method = 'begin_' . ($args{type} || 'class');
Condition100%2/2
$args{'type'} || 'class'
l!l
68118
MC/DC100%1/1
$args{'type'} || 'class'
$args{'type'}
Truth table100%2/2
A
A= $args{'type'}
Aresult
11
00
267 my $meta = Object::Pad::MOP::Class->$method(
268 $class,
269 (
270 $args{extends}
271 ? (extends => $args{extends})
272 186 ▶ : ()
Branch: 1 branch
BranchTrueFalseTotal
$args{'extends'} ? : 27 159 186
273 ),
274 );
275 186 ▶ $args{does} = [ $args{does} // () ] unless ref $args{does};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args{"does"} 173 13 186
Condition66%2/3
$args{"does"} // ()
l!l&&r!l&&!r
10172
MC/DC50%1/2
$args{"does"} // ()
$args{"does"} ()
Truth table66%2/3
A // B
A= $args{"does"}B= ()
ABresult
1X1
011
000
276 186 for my $role ($args{does}->@*) {
277 17 ▶ Module::Load::load($role) unless eval { Object::Pad::MOP::Class->for_class($role) };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { "Object::Pad::MOP::Class"->for_class($role) } } 16 1 17
278 17 $meta->add_role($role);
279 }
280 186 return $meta;
281 }
282 57 return $pkg;
283 }
284  
285 70 1;
286  
287 __END__
« lib/Myriad/RPC.pm ↑ Summary lib/Myriad/Role.pm »