lib/Yote/YapiServer/Compiler.pm

filter: Statementstmt 73.0% 293 / 401 Branchbran 60.3% 70 / 116 Conditioncond 28.5% 8 / 28 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 95.2% 20 / 21 Podpod 0.0% 0 / 13 totaltotal 65.8% 391 / 594 CC 105 SCAR 39.5
CC
105
Cov
68%
compile_object
240.0
compile_app
50.8
merge_method_shorthands
45.0
CRAP 52.2
« lib/Yote/YapiServer/User.pm ↑ Summary blib/lib/Yote/YapiServer/YapiDef.pm »
1 package Yote::YapiServer::Compiler;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Yote::YapiServer::YapiDef;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 #======================================================================
13 # Public API
14 #======================================================================
15  
16 sub compile {
17 4 ▶ my ($input, $outdir) = @_;
Subroutine compile: called CC=12 SCAR=25.5
Pod undocumented
18  
19 4 my @files;
20 4 ▶ if (-d $input) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $input) { } 1 3 4
elsif (-f $input) { } 3 0 3
21 1 ▶ find(sub { push @files, $File::Find::name if /\.(ya?ml|ydef)$/ }, $input);
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if /\.(ya?ml|ydef)$/ 4 1 5
22 } elsif (-f $input) {
23 3 @files = ($input);
24 } else {
25 0 die "Input not found: $input\n";
26 }
27  
28 4 ▶ die "No definition files found\n" unless @files;
Branch: 1 branch
BranchTrueFalseTotal
unless @files 0 4 4
29  
30 4 for my $file (@files) {
31 7 print "Compiling $file...\n";
32  
33 7 my @defs;
34 7 ▶ if ($file =~ /\.ydef$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /\.ydef$/) { } 3 4 7
35 3 @defs = @{ Yote::YapiServer::YapiDef::parse_file($file) };
36 } else {
37 4 @defs = (YAML::LoadFile($file));
38 }
39  
40 7 for my $yaml (@defs) {
41 7 ▶ my $type = $yaml->{type} // 'app';
Condition50%1/2
$yaml->{"type"} // "app"
l!l
70
MC/DC0%0/1
$yaml->{"type"} // "app"
$yaml->{"type"}
Truth table50%1/2
A
A= $yaml->{"type"}
Aresult
11
00
42  
43 7 my @outputs; # list of [package, content] pairs
44 7 ▶ if ($type eq 'app') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'app') { } 5 2 7
elsif ($type eq 'object') { } 0 2 2
elsif ($type eq 'server') { } 2 0 2
45 5 @outputs = compile_app($yaml);
46 } elsif ($type eq 'object') {
47 0 @outputs = ( [$yaml->{package}, compile_object($yaml)] );
48 } elsif ($type eq 'server') {
49 2 @outputs = ( [$yaml->{package}, compile_server($yaml)] );
50 } else {
51 0 die "Unknown type '$type' in $file\n";
52 }
53  
54 7 for my $pair (@outputs) {
55 16 write_output($pair->[0], $pair->[1], $outdir);
56 }
57 }
58 }
59  
60 4 print "Done.\n";
61 }
62  
63 #======================================================================
64 # Compile an app YAML into Perl
65 #======================================================================
66 sub compile_app {
67 5 ▶ my ($yaml) = @_;
Subroutine compile_app: called CC=42 SCAR=39.3
Pod undocumented
68 5 merge_method_shorthands($yaml);
69 5 my $pkg = $yaml->{package};
70 5 ▶ my $base = $yaml->{base} // 'Yote::YapiServer::App::Base';
Condition50%1/2
$yaml->{"base"} // "Yote::YapiServer::App::Base"
l!l
05
MC/DC0%0/1
$yaml->{"base"} // "Yote::YapiServer::App::Base"
$yaml->{"base"}
Truth table50%1/2
A
A= $yaml->{"base"}
Aresult
11
00
71  
72 5 my @out;
73  
74 # Package header
75 5 push @out, "package $pkg;";
76 5 push @out, "";
77 5 push @out, "use strict;";
78 5 push @out, "use warnings;";
79 5 push @out, "use base '$base';";
80  
81 # Extra uses
82 5 ▶ if ($yaml->{uses}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yaml->{"uses"}) 0 5 5
83 0 push @out, "";
84 0 for my $mod (@{$yaml->{uses}}) {
85 0 push @out, "use $mod;";
86 }
87 }
88  
89 # Use nested object packages so their tables get created
90 5 ▶ if ($yaml->{objects}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yaml->{"objects"}) 5 0 5
91 5 push @out, "";
92 5 for my $obj_name (sort keys %{$yaml->{objects}}) {
93 9 push @out, "use ${pkg}::${obj_name};";
94 }
95 }
96  
97 5 push @out, "";
98  
99 # Columns - merge with base
100 5 push @out, "# Inherit base columns and add our own";
101 5 push @out, "our \%cols = (";
102 5 push @out, " \%${base}::cols,";
103 5 ▶ if ($yaml->{cols}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yaml->{"cols"}) 5 0 5
104 5 for my $col (sort keys %{$yaml->{cols}}) {
105 9 my $val = expand_col_type($yaml->{cols}{$col}, $pkg);
106 9 push @out, " $col => '$val',";
107 }
108 }
109 5 push @out, ");";
110  
111 5 push @out, "";
112  
113 # Methods
114 5 push @out, "# Method access control";
115 5 push @out, "our \%METHODS = (";
116 5 ▶ if ($yaml->{methods}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yaml->{"methods"}) 5 0 5
117 5 my @pub; my @auth; my @admin;
118 5 for my $name (sort keys %{$yaml->{methods}}) {
119 29 my $def = $yaml->{methods}{$name};
120 29 ▶ my $access = $def->{access} // 'auth';
Condition50%1/2
$def->{"access"} // "auth"
l!l
290
MC/DC0%0/1
$def->{"access"} // "auth"
$def->{"access"}
Truth table50%1/2
A
A= $def->{"access"}
Aresult
11
00
121 29 ▶ if (ref $access eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $access eq 'HASH') { } 0 29 29
elsif ($access eq 'public') { } 10 19 29
elsif ($access eq 'admin_only') { } 1 18 19
122 0 push @auth, [$name, $def]; # compound access goes with auth
123 } elsif ($access eq 'public') {
124 10 push @pub, [$name, $def];
125 } elsif ($access eq 'admin_only') {
126 1 push @admin, [$name, $def];
127 } else {
128 18 push @auth, [$name, $def];
129 }
130 }
131 5 ▶ if (@pub) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pub) 5 0 5
132 5 push @out, " # Public - no auth required";
133 5 for my $item (@pub) {
134 10 my ($name, $def) = @$item;
135 10 my $pad = pad_to($name, \@pub, \@auth, \@admin);
136 10 push @out, " $name$pad => " . method_hash_str($def, 'public') . ",";
137 }
138 5 ▶ push @out, "" if @auth || @admin;
Branch: 1 branch
BranchTrueFalseTotal
if @auth or @admin 5 0 5
Condition33%1/3
@auth or @admin
l!l&&r!l&&!r
500
MC/DC0%0/2
@auth or @admin
@auth @admin
Truth table33%1/3
A or B
A= @authB= @admin
ABresult
1X1
011
000
139 }
140 5 ▶ if (@auth) {
Branch: 1 branch
BranchTrueFalseTotal
if (@auth) 5 0 5
141 5 push @out, " # Authenticated users";
142 5 for my $item (@auth) {
143 18 my ($name, $def) = @$item;
144 18 my $pad = pad_to($name, \@pub, \@auth, \@admin);
145 18 push @out, " $name$pad => " . method_hash_str($def, 'auth') . ",";
146 }
147 5 ▶ push @out, "" if @admin;
Branch: 1 branch
BranchTrueFalseTotal
if @admin 1 4 5
148 }
149 5 ▶ if (@admin) {
Branch: 1 branch
BranchTrueFalseTotal
if (@admin) 1 4 5
150 1 push @out, " # Admin only";
151 1 for my $item (@admin) {
152 1 my ($name, $def) = @$item;
153 1 my $pad = pad_to($name, \@pub, \@auth, \@admin);
154 1 push @out, " $name$pad => " . method_hash_str($def, 'admin_only') . ",";
155 }
156 }
157 }
158 5 push @out, ");";
159  
160 5 push @out, "";
161  
162 # Field access - merge with base
163 5 push @out, "# Field visibility";
164 5 push @out, "our \%FIELD_ACCESS = (";
165 5 push @out, " \%${base}::FIELD_ACCESS,";
166 5 ▶ if ($yaml->{field_access}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yaml->{"field_access"}) 3 2 5
167 3 for my $field (sort keys %{$yaml->{field_access}}) {
168 1 my $rule = $yaml->{field_access}{$field};
169 1 push @out, " $field => " . format_access_hash($rule) . ",";
170 }
171 }
172 5 push @out, ");";
173  
174 5 push @out, "";
175  
176 # Public vars
177 5 push @out, "# Public vars exposed on connect";
178 5 push @out, "our \%PUBLIC_VARS = (";
179 5 ▶ if ($yaml->{public_vars}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yaml->{"public_vars"}) 5 0 5
180 5 for my $k (sort keys %{$yaml->{public_vars}}) {
181 18 my $v = $yaml->{public_vars}{$k};
182 18 push @out, " $k => " . perl_value($v) . ",";
183 }
184 }
185 5 push @out, ");";
186  
187 # Package-level scalar vars
188 5 ▶ if ($yaml->{vars}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yaml->{"vars"}) 4 1 5
189 4 push @out, "";
190 4 for my $var (sort keys %{$yaml->{vars}}) {
191 8 my $v = $yaml->{vars}{$var};
192 8 push @out, "our \$$var = " . perl_value($v) . ";";
193 }
194 }
195  
196 # Method implementations
197 5 ▶ if ($yaml->{methods}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yaml->{"methods"}) 5 0 5
198 # Group by access for section headers
199 5 my @pub_methods; my @auth_methods; my @admin_methods;
200 5 for my $name (sort keys %{$yaml->{methods}}) {
201 29 my $def = $yaml->{methods}{$name};
202 29 ▶ my $access = $def->{access} // 'auth';
Condition50%1/2
$def->{"access"} // "auth"
l!l
290
MC/DC0%0/1
$def->{"access"} // "auth"
$def->{"access"}
Truth table50%1/2
A
A= $def->{"access"}
Aresult
11
00
203 29 ▶ my $access_str = ref $access ? 'auth' : $access;
Branch: 1 branch
BranchTrueFalseTotal
ref $access ? : 0 29 29
204 29 ▶ if ($access_str eq 'public') {
Branch: 2 branches
BranchTrueFalseTotal
if ($access_str eq 'public') { } 10 19 29
elsif ($access_str eq 'admin_only') { } 1 18 19
205 10 push @pub_methods, $name;
206 } elsif ($access_str eq 'admin_only') {
207 1 push @admin_methods, $name;
208 } else {
209 18 push @auth_methods, $name;
210 }
211 }
212  
213 5 for my $group (['Public methods', \@pub_methods],
214 ['Authenticated methods', \@auth_methods],
215 ['Admin methods', \@admin_methods]) {
216 15 my ($label, $names) = @$group;
217 15 ▶ next unless @$names;
Branch: 1 branch
BranchTrueFalseTotal
unless @$names 4 11 15
218 11 push @out, "";
219 11 push @out, "#" . "-" x 70;
220 11 push @out, "# $label";
221 11 push @out, "#" . "-" x 70;
222 11 for my $name (@$names) {
223 29 push @out, "";
224 29 my $code = $yaml->{methods}{$name}{code};
225 29 push @out, "sub $name {";
226 29 push @out, indent_code($code);
227 29 push @out, "}";
228 }
229 }
230 }
231  
232 # Non-API subs
233 5 ▶ if ($yaml->{subs}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yaml->{"subs"}) 0 5 5
234 0 push @out, "";
235 0 push @out, "#" . "-" x 70;
236 0 push @out, "# Utility methods";
237 0 push @out, "#" . "-" x 70;
238 0 for my $name (sort keys %{$yaml->{subs}}) {
239 0 push @out, "";
240 0 push @out, "sub $name {";
241 0 push @out, indent_code($yaml->{subs}{$name});
242 0 push @out, "}";
243 }
244 }
245  
246 5 push @out, "";
247 5 push @out, "1;";
248  
249 5 push @out, "";
250 5 push @out, "__END__";
251  
252 5 my @outputs = ( [$pkg, join("\n", @out) . "\n"] );
253  
254 # Nested objects - each gets its own file
255 5 ▶ if ($yaml->{objects}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yaml->{"objects"}) 5 0 5
256 5 for my $obj_name (sort keys %{$yaml->{objects}}) {
257 9 my $obj_def = $yaml->{objects}{$obj_name};
258 9 my $obj_pkg = "${pkg}::${obj_name}";
259 9 my $obj_content = compile_nested_object($obj_pkg, $obj_def, $pkg);
260 9 push @outputs, [$obj_pkg, $obj_content . "\n"];
261 }
262 }
263  
264 5 return @outputs;
265 }
266  
267 #======================================================================
268 # Compile a standalone object YAML
269 #======================================================================
270 sub compile_object {
271 0 ▶ my ($yaml) = @_;
Subroutine compile_object: not called CC=15 SCAR=54.8
Pod undocumented
272 0 merge_method_shorthands($yaml);
273 0 my $pkg = $yaml->{package};
274 0 ▶ my $base = $yaml->{base} // 'Yote::YapiServer::BaseObj';
Condition0%0/2
$yaml->{"base"} // "Yote::YapiServer::BaseObj"
l!l
00
MC/DC0%0/1
$yaml->{"base"} // "Yote::YapiServer::BaseObj"
$yaml->{"base"}
Truth table0%0/2
A
A= $yaml->{"base"}
Aresult
11
00
275  
276 0 my @out;
277  
278 0 push @out, "package $pkg;";
279 0 push @out, "";
280 0 push @out, "use strict;";
281 0 push @out, "use warnings;";
282 0 push @out, "use base '$base';";
283  
284 0 ▶ if ($yaml->{uses}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yaml->{"uses"}) 0 0 0
285 0 push @out, "";
286 0 for my $mod (@{$yaml->{uses}}) {
287 0 push @out, "use $mod;";
288 }
289 }
290  
291 0 push @out, "";
292  
293 # Columns (no base merging for standalone objects)
294 0 push @out, "# Database column definitions";
295 0 push @out, "our \%cols = (";
296 0 ▶ if ($yaml->{cols}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yaml->{"cols"}) 0 0 0
297 0 for my $col (sort keys %{$yaml->{cols}}) {
298 0 my $val = expand_col_type($yaml->{cols}{$col}, $pkg);
299 0 push @out, " $col => '$val',";
300 }
301 }
302 0 push @out, ");";
303  
304 0 push @out, "";
305  
306 # Field access
307 0 push @out, "# Field visibility rules for client serialization";
308 0 push @out, "our \%FIELD_ACCESS = (";
309 0 ▶ if ($yaml->{field_access}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yaml->{"field_access"}) 0 0 0
310 0 for my $field (sort keys %{$yaml->{field_access}}) {
311 0 my $rule = $yaml->{field_access}{$field};
312 0 push @out, " $field => " . format_access_hash($rule) . ",";
313 }
314 }
315 0 push @out, ");";
316  
317 0 push @out, "";
318  
319 # Methods
320 0 push @out, "# Methods callable from client";
321 0 push @out, "our \%METHODS = (";
322 0 ▶ if ($yaml->{methods}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yaml->{"methods"}) 0 0 0
323 0 for my $name (sort keys %{$yaml->{methods}}) {
324 0 my $def = $yaml->{methods}{$name};
325 0 push @out, " $name => " . method_hash_str($def) . ",";
326 }
327 }
328 0 push @out, ");";
329  
330 # field_access and method_defs inherited from Yote::YapiServer::BaseObj
331  
332 # Method implementations
333 0 ▶ if ($yaml->{methods}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yaml->{"methods"}) 0 0 0
334 0 push @out, "";
335 0 push @out, "#" . "-" x 70;
336 0 push @out, "# Client-callable methods";
337 0 push @out, "#" . "-" x 70;
338 0 for my $name (sort keys %{$yaml->{methods}}) {
339 0 my $def = $yaml->{methods}{$name};
340 0 ▶ next unless $def->{code};
Branch: 1 branch
BranchTrueFalseTotal
unless $def->{"code"} 0 0 0
341 0 push @out, "";
342 0 push @out, "sub $name {";
343 0 push @out, indent_code($def->{code});
344 0 push @out, "}";
345 }
346 }
347  
348 # Non-API subs
349 0 ▶ if ($yaml->{subs}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yaml->{"subs"}) 0 0 0
350 0 push @out, "";
351 0 push @out, "#" . "-" x 70;
352 0 push @out, "# Server-side utility methods";
353 0 push @out, "#" . "-" x 70;
354 0 for my $name (sort keys %{$yaml->{subs}}) {
355 0 push @out, "";
356 0 push @out, "sub $name {";
357 0 push @out, indent_code($yaml->{subs}{$name});
358 0 push @out, "}";
359 }
360 }
361  
362 0 push @out, "";
363 0 push @out, "1;";
364  
365 0 push @out, "";
366 0 push @out, "__END__";
367  
368 0 return join("\n", @out) . "\n";
369 }
370  
371 #======================================================================
372 # Compile a server YAML
373 #======================================================================
374 sub compile_server {
375 2 ▶ my ($yaml) = @_;
Subroutine compile_server: called CC=6 SCAR=17.9
Pod undocumented
376 2 my $pkg = $yaml->{package};
377 2 ▶ my $base = $yaml->{base} // 'Yote::YapiServer::Site';
Condition50%1/2
$yaml->{"base"} // "Yote::YapiServer::Site"
l!l
20
MC/DC0%0/1
$yaml->{"base"} // "Yote::YapiServer::Site"
$yaml->{"base"}
Truth table50%1/2
A
A= $yaml->{"base"}
Aresult
11
00
378  
379 2 my @out;
380  
381 2 push @out, "package $pkg;";
382 2 push @out, "";
383 2 push @out, "use strict;";
384 2 push @out, "use warnings;";
385 2 push @out, "use base '$base'; # extends Yote::SQLObjectStore::BaseObj";
386  
387 2 ▶ if ($yaml->{uses}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yaml->{"uses"}) 2 0 2
388 2 push @out, "";
389 2 for my $mod (@{$yaml->{uses}}) {
390 2 push @out, "use $mod;";
391 }
392 }
393  
394 2 push @out, "";
395  
396 2 push @out, "our \%cols = (";
397 2 push @out, " \%${base}::cols,";
398 2 push @out, ");";
399  
400 2 push @out, "";
401  
402 2 push @out, "our \%INSTALLED_APPS = (";
403 2 push @out, " \%${base}::INSTALLED_APPS,";
404 2 ▶ if ($yaml->{apps}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yaml->{"apps"}) 2 0 2
405 2 for my $name (sort keys %{$yaml->{apps}}) {
406 2 push @out, " $name => '$yaml->{apps}{$name}',";
407 }
408 }
409 2 push @out, ");";
410  
411 2 push @out, "";
412 2 push @out, "sub installed_apps {";
413 2 push @out, " return \\\%INSTALLED_APPS;";
414 2 push @out, "}";
415  
416 2 push @out, "";
417 2 push @out, "1;";
418  
419 2 return join("\n", @out) . "\n";
420 }
421  
422 #======================================================================
423 # Compile a nested object (inside an app)
424 #======================================================================
425 sub compile_nested_object {
426 9 ▶ my ($obj_pkg, $def, $parent_pkg) = @_;
Subroutine compile_nested_object: called CC=10 SCAR=24.0
Pod undocumented
427 9 merge_method_shorthands($def);
428 9 my @out;
429  
430 9 push @out, "package $obj_pkg;";
431 9 push @out, "";
432 9 push @out, "use strict;";
433 9 push @out, "use warnings;";
434 9 push @out, "use base 'Yote::YapiServer::BaseObj';";
435  
436 9 push @out, "";
437 9 push @out, "our \%cols = (";
438 9 ▶ if ($def->{cols}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($def->{"cols"}) 9 0 9
439 9 for my $col (sort keys %{$def->{cols}}) {
440 39 my $val = expand_col_type($def->{cols}{$col}, $parent_pkg);
441 39 push @out, " $col => '$val',";
442 }
443 }
444 9 push @out, ");";
445  
446 9 push @out, "";
447 9 push @out, "our \%FIELD_ACCESS = (";
448 9 ▶ if ($def->{field_access}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($def->{"field_access"}) 9 0 9
449 9 for my $field (sort keys %{$def->{field_access}}) {
450 39 my $rule = $def->{field_access}{$field};
451 39 push @out, " $field => " . format_access_hash($rule) . ",";
452 }
453 }
454 9 push @out, ");";
455  
456 9 push @out, "";
457 9 push @out, "our \%METHODS = ();";
458  
459 # field_access, method_defs, to_client_hash inherited from Yote::YapiServer::BaseObj
460  
461 # Auto-generate _client_class_name from object name
462 9 my ($obj_name) = $obj_pkg =~ /::(\w+)$/;
463 9 push @out, "";
464 9 push @out, "sub _client_class_name { return '$obj_name'; }";
465  
466 # to_client_hash inherited from BaseObj; custom overrides via subs:
467  
468 # Object-level methods
469 9 ▶ if ($def->{methods}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($def->{"methods"}) 0 9 9
470 0 for my $name (sort keys %{$def->{methods}}) {
471 0 my $mdef = $def->{methods}{$name};
472 0 push @out, "";
473 0 push @out, "sub $name {";
474 0 ▶ push @out, indent_code($mdef->{code} // $mdef);
Condition0%0/3
$mdef->{"code"} // $mdef
l!l&&r!l&&!r
000
MC/DC0%0/2
$mdef->{"code"} // $mdef
$mdef->{"code"} $mdef
Truth table0%0/3
A // B
A= $mdef->{"code"}B= $mdef
ABresult
1X1
011
000
475 0 push @out, "}";
476 }
477 }
478  
479 # Non-API subs on objects
480 9 ▶ if ($def->{subs}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($def->{"subs"}) 4 5 9
481 4 for my $name (sort keys %{$def->{subs}}) {
482 4 push @out, "";
483 4 push @out, "sub $name {";
484 4 push @out, indent_code($def->{subs}{$name});
485 4 push @out, "}";
486 }
487 }
488  
489 9 push @out, "";
490 9 push @out, "1;";
491  
492 9 push @out, "";
493 9 push @out, "__END__";
494  
495 9 return join("\n", @out);
496 }
497  
498 #======================================================================
499 # Helpers
500 #======================================================================
501  
502 # Merge methods_public and methods_auth into methods with implicit access
503 sub merge_method_shorthands {
504 14 ▶ my ($yaml) = @_;
Subroutine merge_method_shorthands: called CC=8 SCAR=38.1
Pod undocumented
505 14 for my $key (qw(methods_public methods_auth)) {
506 28 ▶ next unless $yaml->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $yaml->{$key} 28 0 28
507 0 ▶ my $access = $key eq 'methods_public' ? 'public' : 'auth';
Branch: 1 branch
BranchTrueFalseTotal
$key eq 'methods_public' ? : 0 0 0
508 0 ▶ $yaml->{methods} //= {};
Condition0%0/2
$yaml->{"methods"} //= {}
l!l
00
MC/DC0%0/1
$yaml->{"methods"} //= {}
$yaml->{"methods"}
Truth table0%0/2
A
A= $yaml->{"methods"}
Aresult
11
00
509 0 for my $name (keys %{$yaml->{$key}}) {
510 0 my $def = $yaml->{$key}{$name};
511 # Accept bare code string or hash with code key
512 0 ▶ if (!ref $def) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $def) 0 0 0
513 0 $def = { code => $def };
514 }
515 0 ▶ $def->{access} //= $access;
Condition0%0/3
$def->{"access"} //= $access
l!l&&r!l&&!r
000
MC/DC0%0/2
$def->{"access"} //= $access
$def->{"access"} $access
Truth table0%0/3
A //= B
A= $def->{"access"}B= $access
ABresult
1X1
011
000
516 0 $yaml->{methods}{$name} = $def;
517 }
518 0 delete $yaml->{$key};
519 }
520 }
521  
522 # Expand relative package references: *::Foo -> *Parent::Package::Foo
523 # Only expands when * is immediately followed by :: (relative ref)
524 # Leaves absolute refs like *Yote::YapiServer::User unchanged
525 sub expand_col_type {
526 48 ▶ my ($type, $parent_pkg) = @_;
Subroutine expand_col_type: called CC=1 SCAR=0.0
Pod undocumented
527 # *::Name or *ARRAY_*::Name patterns
528 48 $type =~ s/\*::/\*${parent_pkg}::/g;
529 48 return $type;
530 }
531  
532 # Build method hash string from definition, including files flag
533 sub method_hash_str {
534 29 ▶ my ($def, $default_access) = @_;
Subroutine method_hash_str: called CC=7 SCAR=21.1
Pod undocumented
535 29 ▶ my $access = $def->{access} // $default_access // 'auth';
Condition40%2/5
($def->{"access"} // $default_access) // "auth"
l!l
290
$def->{"access"} // $default_access
l!l&&r!l&&!r
2900
MC/DC0%0/2
($def->{"access"} // $default_access) // "auth"
$def->{"access"} $default_access
Truth table33%1/3
A // B
A= $def->{"access"}B= $default_access
ABresult
1X1
011
000
536 29 my @parts;
537  
538 29 ▶ if (ref $access eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $access eq 'HASH') { } 0 29 29
elsif ($access eq 'public') { } 10 19 29
elsif ($access eq 'admin_only') { } 1 18 19
539 0 push @parts, map { "$_ => $access->{$_}" } sort keys %$access;
540 } elsif ($access eq 'public') {
541 10 push @parts, "public => 1";
542 } elsif ($access eq 'admin_only') {
543 1 push @parts, "admin_only => 1";
544 } else {
545 18 push @parts, "auth => 1";
546 }
547  
548 29 ▶ push @parts, "files => 1" if $def->{files};
Branch: 1 branch
BranchTrueFalseTotal
if $def->{"files"} 0 29 29
549  
550 29 return "{ " . join(", ", @parts) . " }";
551 }
552  
553 # Format access rule as Perl hash ref
554 sub format_access_hash {
555 40 ▶ my ($rule) = @_;
Subroutine format_access_hash: called CC=2 SCAR=6.9
Pod undocumented
556 40 ▶ if (ref $rule eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rule eq "HASH") 1 39 40
557 1 my @parts = map { "$_ => $rule->{$_}" } sort keys %$rule;
558 1 return "{ " . join(", ", @parts) . " }";
559 }
560 # Simple string like "public", "auth", "never", "owner_only", "admin_only"
561 39 return "{ $rule => 1 }";
562 }
563  
564 # Format a Perl value (string or number)
565 sub perl_value {
566 26 ▶ my ($v) = @_;
Subroutine perl_value: called CC=2 SCAR=6.9
Pod undocumented
567 26 ▶ return $v if $v =~ /^\d+$/; # integer
Branch: 1 branch
BranchTrueFalseTotal
if $v =~ /^\d+$/ 12 14 26
568 14 return "'$v'"; # string
569 }
570  
571 # Indent code block for insertion into sub body
572 sub indent_code {
573 33 ▶ my ($code) = @_;
Subroutine indent_code: called CC=2 SCAR=7.0
Pod undocumented
574 33 ▶ return "" unless defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 33 33
575 33 chomp $code;
576 33 my @lines = split /\n/, $code;
577 33 return join("\n", map { " $_" } @lines);
578 }
579  
580 # Calculate padding for alignment
581 sub pad_to {
582 29 ▶ my ($name, @groups) = @_;
Subroutine pad_to: called CC=6 SCAR=17.9
Pod undocumented
583 29 my $max = 0;
584 29 for my $group (@groups) {
585 87 for my $item (@$group) {
586 169 ▶ my $n = ref $item eq 'ARRAY' ? $item->[0] : $item;
Branch: 1 branch
BranchTrueFalseTotal
ref $item eq 'ARRAY' ? : 169 0 169
587 169 ▶ $max = length($n) if length($n) > $max;
Branch: 1 branch
BranchTrueFalseTotal
if length $n > $max 39 130 169
588 }
589 }
590 29 my $pad = $max - length($name);
591 29 ▶ return $pad > 0 ? ' ' x $pad : '';
Branch: 1 branch
BranchTrueFalseTotal
$pad > 0 ? : 20 9 29
592 }
593  
594 # Write generated Perl to the correct file path
595 sub write_output {
596 16 ▶ my ($package, $content, $outdir) = @_;
Subroutine write_output: called CC=3 SCAR=11.0
Pod undocumented
597  
598 16 my $path = $package;
599 16 $path =~ s/::/\//g;
600 16 $path = "$outdir/$path.pm";
601  
602 16 my $dir = dirname($path);
603 16 ▶ make_path($dir) unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 8 8 16
604  
605 16 ▶ open my $fh, '>', $path or die "Cannot write $path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $path or die "Cannot write ${path}: $!\n" 0 16 16
606 16 print $fh $content;
607 16 close $fh;
608  
609 16 print " -> $path\n";
610 }
611  
612 1 1;
« lib/Yote/YapiServer/User.pm ↑ Summary blib/lib/Yote/YapiServer/YapiDef.pm »