File Coverage

blib/lib/Data/Sah/Terse.pm
Criterion Covered Total %
statement 39 40 97.5
branch 18 20 90.0
condition 7 9 77.7
subroutine 7 7 100.0
pod 1 1 100.0
total 72 77 93.5


line stmt bran cond sub pod time code
1             package Data::Sah::Terse;
2              
3             our $DATE = '2015-01-03'; # DATE
4             our $VERSION = '0.02'; # VERSION
5              
6 1     1   507 use 5.010001;
  1         2  
  1         30  
7 1     1   3 use strict;
  1         2  
  1         20  
8 1     1   3 use warnings;
  1         1  
  1         20  
9              
10 1     1   513 use Data::Sah::Normalize qw(normalize_schema);
  1         1561  
  1         503  
11              
12             require Exporter;
13             our @ISA = qw(Exporter);
14             our @EXPORT_OK = qw(terse_schema);
15              
16             # get type, this is a duplicate Data::Sah::Util::Type's get_type(). we can use
17             # it but that will currently pull Data-Sah as dependency.
18             sub _get_type {
19 28     28   27 my $sch = shift;
20              
21 28 100       60 if (ref($sch) eq 'ARRAY') {
22 14         19 $sch = $sch->[0];
23             }
24              
25 28 50 33     95 if (defined($sch) && !ref($sch)) {
26 28         63 $sch =~ s/\*\z//;
27 28         48 return $sch;
28             } else {
29 0         0 return undef;
30             }
31             }
32              
33             sub _get_cset {
34 15     15   15 my ($sch, $opts) = @_;
35              
36 15 100       22 if (ref($sch) eq 'ARRAY') {
37 12 50       35 my $schn = $opts->{schema_is_normalized} ? $sch :
38             normalize_schema($sch);
39 12         614 return $schn->[1];
40             } else {
41 3 100       9 if ($sch =~ /\*/) {
42 1         3 return {req=>1};
43             } else {
44 2         6 return {};
45             }
46             }
47             }
48              
49             sub terse_schema {
50 28     28 1 41 my ($sch, $opts) = @_;
51 28         40 my $type = _get_type($sch);
52 28 100 100     95 if ($type eq 'array') {
    100          
53 6         11 my $cset = _get_cset($sch);
54 6 100       22 return $type unless $cset->{of};
55 4         14 return $type . "[" . terse_schema($cset->{of}) . "]";
56             } elsif ($type eq 'any' || $type eq 'all') {
57 9         15 my $cset = _get_cset($sch);
58 9 100 100     29 return $type unless $cset->{of} && @{ $cset->{of} };
  7         29  
59 5 100       12 my $join = $type eq 'any' ? '|' : ' & ';
60 5         6 return join($join, map {terse_schema($_)} @{ $cset->{of} });
  8         11  
  5         9  
61             } else {
62 13         56 return $type;
63             }
64             }
65              
66             1;
67             # ABSTRACT: Make human-readable terse representation of Sah schema
68              
69             __END__