NAME
Data::Transform::Position - a compound transformer that specifies one or more locations within the data structure to apply to
SYNOPSIS
Data::Transform::Position->new(
position => '/*/author',
transformer => Data::Transform::Type->new(
type => 'Result::Person',
handler => sub ($data) {
sprintf("%s, %s", $data->lastname, $data->firstname)
}
)
); # applies to any 2nd-level hash key "author", but only if that value's
# type is 'Result::Person', and then performs a custom stringification
Data::Transform::Position->new(
position => '/book/author',
transformer => Data::Transform::Default->new(
handler => sub ($data ) {
{
firstname => $data->names->{first} // '',
lastname => $data->names->{last} // '',
}
}
)
); # applies only to the node at $data->{book}->{author}, and tries to
# hash-ify the value there, regardless of its type.
DESCRIPTION
Data::Transform::Position
is a compound transformer, meaning that it both is, and has, a transformer. The transformer you give it at construction can be of any type, with its own handler and match criteria. The Data::Transform::Position
's handler will become the one from the supplied transformer, so handler
should not be specified when creating this transformer.
This construction is used so that transformers can be treated like building blocks and in some cases inserted to apply to the entire tree, but in other scenarios, used much more specifically.
FIELDS
position (required parameter)
Ex. "/book/author"
, "/*/*/title"
, "/values/0/id"
A position specifier for a location in the data structure. The forward slash character is used to delineate levels, which are hashes or arrays. The asterisk character can be used to represent "anything" at that level, whether hash key or array index.
Cam be an arrayref of position specifiers to match any of them.
transformer (required parameter)
A Data::Transform
transformer conforming to the Data::Transform::Node
role. Weird things will happen if you provide a Data::Transform::Tree
-type transformer, so you probably shouldn't do that.
applies_to( %params )
$params{position}
must exist, as well as any params required by the supplied transformer.
Passes %params
to the instance's transformer's applies_to
method - if that results in $NO_MATCH
, then that value is returned by this method.
Then, the position(s)
is/are checked against $params{position}
. If any matches exactly, returns $MATCH_EXACT_POSITION
. Otherwise, if any matches with wildcard evaluation, returns $MATCH_WILDCARD_POSITION
.
If no positions match, returns $NO_MATCH
AUTHOR
Mark Tyrrell <mtyrrell@cpan.org>
LICENSE
Copyright (c) 2023 Mark Tyrrell
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.