Skip to content

Commit

Permalink
Fixing #399
Browse files Browse the repository at this point in the history
  • Loading branch information
phochste committed Sep 29, 2024
1 parent 359e7f6 commit c94711e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Build.PL
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# This file was automatically generated by Dist::Zilla::Plugin::ModuleBuild v6.030.
# This file was automatically generated by Dist::Zilla::Plugin::ModuleBuild v6.032.
use strict;
use warnings;

Expand Down
4 changes: 2 additions & 2 deletions lib/Catmandu/Error.pm
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ use Catmandu::Sane;
our $VERSION = '1.2021';

use Moo::Role;
use Catmandu::Util qw(is_string);
use Catmandu::Util;
use namespace::clean;

has source => (is => 'rw', writer => 'set_source');

sub _source_log_message {
my $msg = "";
if (is_string(my $source = $_[0]->source)) {
if (Catmandu::Util::is_string(my $source = $_[0]->source)) {
$msg .= "\nSource:";
for (split(/\n/, $source)) {
$msg .= "\n\t$_";
Expand Down
45 changes: 43 additions & 2 deletions lib/Catmandu/Fix/Condition/in.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use Moo;
use Catmandu::Util::Path qw(as_path);
use namespace::clean;
use Catmandu::Fix::Has;
use Data::Compare;

has path1 => (fix_arg => 1);
has path2 => (fix_arg => 1);
Expand All @@ -24,13 +25,53 @@ sub _build_tester {
my $vals2 = $path2_getter->($data);
return 0 unless @$vals1 && @$vals2 && @$vals1 == @$vals2;
for (my $i = 0; $i < @$vals1; $i++) {
no if $] >= 5.018, warnings => 'experimental::smartmatch';
return 0 unless $vals1->[$i] ~~ $vals2->[$i];
return 0 unless in($vals1->[$i],$vals2->[$i]);
}
return 1;
}
}

sub in {
my ($a,$b) = @_;

return 1 if ( ! ( defined($a) && defined($b)));
return 0 if ( ! defined($a) || ! defined($b));

# scalar vs scalar
if (ref($a) eq "" && ref($b) eq "") {
return $a eq $b;
}
# scalar vs list
elsif (ref($a) eq "" && ref($b) eq "ARRAY") {
return scalar grep( { $_ eq $a } @$b);
}
# scalar vs hash
elsif (ref($a) eq "" && ref($b) eq "HASH") {
return exists $b->{$a};
}
# array vs array
elsif (ref($a) eq "ARRAY" && ref($b) eq "ARRAY") {
return Compare($a,$b);
}
# hash vs hash
elsif (ref($a) eq "HASH" && ref($b) eq "HASH") {
return Compare($a,$b);
}
# hash vs array
elsif (ref($a) eq "HASH" && ref($b) eq "ARRAY") {
my @h = %$a;
return Compare(\@h,$b);
}
# array vs hash
elsif (ref($a) eq "ARRAY" && ref($b) eq "HASH") {
my @h = %$b;
return Compare($a,@h);
}
else {
return 0;
}
}

1;

__END__
Expand Down

0 comments on commit c94711e

Please sign in to comment.