Skip to content

Commit

Permalink
Supporting also paths using double quotes #398
Browse files Browse the repository at this point in the history
  • Loading branch information
phochste committed Sep 15, 2023
1 parent b16716e commit fbb2076
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 27 deletions.
46 changes: 20 additions & 26 deletions lib/Catmandu/Path/simple.pm
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ sub _emit_get {
$path = [@$path];

my $key = shift @$path;
my $str_key = $self->_emit_string($key);
my $str_key = $self->_emit_string($self->unquote($key));
my $perl = "";

%opts = (up_var => my $up_var = $var);
Expand Down Expand Up @@ -242,12 +242,6 @@ sub _emit_get {
$perl .= $self->_emit_declare_vars($i, "\@{${up_var}} - 1");
$perl .= "my ${var} = ${up_var}->[${i}];";
}
elsif ($key eq "''") {
$opts{key} = $str_key;
$perl
.= "if (is_hash_ref(${up_var}) && exists(${up_var}->{''})) {";
$perl .= "my ${var} = ${up_var}->{''};";
}
else {
$opts{key} = $str_key;
$perl
Expand All @@ -267,7 +261,7 @@ sub _emit_set_key {
return "${var} = $val;" unless defined $key;

my $perl = "";
my $str_key = $self->_emit_string($key);
my $str_key = $self->_emit_string($self->unquote($key));

if (is_natural($key)) {
$perl .= "if (is_hash_ref(${var})) {";
Expand Down Expand Up @@ -303,11 +297,6 @@ sub _emit_set_key {
$perl .= "${var}->[${i}] = $val;";
$perl .= "}}";
}
elsif ($key eq "''") {
$perl .= "if (is_hash_ref(${var})) {";
$perl .= "${var}->{''} = $val;";
$perl .= "}";
}
else {
$perl .= "if (is_hash_ref(${var})) {";
$perl .= "${var}->{${str_key}} = $val;";
Expand All @@ -323,7 +312,7 @@ sub _emit_create_path {
@$path || return $cb->($var);

my $key = shift @$path;
my $str_key = $self->_emit_string($key);
my $str_key = $self->_emit_string($self->unquote($key));
my $perl = "";

if (is_natural($key)) {
Expand Down Expand Up @@ -385,13 +374,6 @@ sub _emit_create_path {
}
$perl .= "}";
}
elsif ($key eq "''") {
$perl .= "if (is_maybe_hash_ref(${var})) {";
$perl .= "my ${v} = ${var} //= {};";
$perl
.= $self->_emit_create_path("${v}->{''}", $path, $cb);
$perl .= "}";
}
else {
$perl .= "if (is_maybe_hash_ref(${var})) {";
$perl .= "my ${v} = ${var} //= {};";
Expand All @@ -407,7 +389,7 @@ sub _emit_create_path {
sub _emit_delete_key {
my ($self, $var, $key) = @_;

my $str_key = $self->_emit_string($key);
my $str_key = $self->_emit_string($self->unquote($key));
my $perl = "";

if (defined($key) && is_natural($key)) {
Expand All @@ -422,10 +404,6 @@ sub _emit_delete_key {
$perl .= "splice(\@{${var}}, \@{${var}} - 1, 1)" if $key eq '$last';
$perl .= "splice(\@{${var}}, 0, \@{${var}})" if $key eq '*';
}
elsif (defined($key) && $key eq "''") {
$perl .= "if (is_hash_ref(${var})) {";
$perl .= "delete(${var}->{''})";
}
else {
$perl .= "if (is_hash_ref(${var})) {";
$perl .= "delete(${var}->{${str_key}})";
Expand All @@ -436,6 +414,22 @@ sub _emit_delete_key {
$perl;
}

sub unquote {
my ($self, $str) = @_;
if (! defined $str) {
return $str;
}
elsif ($str =~ /^['](.*)[']$/) {
return $1;
}
elsif ($str =~ /^["](.*)["]$/) {
return $1;
}
else {
return $str;
}
}

1;

__END__
Expand Down
9 changes: 9 additions & 0 deletions t/Catmandu-Fix-add_field.t
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,13 @@ is_deeply $pkg->new('test')->fix({}), {test => undef}, "set key to undef";
is_deeply $pkg->new("''", 'empty')->fix({}), {'' => 'empty'},
"add an empty field";

is_deeply $pkg->new("'a'", 'test')->fix({}), {a => 'test'},
"add a single quoted field";

is_deeply $pkg->new("\"a\"", 'test')->fix({}), {a => 'test'},
"add a double quoted field";

is_deeply $pkg->new("\"a b c\"", 'test')->fix({}), {"a b c" => 'test'},
"add a double quoted field with spaces";

done_testing;
10 changes: 9 additions & 1 deletion t/Catmandu-Fix-remove_field.t
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,16 @@ is_deeply $pkg->new("''")
->fix({a => 'A', '' => 'Empty', c => 'C'}),
{a => 'A', c => 'C'}, 'remove empty';

is_deeply $pkg->new("\"\"")
->fix({a => 'A', '' => 'Empty', c => 'C'}),
{a => 'A', c => 'C'}, 'remove empty (double quotes)';

is_deeply $pkg->new("x.''")
->fix({ x => {a => 'A', '' => 'Empty', c => 'C'}}),
{x => {a => 'A', c => 'C'} }, 'remove nested empty';

done_testing 5;
is_deeply $pkg->new("\"x y z\"")
->fix({a => 'A', 'x y z' => 'Empty', c => 'C'}),
{a => 'A', c => 'C'}, 'remove keys with spaces';

done_testing 7;
9 changes: 9 additions & 0 deletions t/Catmandu-Fix-set_field.t
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,13 @@ is_deeply $pkg->new('test', '0123')->fix({test => 'ok'}), {test => '0123'},

is_deeply $pkg->new('test')->fix({}), {test => undef}, "set key to undef";

is_deeply $pkg->new("'a'", 'test')->fix({}), {a => 'test'},
"add a single quoted field";

is_deeply $pkg->new("\"a\"", 'test')->fix({}), {a => 'test'},
"add a double quoted field";

is_deeply $pkg->new("\"a b c\"", 'test')->fix({}), {"a b c" => 'test'},
"add a double quoted field with spaces";

done_testing;

0 comments on commit fbb2076

Please sign in to comment.