Name
QBit::Validator::PathManager - path manager. It's works with simple hash keys only.
$key =~ /^[0-9a-zA-Z_]+\z/
Package methods
new
create object QBit::Validator::PathManager
Example:
my $path_manager = QBit::Validator::PathManager->new();
root
returns root symbol (default: '/')
Example:
my $root = $path_manager->root();
delimiter
return delimiter (default: '/')
Example:
my $delimiter = $path_manager->delimiter();
concatenate
concatenate root path, delimiter and path part
Example:
my $root_path = '/key';
my $path = $path_manager->concatenate($root_path, $path_manager->delimiter, 'key2');
# /key/key2
hash_path
returns path for hash key (default: as it is). You can use this method for escape a hash key if needed.
Example:
my $hash_path = $path_manager->hash_path('field');
# 'field'
array_path
returns path for array index (default: as it is). You can use this method for escape a array index if needed.
Example:
my $array_path = $path_manager->array_path(0);
# 0
get_data_by_path
returns data by path. No difference between hash key and array index in path. Hash key must satisfy regex
/^[0-9a-zA-Z_]+\z/.
Path consists of:
root symbol - '/'
delimiter - '/'
current element - '.'
parent - '..'
Example:
my $data = {
key => 1,
key2 => [
2,
3,
],
};
$path_manager->get_data_by_path('/', $data); # $data
$path_manager->get_data_by_path('/key', $data); # 1
$path_manager->get_data_by_path('/key/.', $data); # 1
$path_manager->get_data_by_path('/key2', $data); # [2, 3]
$path_manager->get_data_by_path('/key2/0', $data); # 2
$path_manager->get_data_by_path('/key2/../key', $data); # 1
Your path manager
You can write path manager for your favoriet data manager (Data::DPath, JSON::Pointer, JSON::Path, etc)
Example:
package QBit::Validator::PathManager::Data::DPath;
use base qw(QBit::Validator::PathManager);
use Data::DPath qw(dpath);
use String::Escape qw(qqbackslash);
sub hash_path {qqbackslash($_[1])}
sub array_path {'[$_[1]]'}
sub get_data_by_path {my @result = dpath($_[1])->match($_[2]); return $result[0];}
1;