File Coverage

blib/lib/Couch/DB/Util.pm
Criterion Covered Total %
statement 30 30 100.0
branch 12 22 54.5
condition n/a
subroutine 12 12 100.0
pod 4 4 100.0
total 58 68 85.2


line stmt bran cond sub pod time code
1             # This code is part of Perl distribution Couch-DB version 0.201.
2             # The POD got stripped from this file by OODoc version 3.06.
3             # For contributors see file ChangeLog.
4              
5             # This software is copyright (c) 2024-2026 by Mark Overmeer.
6              
7             # This is free software; you can redistribute it and/or modify it under
8             # the same terms as the Perl 5 programming language system itself.
9             # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10              
11              
12             package Couch::DB::Util;{
13             our $VERSION = '0.201';
14             }
15              
16 14     14   4016832 use parent 'Exporter';
  14         3806  
  14         91  
17              
18 14     14   1041 use warnings;
  14         27  
  14         821  
19 14     14   107 use strict;
  14         23  
  14         337  
20              
21 14     14   7127 use Log::Report 'couch-db';
  14         1660299  
  14         95  
22 14     14   4380 use Data::Dumper ();
  14         25  
  14         373  
23 14     14   60 use Scalar::Util qw/blessed/;
  14         23  
  14         9253  
24              
25             our @EXPORT_OK = qw/flat pile apply_tree simplified/;
26             our %EXPORT_TAGS = ( all => \@EXPORT_OK );
27              
28             sub import
29 36     36   16566 { my $class = shift;
30 36         1357 $_->import for qw(strict warnings utf8 version);
31 36         11868 $class->export_to_level(1, undef, @_);
32             }
33              
34             #--------------------
35              
36 18 100   18 1 744841 sub flat(@) { grep defined, map +(ref eq 'ARRAY' ? @$_ : $_), @_ }
37              
38              
39 5     5 1 12 sub pile(@) { +[ flat @_ ] }
40              
41              
42             #XXX why can't I find a CPAN module which does this?
43              
44             sub apply_tree($$);
45             sub apply_tree($$)
46 24     24 1 1032 { my ($tree, $code) = @_;
47             ! ref $tree ? $code->($tree)
48             : ref $tree eq 'ARRAY' ? +[ map apply_tree($_, $code), @$tree ]
49 24 50       536 : ref $tree eq 'HASH' ? +{ map +($_ => apply_tree($tree->{$_}, $code)), keys %$tree }
    100          
    100          
    100          
50             : ref $tree eq 'CODE' ? "$tree"
51             : $code->($tree);
52             }
53              
54              
55             sub simplified($$)
56 4     4 1 6565 { my ($name, $data) = @_;
57              
58             my $v = apply_tree $data, sub ($) {
59 8     8   13 my $e = shift;
60 8 0       59 ! blessed $e ? $e
    0          
    0          
    0          
    50          
    100          
61             : $e->isa('DateTime') ? "DATETIME($e)"
62             : $e->isa('Couch::DB::Document') ? 'DOCUMENT('.$e->id.')'
63             : $e->isa('JSON::PP::Boolean') ? ($e ? 'BOOL(true)' : 'BOOL(false)')
64             : $e->isa('version') ? "VERSION($e)"
65             : 'OBJECT('.(ref $e).')';
66 4         53 };
67              
68 4         98 Data::Dumper->new([$v], [$name])->Indent(1)->Quotekeys(0)->Sortkeys(1)->Dump;
69             }
70              
71             1;