File: SimpleCompareTest.pm

package info (click to toggle)
libcatmandu-perl 1.2020-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,552 kB
  • sloc: perl: 16,933; makefile: 24; sh: 1
file content (143 lines) | stat: -rw-r--r-- 3,218 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package Catmandu::Fix::Condition::SimpleCompareTest;

use Catmandu::Sane;

our $VERSION = '1.2020';

use Moo::Role;
use namespace::clean;

with 'Catmandu::Fix::Condition';

requires 'path';
requires 'path2';
requires 'emit_test';

sub emit {
    my ($self, $fixer, $label) = @_;

    my $path = $fixer->split_path($self->path);
    my $key  = pop @$path;

    my $path2 = $fixer->split_path($self->path2);
    my $key2  = pop @$path2;

    my $pass_fixes = $self->pass_fixes;
    my $fail_fixes = $self->fail_fixes;

    my $fail_label;
    my $fail_block = $fixer->emit_block(
        sub {
            $fail_label = shift;
            $fixer->emit_fixes($fail_fixes);
        }
    );

    my $perl = "no if ($] >= 5.018), 'warnings' => 'experimental';";

    my $has_match_var = $fixer->generate_var;
    $perl .= $fixer->emit_declare_vars($has_match_var, '0');

    my $vals_1 = $fixer->generate_var;
    $perl .= $fixer->emit_declare_vars($vals_1, '{}');

    $perl .= $fixer->emit_walk_path(
        $fixer->var,
        $path,
        sub {
            my $var = shift;
            $fixer->emit_get_key(
                $var, $key,
                sub {
                    my $var  = shift;
                    my $perl = "${has_match_var} ||= 1;";
                    $perl .= "${vals_1} = ${var};";
                    $perl;
                }
            );
        }
    );

    my $vals_2 = $fixer->generate_var;
    $perl .= $fixer->emit_declare_vars($vals_2, '{}');

    $perl .= $fixer->emit_walk_path(
        $fixer->var,
        $path2,
        sub {
            my $var = shift;
            $fixer->emit_get_key(
                $var, $key2,
                sub {
                    my $var  = shift;
                    my $perl = "${has_match_var} ||= 1;";
                    $perl .= "${vals_2} = ${var};";
                    $perl;
                }
            );
        }
    );

    $perl .= "unless (" . $self->emit_test($vals_1, $vals_2, $fixer) . ") {";
    if (@$fail_fixes) {
        $perl .= "goto ${fail_label};";
    }
    else {
        $perl .= "last ${label};";
    }
    $perl .= "}";

    $perl .= "if (${has_match_var}) {";

    $perl .= $fixer->emit_fixes($pass_fixes);

    $perl .= "last ${label};";
    $perl .= "}";

    if (@$fail_fixes) {
        $perl .= $fail_block;
    }

    $perl;
}

1;

__END__

=pod

=head1 NAME

Catmandu::Fix::Condition::SimpleCompareTest - Base class to ease the construction of compare conditionals

=head1 SYNOPSIS

   package Catmandu::Fix::Condition::has_equal_type

   use Catmandu::Sane;
   use Moo;
   use Catmandu::Fix::Has;

   has path  => (fix_arg => 1);
   has path2 => (fix_arg => 1);

   with 'Catmandu::Fix::Condition::SimpleCompareTest';

   sub emit_test {
       my ($self, $var, $var2, $fixer) = @_;
       "is_value(${var}) && is_value(${var2}) && ref ${var} eq ref ${var2}";
   }

   1;

   # Now you can write in your fixes
   has_equal_type(my_field_1,my_field_2)  # True when my_field_1 and my_field_2 have
                                          # the same refence type (both scalas, arrays, hashes)

=head1 SEE ALSO

L<Catmandu::Fix::Condition::SimpleAllTest>,
L<Catmandu::Fix::Condition::SimpleAnyTest>

=cut