| 1 | package Elastic::Model::TypeMap::Structured; | ||||||||||||||
| 2 | 23 | $Elastic::Model::TypeMap::Structured::VERSION = '0.52'; | |||||||||||||
| 3 | 23 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 23 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 23 | ▶ | use Elastic::Model::TypeMap::Base qw(:all); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 23 | ▶ | use namespace::autoclean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | #=================================== | ||||||||||||||
| 10 | has_type 'MooseX::Meta::TypeConstraint::Structured', | ||||||||||||||
| 11 | #=================================== | ||||||||||||||
| 12 | 0 | ▶ | deflate_via { _structured( 'deflator', @_ ) }, | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 13 | 0 | ▶ | inflate_via { _structured( 'inflator', @_ ) }, | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 14 | 0 | ▶ | map_via { _structured( 'mapper', @_ ) }; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||
| 15 | |||||||||||||||
| 16 | #=================================== | ||||||||||||||
| 17 | has_type 'MooseX::Types::Structured::Optional', | ||||||||||||||
| 18 | #=================================== | ||||||||||||||
| 19 | 0 | ▶ | deflate_via { _content_handler( 'deflator', @_ ) }, | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 20 | 0 | ▶ | inflate_via { _content_handler( 'inflator', @_ ) }, | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 21 | 0 | ▶ | map_via { _content_handler( 'mapper', @_ ) }; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||
| 22 | |||||||||||||||
| 23 | #=================================== | ||||||||||||||
| 24 | has_type 'MooseX::Types::Structured::Tuple', | ||||||||||||||
| 25 | #=================================== | ||||||||||||||
| 26 | 0 | ▶ | deflate_via { _deflate_tuple(@_) }, # | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 27 | 0 | ▶ | inflate_via { _inflate_tuple(@_) }, # | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 28 | 0 | ▶ | map_via { _map_dict( _tuple_to_dict(shift), @_ ) }; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||
| 29 | |||||||||||||||
| 30 | #=================================== | ||||||||||||||
| 31 | has_type 'MooseX::Types::Structured::Dict', | ||||||||||||||
| 32 | #=================================== | ||||||||||||||
| 33 | 0 | ▶ | deflate_via { | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||
| 34 | 0 | ▶ | _flate_dict( 'deflator', { @{ shift->type_constraints || [] } }, @_ ); | ||||||||||||
|
Condition0%0/2
(shift())->type_constraints || []
MC/DC0%0/1
(shift())->type_constraints || []
(shift())->type_constraints
Truth table0%0/2
A
A= (shift())->type_constraints
| |||||||||||||||
| 35 | }, | ||||||||||||||
| 36 | |||||||||||||||
| 37 | 0 | ▶ | inflate_via { | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||
| 38 | 0 | ▶ | _flate_dict( 'inflator', { @{ shift->type_constraints || [] } }, @_ ); | ||||||||||||
|
Condition0%0/2
(shift())->type_constraints || []
MC/DC0%0/1
(shift())->type_constraints || []
(shift())->type_constraints
Truth table0%0/2
A
A= (shift())->type_constraints
| |||||||||||||||
| 39 | }, | ||||||||||||||
| 40 | |||||||||||||||
| 41 | 0 | ▶ | map_via { | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||
| 42 | 0 | ▶ | _map_dict( { @{ shift->type_constraints || [] } }, @_ ); | ||||||||||||
|
Condition0%0/2
(shift())->type_constraints || []
MC/DC0%0/1
(shift())->type_constraints || []
(shift())->type_constraints
Truth table0%0/2
A
A= (shift())->type_constraints
| |||||||||||||||
| 43 | 23 | }; | |||||||||||||
| 44 | |||||||||||||||
| 45 | #=================================== | ||||||||||||||
| 46 | has_type 'MooseX::Types::Structured::Map', | ||||||||||||||
| 47 | #=================================== | ||||||||||||||
| 48 | 0 | ▶ | deflate_via { _flate_map( 'deflator', @_ ) }, | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 49 | 0 | ▶ | inflate_via { _flate_map( 'inflator', @_ ) }, | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 50 | 0 | ▶ | map_via { type => 'object', enabled => 0 }; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||
| 51 | |||||||||||||||
| 52 | #=================================== | ||||||||||||||
| 53 | 0 | ▶ | sub _deflate_tuple { | ||||||||||||
|
Subroutine
_deflate_tuple: not called CC=2 SCAR=17.9
| |||||||||||||||
| 54 | #=================================== | ||||||||||||||
| 55 | 0 | my $dict = _tuple_to_dict(shift); | |||||||||||||
| 56 | 0 | ▶ | return '$val' unless %$dict; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 57 | |||||||||||||||
| 58 | 0 | my $deflator = _flate_dict( 'deflator', $dict, @_ ); | |||||||||||||
| 59 | |||||||||||||||
| 60 | 0 | ▶ | return sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 61 | 0 | my $array = shift; | |||||||||||||
| 62 | 0 | my %hash; | |||||||||||||
| 63 | 0 | @hash{ 0 .. $#{$array} } = @$array; | |||||||||||||
| 64 | 0 | $deflator->( \%hash ); | |||||||||||||
| 65 | 0 | }; | |||||||||||||
| 66 | } | ||||||||||||||
| 67 | |||||||||||||||
| 68 | #=================================== | ||||||||||||||
| 69 | 0 | ▶ | sub _inflate_tuple { | ||||||||||||
|
Subroutine
_inflate_tuple: not called CC=2 SCAR=17.9
| |||||||||||||||
| 70 | #=================================== | ||||||||||||||
| 71 | 0 | my $dict = _tuple_to_dict(shift); | |||||||||||||
| 72 | 0 | ▶ | return '$val' unless %$dict; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 73 | 0 | my $inflator = _flate_dict( 'inflator', $dict, @_ ); | |||||||||||||
| 74 | 0 | ▶ | sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 75 | 0 | my $hash = $inflator->(@_); | |||||||||||||
| 76 | 0 | [ @{$hash}{ 0 .. keys(%$hash) - 1 } ]; | |||||||||||||
| 77 | 0 | }; | |||||||||||||
| 78 | } | ||||||||||||||
| 79 | |||||||||||||||
| 80 | #=================================== | ||||||||||||||
| 81 | 0 | ▶ | sub _tuple_to_dict { | ||||||||||||
|
Subroutine
_tuple_to_dict: not called CC=2 SCAR=17.9
| |||||||||||||||
| 82 | #=================================== | ||||||||||||||
| 83 | 0 | my $i = 0; | |||||||||||||
| 84 | 0 | ▶ | return { map { $i++ => $_ } @{ shift->type_constraints || [] } }; | ||||||||||||
|
Condition0%0/2
(shift())->type_constraints || []
MC/DC0%0/1
(shift())->type_constraints || []
(shift())->type_constraints
Truth table0%0/2
A
A= (shift())->type_constraints
| |||||||||||||||
| 85 | } | ||||||||||||||
| 86 | |||||||||||||||
| 87 | #=================================== | ||||||||||||||
| 88 | 0 | ▶ | sub _flate_dict { | ||||||||||||
|
Subroutine
_flate_dict: not called CC=5 SCAR=34.0
| |||||||||||||||
| 89 | #=================================== | ||||||||||||||
| 90 | 0 | my ( $type, $dict, $attr, $map ) = @_; | |||||||||||||
| 91 | |||||||||||||||
| 92 | 0 | ▶ | return '$val' unless %$dict; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 93 | |||||||||||||||
| 94 | 0 | my %flators; | |||||||||||||
| 95 | |||||||||||||||
| 96 | 0 | for my $key ( keys %$dict ) { | |||||||||||||
| 97 | 0 | ▶ | my $flator = $map->find( $type, $dict->{$key}, $attr ) | ||||||||||||
|
Condition0%0/2
$map->find($type, $dict->{$key}, $attr) || die("No $type found for key ($key)")
MC/DC0%0/1
$map->find($type, $dict->{$key}, $attr) || die("No $type found for key ($key)")
$map->find($type, $dict->{$key}, $attr)
Truth table0%0/2
A
A= $map->find($type, $dict->{$key}, $attr)
| |||||||||||||||
| 98 | || die "No $type found for key ($key)"; | ||||||||||||||
| 99 | |||||||||||||||
| 100 | 0 | ▶ | $flators{$key} | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 101 | = ref $flator | ||||||||||||||
| 102 | ? $flator | ||||||||||||||
| 103 | : Eval::Closure::eval_closure( | ||||||||||||||
| 104 | source => [ 'sub { my $val = $_[0];', $flator, '}' ] ); | ||||||||||||||
| 105 | } | ||||||||||||||
| 106 | |||||||||||||||
| 107 | 0 | ▶ | sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 108 | 0 | my $hash = shift; | |||||||||||||
| 109 | 0 | +{ map { $_ => $flators{$_}->( $hash->{$_} ) } | |||||||||||||
| 110 | 0 | grep { exists $flators{$_} } keys %$hash | |||||||||||||
| 111 | }; | ||||||||||||||
| 112 | 0 | }; | |||||||||||||
| 113 | } | ||||||||||||||
| 114 | |||||||||||||||
| 115 | #=================================== | ||||||||||||||
| 116 | 0 | ▶ | sub _map_dict { | ||||||||||||
|
Subroutine
_map_dict: not called CC=4 SCAR=30.0
| |||||||||||||||
| 117 | #=================================== | ||||||||||||||
| 118 | 0 | my ( $tcs, $attr, $map ) = @_; | |||||||||||||
| 119 | |||||||||||||||
| 120 | 0 | ▶ | return ( type => 'object', enabled => 0 ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 121 | unless %$tcs; | ||||||||||||||
| 122 | |||||||||||||||
| 123 | 0 | my %properties; | |||||||||||||
| 124 | 0 | for ( keys %$tcs ) { | |||||||||||||
| 125 | 0 | my %key_mapping = $map->find( 'mapper', $tcs->{$_}, $attr ); | |||||||||||||
| 126 | 0 | ▶ | die "Couldn't find mapping for key $_" | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 127 | unless %key_mapping; | ||||||||||||||
| 128 | 0 | $properties{$_} = \%key_mapping; | |||||||||||||
| 129 | } | ||||||||||||||
| 130 | return ( | ||||||||||||||
| 131 | 0 | type => 'object', | |||||||||||||
| 132 | dynamic => 'strict', | ||||||||||||||
| 133 | properties => \%properties | ||||||||||||||
| 134 | ); | ||||||||||||||
| 135 | } | ||||||||||||||
| 136 | |||||||||||||||
| 137 | #=================================== | ||||||||||||||
| 138 | 0 | ▶ | sub _flate_map { | ||||||||||||
|
Subroutine
_flate_map: not called CC=5 SCAR=34.0
| |||||||||||||||
| 139 | #=================================== | ||||||||||||||
| 140 | 0 | my ( $type, $tc, $attr, $map ) = @_; | |||||||||||||
| 141 | |||||||||||||||
| 142 | 0 | ▶ | my $tcs = $tc->type_constraints || []; | ||||||||||||
|
Condition0%0/2
$tc->type_constraints || []
MC/DC0%0/1
$tc->type_constraints || []
$tc->type_constraints
Truth table0%0/2
A
A= $tc->type_constraints
| |||||||||||||||
| 143 | 0 | ▶ | my $content_tc = $tcs->[1] | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 144 | or return \&_pass_through; | ||||||||||||||
| 145 | |||||||||||||||
| 146 | 0 | ▶ | my $content = $map->find( $type, $content_tc, $attr ) or return; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 147 | |||||||||||||||
| 148 | 0 | ▶ | return sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 149 | 0 | my $hash = shift; | |||||||||||||
| 150 | 0 | +{ map { $_ => $content->( $hash->{$_} ) } keys %$hash }; | |||||||||||||
| 151 | } | ||||||||||||||
| 152 | 0 | ▶ | if ref $content; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 153 | |||||||||||||||
| 154 | 0 | 'do { ' | |||||||||||||
| 155 | . 'my $hash = $val; ' | ||||||||||||||
| 156 | . '+{map { ' | ||||||||||||||
| 157 | . 'my $key = $_; my $val = $hash->{$_}; ' | ||||||||||||||
| 158 | . '$key => ' | ||||||||||||||
| 159 | . $content | ||||||||||||||
| 160 | . '} keys %$hash}}'; | ||||||||||||||
| 161 | |||||||||||||||
| 162 | } | ||||||||||||||
| 163 | |||||||||||||||
| 164 | #=================================== | ||||||||||||||
| 165 | 0 | ▶ | sub _structured { | ||||||||||||
|
Subroutine
_structured: not called CC=2 SCAR=17.9
| |||||||||||||||
| 166 | #=================================== | ||||||||||||||
| 167 | 0 | my ( $type, $tc, $attr, $map ) = @_; | |||||||||||||
| 168 | 0 | my $types = $type . 's'; | |||||||||||||
| 169 | 0 | my $parent = $tc->parent; | |||||||||||||
| 170 | 0 | ▶ | if ( my $handler = $map->$types->{ $parent->name } ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 171 | 0 | return $handler->( $tc, $attr, $map ); | |||||||||||||
| 172 | } | ||||||||||||||
| 173 | 0 | $map->find( $type, $parent, $attr ); | |||||||||||||
| 174 | } | ||||||||||||||
| 175 | |||||||||||||||
| 176 | #=================================== | ||||||||||||||
| 177 | 0 | ▶ | sub _content_handler { | ||||||||||||
|
Subroutine
_content_handler: not called CC=3 SCAR=24.8
| |||||||||||||||
| 178 | #=================================== | ||||||||||||||
| 179 | 0 | my ( $type, $tc, $attr, $map ) = @_; | |||||||||||||
| 180 | 0 | ▶ | return $tc->can('type_parameter') | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 181 | ? $map->find( $type, $tc->type_parameter, $attr ) | ||||||||||||||
| 182 | : $type eq 'mapper' ? ( type => 'object', enabled => 0 ) | ||||||||||||||
| 183 | : '$val'; | ||||||||||||||
| 184 | } | ||||||||||||||
| 185 | |||||||||||||||
| 186 | #=================================== | ||||||||||||||
| 187 | 0 | ▶ | sub _pass_through { $_[0] } | ||||||||||||
|
Subroutine
_pass_through: not called CC=1 SCAR=6.9
| |||||||||||||||
| 188 | #=================================== | ||||||||||||||
| 189 | |||||||||||||||
| 190 | 23 | 1; | |||||||||||||
| 191 | |||||||||||||||
| 192 | =pod | ||||||||||||||
| 193 | |||||||||||||||
| 194 | =encoding UTF-8 | ||||||||||||||
| 195 | |||||||||||||||
| 196 | =head1 NAME | ||||||||||||||
| 197 | |||||||||||||||
| 198 | Elastic::Model::TypeMap::Structured - Type maps for MooseX::Types::Structured | ||||||||||||||
| 199 | |||||||||||||||
| 200 | =head1 VERSION | ||||||||||||||
| 201 | |||||||||||||||
| 202 | version 0.52 | ||||||||||||||
| 203 | |||||||||||||||
| 204 | =head1 DESCRIPTION | ||||||||||||||
| 205 | |||||||||||||||
| 206 | L<Elastic::Model::TypeMap::Structured> provides mapping, inflation and deflation | ||||||||||||||
| 207 | for the L<MooseX::Types::Structured> type constraints. | ||||||||||||||
| 208 | It is loaded automatically byL<Elastic::Model::TypeMap::Default>. | ||||||||||||||
| 209 | |||||||||||||||
| 210 | =head1 TYPES | ||||||||||||||
| 211 | |||||||||||||||
| 212 | =head2 Optional | ||||||||||||||
| 213 | |||||||||||||||
| 214 | Optional values are mapped, inflated and deflated according to their | ||||||||||||||
| 215 | content type, eg C<Optional[Int]>. An C<Optional> type with no | ||||||||||||||
| 216 | content type is mapped as C<<{ type => 'object', enabled => 'no' }>> | ||||||||||||||
| 217 | and the value would be passed through unaltered when deflating/inflating. | ||||||||||||||
| 218 | |||||||||||||||
| 219 | =head2 Tuple | ||||||||||||||
| 220 | |||||||||||||||
| 221 | Because array refs are interpreted by Elasticsearch as multiple values | ||||||||||||||
| 222 | of the same type, tuples are converted to hash refs whose keys are | ||||||||||||||
| 223 | the index number. For instance, a field C<foo> with C<Tuple[Int,Str]> | ||||||||||||||
| 224 | and value C<[5,'foo']> will be deflated to C<< { 0 => 5, 1 => 'foo' } >>. | ||||||||||||||
| 225 | |||||||||||||||
| 226 | A tuple is mapped as an object, with: | ||||||||||||||
| 227 | |||||||||||||||
| 228 | { | ||||||||||||||
| 229 | type => 'object', | ||||||||||||||
| 230 | dynamic => 'strict', | ||||||||||||||
| 231 | properties => \%properties | ||||||||||||||
| 232 | } | ||||||||||||||
| 233 | |||||||||||||||
| 234 | The C<%properties> mapping depends on the content types. A C<Tuple> without | ||||||||||||||
| 235 | content types is mapped as C<<{ type => 'object', enabled => 'no' }>> | ||||||||||||||
| 236 | and the value would be passed through unaltered when deflating/inflating. | ||||||||||||||
| 237 | |||||||||||||||
| 238 | =head2 Dict | ||||||||||||||
| 239 | |||||||||||||||
| 240 | A C<Dict> is mapped as an object, with: | ||||||||||||||
| 241 | |||||||||||||||
| 242 | { | ||||||||||||||
| 243 | type => 'object', | ||||||||||||||
| 244 | dynamic => 'strict', | ||||||||||||||
| 245 | properties => \%properties | ||||||||||||||
| 246 | } | ||||||||||||||
| 247 | |||||||||||||||
| 248 | The C<%properties> mapping depends on the content types. A C<Dict> without | ||||||||||||||
| 249 | content types is mapped as C<<{ type => 'object', enabled => 'no' }>> | ||||||||||||||
| 250 | and the value would be passed through unaltered when deflating/inflating. | ||||||||||||||
| 251 | |||||||||||||||
| 252 | =head2 Map | ||||||||||||||
| 253 | |||||||||||||||
| 254 | It is not advisable to allow arbitrary key names in indexed hashes, as you | ||||||||||||||
| 255 | could end up generating many (and conflicting) field mappings. For this reason, | ||||||||||||||
| 256 | Maps are mapped as C<< { type => 'object', enabled => 0 } >>. In/deflation | ||||||||||||||
| 257 | depends on the content type (eg C<Map[Str,Int>]). A C<Map> without a content type | ||||||||||||||
| 258 | would pass through the value unaltered when inflating/deflatin. | ||||||||||||||
| 259 | |||||||||||||||
| 260 | =head1 AUTHOR | ||||||||||||||
| 261 | |||||||||||||||
| 262 | Clinton Gormley <drtech@cpan.org> | ||||||||||||||
| 263 | |||||||||||||||
| 264 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 265 | |||||||||||||||
| 266 | This software is copyright (c) 2015 by Clinton Gormley. | ||||||||||||||
| 267 | |||||||||||||||
| 268 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||
| 269 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||
| 270 | |||||||||||||||
| 271 | =cut | ||||||||||||||
| 272 | |||||||||||||||
| 273 | __END__ | ||||||||||||||