File Coverage

lib/Web/DataService/Diagnostic.pm
Criterion Covered Total %
statement 9 293 3.0
branch 0 174 0.0
condition 0 115 0.0
subroutine 3 19 15.7
pod 0 16 0.0
total 12 617 1.9


line stmt bran cond sub pod time code
1             #
2             # Web::DataService::Diagnostic
3             #
4             # This module provides a role that is used by 'Web::DataService'. It implements
5             # routines for generating diagnostic output about the data service.
6             #
7             # Author: Michael McClennen
8              
9 2     2   17 use strict;
  2         5  
  2         90  
10              
11             package Web::DataService::Diagnostic;
12              
13 2     2   13 use Carp qw(carp croak);
  2         5  
  2         113  
14              
15 2     2   13 use Moo::Role;
  2         6  
  2         14  
16              
17              
18             our ($CWD);
19              
20             our (%DIAG_PARAM) = ( show => 1, splat => 1 );
21              
22             # diagnostic_request ( @args )
23             #
24             # Generate diagnostic output requested from the command-line. This is done by
25             # running the data service application with the following command-line
26             # arguments:
27             #
28             # app_name diag
29             #
30             # The path is used to select a data service node, in the same way as if
31             # responding to a request, and the parameters (using the same syntax as URL
32             # parameters) specify the exact diagnostic output to be generated.
33             #
34             # Examples:
35             #
36             # app_name diag /data/records/list 'show=digest'
37             #
38             # app_name diag /data/ 'show=fields&doc=short'
39             #
40             # The path argument is required, and so is the parameter "show". The
41             # parameter string must follow URL parameter syntax, so don't include any
42             # whitespace around the = and & signs. Possible values are:
43             #
44             # show=digest
45             #
46             # Generate a digest of the configuration of one or more data service
47             # nodes, serialized into YAML. This can be saved to a file and then
48             # analyzed by a separate program. The purpose of this functionality is to
49             # provide for the generation of a report summarizing the differences
50             # between the user interfaces of different data service versions
51             # (parameters, responses, formats, vocabularies, etc.), for the purposes
52             # of generating change logs and other documentation. By default, the node
53             # specified by the path argument is included in the digest, plus
54             # (recursively) every node that it links to. The digest will also include
55             # every output block, set, and ruleset linked by any of the nodes.
56             #
57             # The following additional parameters are available:
58             #
59             # node =
60             #
61             # Only include nodes whose path matches the specified string, which
62             # may contain the standard shell wildcards * and ?. You can use this
63             # to select a subset of the nodes that would otherwise be included.
64             #
65             # show=fields
66             #
67             # Generate a report (as unformatted text) that tabulates all of the output
68             # field names matching the other parameters. This functionality can be
69             # used to make sure that field names and values are consistent between all
70             # of the different data service operations. The path argument is used only to
71             # select which data service to analyze, if the application defines more
72             # than one. Other parameters include:
73             #
74             # vocab =
75             #
76             # Only report output field names from the specified vocabulary. If
77             # the specified vocabulary has the 'use_field_names' attribute, then
78             # the names of the underlying data fields will be used whenever no
79             # name was explicitly specified for this vocabulary.
80             #
81             # name =
82             #
83             # Only report output field names which match the specified pattern.
84             # The pattern may contain the standard shell wildcards * and ?.
85             #
86             # data =
87             #
88             # Only report output field names which are linked to a data field
89             # matching the specified pattern. The pattern may contain the standard
90             # shell wildcards * and ?.
91             #
92              
93             sub diagnostic_request {
94              
95 0     0 0   my ($ds, $request) = @_;
96            
97             # Start by getting the request parameters, which will tell us which
98             # diagnostic operation was requested.
99            
100 0           my $params = $Web::DataService::FOUNDATION->get_params($request);
101            
102 0 0         my $diag = $params->{show} ? lc $params->{show} : '';
103            
104 0 0         if ( $diag eq 'fields' )
    0          
105             {
106 0           return $ds->diagnostic_fields($request, $params);
107             }
108            
109             elsif ( $diag eq 'digest' )
110             {
111 0           return $ds->diagnostic_digest($request, $params);
112             }
113            
114             else
115             {
116 0           print STDERR "Usage:\n\n $0 diag [path] 'show=fields'\n $0 diag [path] 'show=digest'\n\n";
117 0           return;
118             }
119             }
120              
121              
122             our (%FIELD_PARAM) = ( name => 1,
123             vocab => 1,
124             data => 1,
125             doc => 1 );
126              
127             # diagnostic_fields ( request )
128             #
129             # Generate diagnostic information about the output fields defined for this
130             # data service. See above for documentation. The report generated by this
131             # function is written to standard output, because it is designed to be run
132             # only from the command-line.
133              
134             sub diagnostic_fields {
135            
136 0     0 0   my ($ds, $request, $params) = @_;
137            
138             # First check the query parameters.
139            
140 0           my $bad_param;
141            
142 0           foreach my $key ( keys %$params )
143             {
144 0 0 0       unless ( $DIAG_PARAM{$key} || $FIELD_PARAM{$key} )
145             {
146 0           print STDERR "ERROR: unknown parameter '$key'\n";
147 0           $bad_param = 1;
148             }
149             }
150            
151 0 0         return if $bad_param;
152            
153 0           my $query_vocab = $params->{vocab};
154 0           my $query_name = &diag_generate_regex($params->{name});
155 0           my $query_field = &diag_generate_regex($params->{data});
156            
157 0 0 0       if ( $query_vocab and not $ds->{vocab}{$query_vocab} )
158             {
159 0           print STDERR "ERROR: vocabulary '$query_vocab' is not defined for this data service.\n";
160 0           return;
161             }
162            
163 0           my @query;
164            
165 0 0         push @query, "vocab = $query_vocab" if $query_vocab;
166 0 0         push @query, "name = $params->{name}" if $query_name;
167 0 0         push @query, "data = $params->{data}" if $query_field;
168            
169             # Now check each of the output blocks defined for this data service, looking
170             # for fields in those blocks that match the query parameters.
171            
172 0           my (%by_name);
173            
174 0           foreach my $block ( keys %{$ds->{block}} )
  0            
175             {
176 0           my $output_list = $ds->{block}{$block}{output_list};
177 0 0         next unless ref $output_list eq 'ARRAY';
178            
179             # Check all entries in the block's output list.
180            
181             FIELD:
182 0           foreach my $f ( @$output_list )
183             {
184             # Ignore any entry in the output_list that does not correspond to
185             # an output field.
186            
187 0 0 0       next unless ref $f eq 'HASH' && $f->{output};
188            
189             # If an output name was speciied, then ignore any entries whose
190             # 'output' attribute does not match.
191            
192 0 0         if ( $query_field )
193             {
194 0 0         next FIELD if $f->{output} !~ $query_field;
195             }
196            
197             # If a vocabulary was specified, then determine the output name.
198             # Ignore entries which do not have a name under this vocabulary.
199            
200 0           my (@matches);
201            
202 0 0         if ( $query_vocab )
203             {
204 0   0       my $name = $f->{"${query_vocab}_name"} ||= $f->{name};
205 0 0 0       $name ||= $f->{output} if $ds->{vocab}{$query_vocab}{use_field_names};
206            
207 0   0       my $value = $f->{"${query_vocab}_value"} || $f->{value};
208            
209 0 0 0       next unless defined $name && $name ne '';
210 0 0 0       next if $query_name && $name !~ $query_name;
211            
212 0           push @matches, [$name, $query_vocab, $value];
213             }
214            
215             # If no vocabulary was specified, then determine the output name
216             # under each available vocabulary.
217            
218             else
219             {
220 0           foreach my $v ( @{$ds->{vocab_list}} )
  0            
221             {
222 0   0       my $name = $f->{"${v}_name"} || $f->{name};
223 0 0 0       $name ||= $f->{output} if $ds->{vocab}{$v}{use_field_names};
224            
225 0   0       my $value = $f->{"${v}_value"} || $f->{value};
226            
227 0 0 0       next unless defined $name && $name ne '';
228 0 0 0       next if $query_name && $name !~ $query_name;
229            
230 0           push @matches, [$name, $v, $value];
231             }
232             }
233            
234             # Ignore entries for which we did not find at least one match.
235            
236 0 0         next FIELD unless @matches;
237            
238             # If we get to this point, then the entry matches the query so
239             # tabulate it by each name.
240            
241 0           foreach my $m (@matches)
242             {
243 0           my ($name, $vocab, $value) = @$m;
244 0           my $new = { block => $block, vocab => $vocab, vvalue => $value, %$f };
245            
246 0           push @{$by_name{"$vocab:$name"}}, $new;
  0            
247             }
248             }
249             }
250            
251             # Get the current working directory, so we can trim path names.
252            
253 0           require "Cwd.pm"; $CWD = &Cwd::getcwd;
  0            
254            
255             # Go through the entries and compute field widths.
256            
257 0           my $options = { doc => $params->{doc}, values => $params->{values} };
258            
259 0           my @column_widths = 0 x 5;
260            
261 0           foreach my $key ( sort { lc $a cmp lc $b } keys %by_name )
  0            
262             {
263 0           foreach my $f ( @{$by_name{$key}} )
  0            
264             {
265 0           $ds->diag_field_widths($key, $f, $options, \@column_widths);
266             }
267             }
268            
269             # Now use this list of rows to print out a report tabulated by field name.
270            
271 0           $options->{template} = " %-$column_widths[0]s %-$column_widths[1]s %-$column_widths[2]s %-$column_widths[3]s\n";
272            
273 0           print STDOUT "\n";
274 0           print STDOUT "DIAGNOSTIC: FIELDS " . join(', ', @query) . "\n";
275 0           print STDOUT "===============================================================================\n\n";
276            
277 0           print STDOUT " field name\n\n";
278            
279 0           my @headings = qw(field block conditionals definition);
280            
281 0           foreach my $i (0..3)
282             {
283 0 0         $headings[$i] = '' unless $column_widths[$i];
284             }
285            
286 0           print STDOUT sprintf($options->{template}, @headings);
287 0           print STDOUT "\n";
288            
289 0           foreach my $key ( sort { lc $a cmp lc $b } keys %by_name )
  0            
290             {
291 0           my ($vocab, $name) = split qr/:/, $key;
292            
293 0           print STDOUT " $vocab : '$name'\n\n";
294            
295 0           foreach my $f ( @{$by_name{$key}} )
  0            
296             {
297 0           $ds->diag_field_output($name, $f, $options);
298             }
299            
300 0           print STDOUT "\n";
301             }
302            
303 0 0         unless ( keys %by_name )
304             {
305 0           print STDOUT "No matching fields were found.\n\n";
306             }
307             }
308              
309              
310             # diag_field_widths ( name, record, options, widths )
311             #
312             # By repeatedly calling this function for each output record, the maximum
313             # width for each field will be computed.
314              
315             sub diag_field_widths {
316            
317 0     0 0   my ($ds, $name, $record, $options, $widths) = @_;
318            
319 0           my ($block, $loc, $output, @conditionals);
320            
321 0           $block = $record->{block};
322 0           $loc = $ds->{block_loc}{$block};
323 0           $output = $record->{output};
324            
325 0           $loc =~ s{$CWD/}{};
326            
327 0 0 0       $output .= " \"$record->{vvalue}\"" if defined $record->{vvalue} && $record->{vvalue} ne '';
328            
329 0 0 0       $widths->[0] = length($output) if !defined $widths->[0] || length($output) > $widths->[0];
330 0 0 0       $widths->[1] = length($block) if !defined $widths->[1] || length($block) > $widths->[1];
331            
332 0           foreach my $c ( qw(if_block not_block if_vocab not_vocab if_field not_field
333             if_format not_format if_code not_code) )
334             {
335 0           my $value = $record->{$c};
336 0 0         next unless $value;
337            
338 0 0         $value = join(q{, }, @$value) if ref $value eq 'ARRAY';
339 0           my $cond = "$c $value";
340 0 0 0       $widths->[2] = length($cond) if !defined $widths->[2] || length($cond) > $widths->[2];
341             }
342            
343 0 0 0       $widths->[3] = length($loc) if !defined $widths->[3] || length($loc) > $widths->[3];
344            
345 0           foreach my $i ( 0..4 )
346             {
347 0   0       $widths->[$i] //= '0';
348             }
349             }
350              
351              
352             # diag_field_output ( name, record, options )
353             #
354             # Generate a description of a single output field and write it to standard
355             # output.
356              
357             sub diag_field_output {
358            
359 0     0 0   my ($ds, $name, $record, $options) = @_;
360            
361 0           my ($block, $loc, $output, @conditionals);
362            
363 0           $block = $record->{block};
364 0           $loc = $ds->{block_loc}{$block};
365 0           $output = $record->{output};
366            
367 0           $loc =~ s{$CWD/}{};
368            
369 0 0 0       $output .= " \"$record->{vvalue}\"" if defined $record->{vvalue} && $record->{vvalue} ne '';
370            
371 0           foreach my $c ( qw(if_block not_block if_vocab not_vocab if_field not_field
372             if_format not_format if_code not_code) )
373             {
374 0           my $value = $record->{$c};
375 0 0         next unless $value;
376            
377 0 0         $value = join(q{, }, @$value) if ref $value eq 'ARRAY';
378 0           push @conditionals, "$c $value";
379             }
380            
381 0 0         push @conditionals, '' unless @conditionals;
382            
383 0           print STDOUT sprintf($options->{template}, $output, $block, $conditionals[0], $loc);
384            
385 0           for ( my $i = 1; $i < @conditionals; $i++ )
386             {
387 0           print STDOUT sprintf($options->{template}, '', '>>>', $conditionals[$i], '');
388             }
389            
390 0 0 0       if ( $options->{doc} && defined $record->{doc_string} && $record->{doc_string} ne '' )
      0        
391             {
392 0           my $doc = $record->{doc_string};
393            
394 0 0         if ( $options->{doc} eq 'long' )
395             {
396 0           $doc =~ s/\n/"\n "/gs;
397             }
398             else
399             {
400 0           $doc =~ s/\n.*//s;
401             }
402            
403 0           print STDOUT " \"$doc\"\n";
404             }
405             }
406              
407              
408             # generate_regex ( string )
409             #
410             # Generate a regular expression that will match the given string, with * and ?
411             # as wildcards.
412              
413             sub diag_generate_regex {
414            
415 0     0 0   my ($string) = @_;
416            
417 0 0 0       return unless defined $string && $string ne '';
418            
419 0           $string =~ s/[*]/.*/g;
420 0           $string =~ s/[?]/./g;
421            
422 0           return qr{^$string$};
423             }
424              
425              
426             our (%DIGEST_PARAM) = ( node => 1 );
427              
428             # diagnostic_digest ( request, params )
429             #
430             # Generate diagnostic information about the user-level specification of this
431             # data service: the parameters accepted for each operation and the result
432             # fields returned.
433              
434             sub diagnostic_digest {
435            
436 0     0 0   my ($ds, $request, $params) = @_;
437            
438             # First check the query parameters.
439            
440 0           my $bad_param;
441            
442 0           foreach my $key ( keys %$params )
443             {
444 0 0 0       unless ( $DIAG_PARAM{$key} || $DIGEST_PARAM{$key} )
445             {
446 0           print STDERR "ERROR: unknown parameter '$key'\n";
447 0           $bad_param = 1;
448             }
449             }
450            
451 0 0         return if $bad_param;
452            
453             # Then check the node corresponding to the request path. If it is an
454             # operation node, report the specification of that operation.
455            
456 0           my $path = $request->node_path;
457 0           my $node_query = &diag_generate_regex($params->{node});
458 0           my $digest = { };
459            
460 0 0         if ( $node_query )
461             {
462 0           $digest->{node_query} = $node_query;
463 0           $digest->{_node_query} = $params->{node};
464             }
465            
466             # Add all of the nodes defined for this data service.
467            
468 0           foreach my $p ( sort keys %{$ds->{node_attrs}} )
  0            
469             {
470 0           $ds->diag_add_node($digest, $p);
471             }
472            
473             # Add values from the data service object.
474            
475 0           $ds->diag_add_ds_obj($digest);
476            
477             # Delete keys that were used only during the digest process.
478            
479 0           delete $digest->{node_query};
480            
481             # Now dump the entire specification as a YAML file.
482            
483 0           require "YAML.pm";
484            
485 0           binmode(STDOUT, ":utf8");
486 0           print STDOUT YAML::Dump($digest);
487             }
488              
489              
490             # diag_add_ds_obj ( digest )
491             #
492             # Add a record to the specified digest object to represent important fields
493             # from the data service object that are not specific to any node.
494              
495             sub diag_add_ds_obj {
496              
497 0     0 0   my ($ds, $digest) = @_;
498            
499 0           $digest->{_wds_version} = $Web::DataService::VERSION;
500            
501 0 0         $digest->{ds}{feature} = { %{$ds->{feature}} } if ref $ds->{feature} eq 'HASH';
  0            
502 0 0         $digest->{ds}{special} = { %{$ds->{special}} } if ref $ds->{special} eq 'HASH';
  0            
503 0 0         $digest->{ds}{special_alias} = { %{$ds->{special_alias}} } if ref $ds->{special_alias} eq 'HASH';
  0            
504 0 0         $digest->{ds}{format} = { %{$ds->{format}} } if ref $ds->{format} eq 'HASH';
  0            
505 0 0         $digest->{ds}{format_list} = [ @{$ds->{format_list}} ] if ref $ds->{format_list} eq 'ARRAY';
  0            
506 0 0         $digest->{ds}{vocab} = { %{$ds->{vocab}} } if ref $ds->{vocab} eq 'HASH';
  0            
507 0 0         $digest->{ds}{vocab_list} = [ @{$ds->{vocab_list}} ] if ref $ds->{vocab_list} eq 'ARRAY';
  0            
508            
509 0           foreach my $key ( qw( name title version path_prefix path_re ruleset_prefix
510             data_source data_provider data_license license_url
511             contact_name contact_email ) )
512             {
513 0           $digest->{ds}{$key} = $ds->{$key};
514             }
515             }
516              
517              
518             # diag_add_node ( digest, path, options )
519             #
520             # Add a record to the specified digest object to represent the specified
521             # node. Then recursively add records to represent all of the other objects
522             # (nodes, output blocks, sets, rulesets) linked to it.
523              
524             sub diag_add_node {
525            
526 0     0 0   my ($ds, $digest, $path) = @_;
527            
528             # First do some basic checks. Return without doing anything unless we
529             # have an actual path, and return immediately if we have already added
530             # this path.
531            
532 0 0 0       return unless defined $path && $path ne '';
533 0 0         return if $digest->{node}{$path};
534            
535             # Fail gracefully if this node doesn't exist, by simply returning. We
536             # want to complete the specification as best we can. The caller is
537             # responsible for checking and adding and error message if necessary.
538            
539 0 0         return unless ref $ds->{node_attrs}{$path};
540            
541             # If a node query parameter was specified, then only include nodes whose
542             # path matches the specified pattern.
543            
544 0 0 0       if ( ref $digest->{node_query} eq 'Regexp' && $path !~ $digest->{node_query} )
545             {
546 0           return;
547             }
548            
549             # Otherwise copy all of the node attributes into a new hash and add it to
550             # the specification.
551            
552             # print STDERR "Added node $path\n";
553            
554 0           my $node = { %{$ds->{node_attrs}{$path}} };
  0            
555            
556 0           $digest->{node}{$path} = $node;
557            
558             # Then compute some important attributes that might be inherited.
559            
560 0           foreach my $key ( qw(disabled undocumented role method arg ruleset output output_label
561             optional_output summary public_access default_format default_limit
562             default_header default_datainfo default_count default_linebreak
563             default_save_filename allow_method allow_format allow_vocab) )
564             {
565 0           my $value = $ds->node_attr($path, $key);
566            
567 0 0 0       if ( defined $value && $value ne '' )
568             {
569 0           $node->{$key} = $value;
570             }
571             }
572            
573             # Then compute some other values that might not be specified directly.
574            
575 0   0       $node->{ruleset} ||= $ds->determine_ruleset($path);
576            
577 0 0         if ( my @subnode_list = $ds->get_nodelist($path) )
578             {
579             # my @subnode_paths = map { $_->{path} } @subnode_list;
580 0           $node->{node_list} = \@subnode_list;
581             }
582            
583             # Then we go through and figure out all of the blocks, sets, and rulesets
584             # referenced by this node and add those to the specification as well.
585            
586 0           my (@show_list, @block_list);
587            
588 0           my @out_list = &diag_list_value($node, 'output');
589            
590 0           foreach my $blockname ( @out_list )
591             {
592 0           push @block_list, $blockname;
593 0           $ds->diag_add_block($digest, $blockname);
594 0           $ds->diag_add_check($digest, 'block', $blockname, "node '$path': output");
595             }
596            
597 0           my @summary = &diag_list_value($node, 'summary');
598            
599 0           foreach my $blockname ( @summary )
600             {
601 0           $ds->diag_add_block($digest, $blockname);
602 0           $ds->diag_add_check($digest, 'block', $blockname, "node '$path': summary");
603             }
604            
605 0 0         if ( my $outmap = $node->{optional_output} )
606             {
607 0           my $set = $ds->{set}{$outmap};
608            
609 0 0         if ( $ds->diag_add_check($digest, 'set', $outmap, "node '$path': optional_output") )
610             {
611 0           $ds->diag_add_set($digest, $outmap);
612            
613 0           foreach my $v ( @{$set->{value_list}} )
  0            
614             {
615 0           my $vr = $set->{value}{$v};
616            
617 0           push @show_list, $vr->{value};
618            
619 0 0         if ( $vr->{maps_to} )
620             {
621 0           push @block_list, $vr->{maps_to};
622 0           $ds->diag_add_block($digest, $vr->{maps_to});
623 0           $ds->diag_add_check($digest, 'block', $vr->{maps_to}, "node '$path': optional_output");
624             }
625             }
626             }
627             }
628            
629 0           $node->{show_list} = \@show_list;
630 0           $node->{block_list} = \@block_list;
631            
632 0 0         if ( my $rs_name = $node->{ruleset} )
    0          
633             {
634 0           $ds->diag_add_ruleset($digest, $rs_name);
635 0           $ds->diag_add_check($digest, 'ruleset', $rs_name, "node '$path'");
636             }
637            
638             elsif ( $node->{method} )
639             {
640 0           $ds->diag_add_error($digest, "node '$path'", "no ruleset defined for this node");
641             }
642             }
643              
644              
645             sub diag_list_value {
646            
647 0     0 0   my ($hash, $field) = @_;
648            
649 0 0 0       return unless defined $hash->{$field} && $hash->{$field} ne '';
650            
651 0 0         if ( ref $hash->{$field} eq 'ARRAY' )
652             {
653 0 0         return grep { defined $_ && $_ ne '' } @{$hash->{$field}};
  0            
  0            
654             }
655            
656             else
657             {
658 0           return $hash->{$field};
659             }
660             }
661              
662              
663             # diag_add_subnodes ( digest, path )
664             #
665             # Go through the list of nodes that are in the "node list" associated with the
666             # specified path, and add them to the digest. These are the nodes that are
667             # linked as "subnodes" of the specified path.
668              
669             sub diag_add_subnodes {
670            
671 0     0 0   my ($ds, $digest, $path) = @_;
672            
673             # Do some basic checks. Return without doing anything unless we have an
674             # actual path.
675            
676 0 0 0       return unless defined $path && $path ne '';
677            
678             # If there are any nodes listed as subnodes of this path, then add them.
679            
680 0           my @subnodes = $ds->get_nodelist($path);
681            
682 0           foreach my $n ( @subnodes )
683             {
684 0           my $subnode_path = $n->{path};
685 0           $ds->diag_add_node($digest, $subnode_path);
686 0           $ds->diag_add_check($digest, 'node', $subnode_path, "node '$path': subnode list");
687             }
688             }
689              
690              
691             sub diag_add_block {
692            
693 0     0 0   my ($ds, $digest, $name) = @_;
694            
695             # First do some basic checks. Return without doing anything if we weren't
696             # given an actual block name, or if we have already added this block.
697            
698 0 0 0       return unless defined $name && $name ne '';
699 0 0         return if $digest->{block}{$name};
700            
701             # Just as with nodes, fail gracefully if it doesn't exist.
702            
703 0 0         return unless ref $ds->{block}{$name};
704            
705             # Otherwise copy all of the block attributes into a new hash and add it to
706             # the specification.
707            
708 0           my $new = { %{$ds->{block}{$name}} };
  0            
709            
710 0           $digest->{block}{$name} = $new;
711            
712             # Blocks don't have any other named structures hanging off of them, so we
713             # can stop here.
714             }
715              
716              
717             sub diag_add_set {
718              
719 0     0 0   my ($ds, $digest, $name) = @_;
720            
721             # First do some basic checks. Return without doing anything if we weren't
722             # given an actual set name, or if we have already added this set.
723            
724 0 0 0       return unless defined $name && $name ne '';
725 0 0         return if $digest->{set}{$name};
726            
727             # Just as with nodes, fail gracefully if it doesn't exist.
728            
729 0 0         return unless ref $ds->{set}{$name};
730            
731             # Otherwise copy all of the set attributes into a new hash and add it to
732             # the specification.
733            
734 0           my $new = { %{$ds->{set}{$name}} };
  0            
735            
736 0           $digest->{set}{$name} = $new;
737            
738             # Sets don't have any other named structures hanging off of them, so we
739             # can stop here.
740             }
741              
742              
743             sub diag_add_ruleset {
744            
745 0     0 0   my ($ds, $digest, $name) = @_;
746            
747             # First do some basic checks. Return without doing anything if we weren't
748             # given an actual ruleset name, or if we have already added this ruleset.
749            
750 0 0 0       return unless defined $name && $name ne '';
751 0 0         return if $digest->{ruleset}{$name};
752            
753             # Just as with nodes, fail gracefully if it doesn't exist.
754            
755 0           my $rs_list = $ds->{ruleset_diag}{$name};
756            
757 0 0         return unless ref $rs_list eq 'ARRAY';
758            
759             # Otherwise add the ruleset list to the specification.
760            
761 0           $digest->{ruleset}{$name} = $rs_list;
762            
763             # Then go through the rules and add any referenced sets.
764            
765 0           foreach my $rule ( @$rs_list )
766             {
767 0 0         next unless ref $rule eq 'HASH';
768            
769             # First look at the 'valid' field. This might be an array, so look at
770             # each of the values in turn.
771            
772 0 0         my @valid = ref $rule->{valid} eq 'ARRAY' ? @{$rule->{valid}} : $rule->{valid};
  0            
773 0           my @new_valid;
774            
775 0           foreach my $v (@valid)
776             {
777             # If we find a reference, then we must use Perl internal voodoo to
778             # unpack it and figure out the name of whatever it refers to.
779            
780 0 0 0       if ( ref $v )
    0 0        
781             {
782 0           push @new_valid, &diag_decode_ref($v);
783             }
784            
785             # Otherwise, we assume that it is the name of a Set. So add this
786             # set to the digest unless it's already there.
787            
788             elsif ( $v && $v ne 'FLAG_VALUE' && $v ne 'ANY_VALUE' )
789             {
790 0           $ds->diag_add_set($digest, $v);
791 0           $ds->diag_add_check($digest, 'set', $v, "ruleset'$name'");
792 0           push @new_valid, $v;
793             }
794             }
795            
796             # Now copy this list back to 'valid'.
797            
798 0 0         if ( @new_valid == 1 )
    0          
799             {
800 0           $rule->{valid} = $new_valid[0];
801             }
802            
803             elsif ( @new_valid > 1 )
804             {
805 0           $rule->{valid} = \@new_valid;
806             }
807            
808             # If this rule is an inclusion rule, recursively include the target
809             # ruleset as well.
810            
811 0   0       my $inclusion = $rule->{allow} || $rule->{require};
812            
813 0 0 0       if ( $inclusion && ! ref $inclusion )
814             {
815 0           $ds->diag_add_ruleset($digest, $inclusion);
816 0           $ds->diag_add_check($digest, 'ruleset', $inclusion, "ruleset '$name'");
817             }
818             }
819             }
820              
821              
822             sub diag_decode_ref {
823            
824 0 0   0 0   return unless ref $_[0];
825            
826 0           my $obj = B::svref_2object($_[0]);
827            
828 0 0         return '*UNKNOWN*' unless $obj->can('GV');
829            
830 0           my $name = $obj->GV->NAME;
831 0           my $pkg = $obj->GV->STASH->NAME;
832 0 0         my $sigil = ref $_[0] eq 'CODE' ? '&'
    0          
    0          
833             : ref $_[0] eq 'HASH' ? '%'
834             : ref $_[0] eq 'ARRAY' ? '@'
835             : '?';
836            
837 0           return "${sigil}${pkg}::${name}";
838             }
839              
840              
841             sub diag_add_check {
842            
843 0     0 0   my ($ds, $digest, $type, $name, $key) = @_;
844            
845             # Check to see if a proper thingy exists under the proper name. If so,
846             # return true.
847            
848 0           my $hashkey = $type;
849 0 0         $hashkey = 'ruleset_diag' if $type eq 'ruleset';
850 0 0         $hashkey = 'node_attrs' if $type eq 'node';
851            
852 0 0         return 1 if ref $ds->{$hashkey}{$name};
853            
854             # Otherwise, add an error to the specification record and return false.
855            
856 0           $ds->diag_add_error($digest, $key, "unknown $type '$name'");
857 0           return 0;
858             }
859              
860              
861             sub diag_add_error {
862            
863 0     0 0   my ($ds, $digest, $key, $message) = @_;
864            
865 0 0 0       return unless defined $message && $message ne '';
866            
867 0   0       $key ||= 'unclassified';
868            
869 0           push @{$digest->{errors}{$key}}, $message;
  0            
870             }
871              
872              
873             1;