| 1 | package Jmespath::TreeInterpreter; | ||
| 2 | use parent 'Jmespath::Visitor'; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Try::Tiny; | ||
| 6 | use List::Util qw(unpairs); | ||
| 7 | use Scalar::Util qw(looks_like_number); | ||
| 8 | use JSON; | ||
| 9 | use Jmespath::Expression; | ||
| 10 | use Jmespath::Functions; | ||
| 11 | use Jmespath::AttributeException; | ||
| 12 | use Jmespath::IndexException; | ||
| 13 | use Jmespath::UnknownFunctionException; | ||
| 14 | |||
| 15 | |||
| 16 | my $COMPARATOR_FUNC = { 'le' => 'le', | ||
| 17 | 'ne' => 'ne', | ||
| 18 | 'lt' => 'lt', | ||
| 19 | 'lte' => 'lte', | ||
| 20 | 'eq' => 'eq', | ||
| 21 | 'gt' => 'gt', | ||
| 22 | 'gte' => 'gte' }; | ||
| 23 | |||
| 24 | my $MAP_TYPE = 'HASH'; | ||
| 25 | |||
| 26 | my $OPTIONS_DEFAULT = { hash_cls => undef, | ||
| 27 | custom_functions => undef }; | ||
| 28 | |||
| 29 | sub new { | ||
| 30 | my ($class, $options) = @_; | ||
| 31 | my $self = $class->SUPER::new($options); | ||
| 32 | if ( not defined $options) { $options = $OPTIONS_DEFAULT; } | ||
| 33 | $self->{_hash_cls} = $options->{ hash_cls } | ||
| 34 | if defined $options->{hash_cls}; | ||
| 35 | $self->{_functions} = eval { 'use ' . $options->{custom_functions} } | ||
| 36 | if defined $options->{custom_functions}; | ||
| 37 | |||
| 38 | return $self; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub visit { | ||
| 42 | my ($self, $node, $args) = @_; | ||
| 43 | my $node_type = $node->{type}; | ||
| 44 | try { | ||
| 45 | my $method = \&{'visit_' . $node->{type}}; | ||
| 46 | return &$method( $self, $node, $args ); | ||
| 47 | } catch { | ||
| 48 | $_->throw; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | sub default_visit { | ||
| 53 | my ($self, $node, @args) = @_; | ||
| 54 | Jmespath::NotImplementedException->new($node->{type})->throw; | ||
| 55 | } | ||
| 56 | |||
| 57 | =item visit_subexpression(node, value) | ||
| 58 | |||
| 59 | Drills down into a subexpression in the AST graph. | ||
| 60 | |||
| 61 | =cut | ||
| 62 | |||
| 63 | sub visit_subexpression { | ||
| 64 | my ($self, $node, $value) = @_; | ||
| 65 | my $result = $value; | ||
| 66 | foreach my $node (@{$node->{children}}) { | ||
| 67 | $result = $self->visit($node, $result); | ||
| 68 | } | ||
| 69 | return $result; | ||
| 70 | } | ||
| 71 | |||
| 72 | =item visit_field(node, value) | ||
| 73 | |||
| 74 | Returns the value of a field in the JSON data. | ||
| 75 | |||
| 76 | value : the JSON data | ||
| 77 | node : the AST node being evaluated. | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | sub visit_field { | ||
| 82 | my ($self, $node, $value) = @_; | ||
| 83 | try { | ||
| 84 | return $value->{$node->{value}}; | ||
| 85 | } catch { | ||
| 86 | # when the field cannot be looked up, then the spec defines the | ||
| 87 | # return value as undef. | ||
| 88 | return undef; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | sub visit_comparator { | ||
| 93 | my ($self, $node, $value) = @_; | ||
| 94 | |||
| 95 | my $comparator_func = \&{'jp_' . $node->{value}}; | ||
| 96 | if ( not defined &$comparator_func ) { | ||
| 97 | Jmespath::UnknownFunctionException | ||
| 98 | ->new({ message => 'unknown-function: Unknown function: ' . $comparator_func }) | ||
| 99 | ->throw; | ||
| 100 | } | ||
| 101 | |||
| 102 | return &$comparator_func( $self->visit( @{$node->{children}}[0], $value ), | ||
| 103 | $self->visit( @{$node->{children}}[1], $value ) ); | ||
| 104 | } | ||
| 105 | |||
| 106 | sub visit_current { | ||
| 107 | my ( $self, $node, $value ) = @_; | ||
| 108 | return $value; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub visit_expref { | ||
| 112 | my ( $self, $node, $value ) = @_; | ||
| 113 | return Jmespath::Expression->new($node->{children}[0], $self); | ||
| 114 | } | ||
| 115 | |||
| 116 | sub visit_function_expression { | ||
| 117 | my ($self, $node, $value) = @_; | ||
| 118 | my $function = \&{'jp_' . $node->{value}}; | ||
| 119 | if ( not defined &$function ) { | ||
| 120 | Jmespath::UnknownFunctionException | ||
| 121 | ->new({ message => 'unknown-function: Unknown function: ' . $function }) | ||
| 122 | ->throw; | ||
| 123 | } | ||
| 124 | |||
| 125 | my $resolved_args = []; | ||
| 126 | foreach my $child ( @{$node->{ children}} ) { | ||
| 127 | my $current = $self->visit($child, $value); | ||
| 128 | push @{$resolved_args}, $current; | ||
| 129 | } | ||
| 130 | |||
| 131 | return &$function(@$resolved_args); | ||
| 132 | } | ||
| 133 | |||
| 134 | sub visit_filter_projection { | ||
| 135 | my ($self, $node, $value) = @_; | ||
| 136 | my $base = $self->visit( @{$node->{children}}[0], $value); | ||
| 137 | return if ref($base) ne 'ARRAY'; | ||
| 138 | return if scalar @$base == 0; | ||
| 139 | |||
| 140 | my $comparator_node = @{ $node->{children} }[2]; | ||
| 141 | my $collected = []; | ||
| 142 | # use Data::Dumper; | ||
| 143 | foreach my $element (@$base) { | ||
| 144 | # print Dumper $comparator_node; | ||
| 145 | # print Dumper $element; | ||
| 146 | my $cnode_result = $self->visit($comparator_node, $element); | ||
| 147 | # print "cnode_result: $cnode_result\n"; | ||
| 148 | if ( $self->_is_true($cnode_result) ) { | ||
| 149 | my $current = $self->visit(@{$node->{children}}[1], $element); | ||
| 150 | if (defined $current) { | ||
| 151 | push @{$collected}, $current; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | } | ||
| 155 | return $collected; | ||
| 156 | } | ||
| 157 | |||
| 158 | sub visit_flatten { | ||
| 159 | my ($self, $node, $value) = @_; | ||
| 160 | my $base = $self->visit(@{$node->{'children'}}[0], $value); | ||
| 161 | |||
| 162 | return if ref($base) ne 'ARRAY'; | ||
| 163 | |||
| 164 | my $merged_list = []; | ||
| 165 | foreach my $element (@$base) { | ||
| 166 | if (ref($element) eq 'ARRAY') { | ||
| 167 | push @$merged_list, @$element; | ||
| 168 | } | ||
| 169 | else { | ||
| 170 | push @$merged_list, $element; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | return $merged_list; | ||
| 174 | } | ||
| 175 | |||
| 176 | sub visit_identity { | ||
| 177 | my ($self, $node, $value) = @_; | ||
| 178 | return if not defined $value; | ||
| 179 | # SHEER NEGATIVE ENERGY HACKERY - FORCE NUMBERS TO BE NUMBERS | ||
| 180 | # THANK YOU JSON.PM | ||
| 181 | $value = 1 * $value if $value =~ /^[-][0-9]+$/; | ||
| 182 | return $value; | ||
| 183 | } | ||
| 184 | |||
| 185 | sub visit_index { | ||
| 186 | my ($self, $node, $value) = @_; | ||
| 187 | return if ref($value) ne 'ARRAY'; | ||
| 188 | try { | ||
| 189 | return $value->[ $node->{value} ]; | ||
| 190 | } catch { | ||
| 191 | Jmespath::IndexException->new({ message => 'Invalid index' })->throw; | ||
| 192 | }; | ||
| 193 | } | ||
| 194 | |||
| 195 | sub visit_index_expression { | ||
| 196 | my ($self, $node, $value) = @_; | ||
| 197 | my $result = $value; | ||
| 198 | foreach my $node (@{$node->{children}}) { | ||
| 199 | $result = $self->visit($node, $result); | ||
| 200 | } | ||
| 201 | return $result; | ||
| 202 | } | ||
| 203 | |||
| 204 | |||
| 205 | # Rules: | ||
| 206 | # | ||
| 207 | |||
| 208 | sub visit_slice { | ||
| 209 | my ($self, $node, $value) = @_; | ||
| 210 | |||
| 211 | # Rule 08: If the element being sliced is an array and yields no | ||
| 212 | # results, the result MUST be an empty array. | ||
| 213 | my $selected = []; | ||
| 214 | my ($start, $stop); | ||
| 215 | |||
| 216 | # Rule 05: If the given step is omitted, it it assumed to be 1. | ||
| 217 | my $step = defined $node->{children}->[2] ? $node->{children}->[2] : 1; | ||
| 218 | |||
| 219 | # Rule 07: If the element being sliced is not an array, the result is null. | ||
| 220 | return if ref($value) ne 'ARRAY'; | ||
| 221 | |||
| 222 | # Rule 06: If the given step is 0, an error MUST be raised. | ||
| 223 | if ($step == 0) { | ||
| 224 | Jmespath::ValueException->new(message => 'Invalid slice expression')->throw; | ||
| 225 | } | ||
| 226 | if (scalar @{$node->{children}} > 3) { | ||
| 227 | Jmespath::ValueException->new(message => 'Invalid slice expression')->throw; | ||
| 228 | } | ||
| 229 | |||
| 230 | # Rule 02: If no start position is given, it is assumed to be 0 if | ||
| 231 | # the given step is greater than 0 or the end of the array | ||
| 232 | # if the given step is less than 0. | ||
| 233 | if (not defined $node->{children}->[0] and $step > 0) { | ||
| 234 | $start = 0; | ||
| 235 | } | ||
| 236 | elsif (not defined $node->{children}->[0] and $step < 0) { | ||
| 237 | $start = scalar(@$value); | ||
| 238 | } | ||
| 239 | elsif ( $node->{children}->[0] < 0) { | ||
| 240 | $start = scalar(@$value) + $node->{children}->[0]; | ||
| 241 | } | ||
| 242 | else { | ||
| 243 | $start = $node->{children}->[0]; | ||
| 244 | } | ||
| 245 | |||
| 246 | # Rule 01: If a negative start position is given, it is calculated as the | ||
| 247 | # total length of the array plus the given start position. | ||
| 248 | # if ($start < 0) { | ||
| 249 | # $start = scalar(@$value) + $start; | ||
| 250 | # } | ||
| 251 | |||
| 252 | # Rule 04: If no stop position is given, it is assumed to be the | ||
| 253 | # length of the array if the given step is greater than 0 | ||
| 254 | # or 0 if the given step is less than 0. | ||
| 255 | if (not defined $node->{children}->[1] and $step > 0) { | ||
| 256 | $stop = scalar(@$value); | ||
| 257 | } | ||
| 258 | elsif (not defined $node->{children}->[1] and $step < 0) { | ||
| 259 | $stop = -1; | ||
| 260 | } | ||
| 261 | # Rule 03: If a negative stop position is given, it is calculated as | ||
| 262 | # the total length of the array plus the given stop | ||
| 263 | # position. | ||
| 264 | elsif ($node->{children}->[1] < 0 and $step < 0) { | ||
| 265 | $stop = scalar(@$value) + $node->{children}->[1]; | ||
| 266 | } | ||
| 267 | elsif ($node->{children}->[1] < 0 and $step > 0) { | ||
| 268 | $stop = scalar(@$value) + $node->{children}->[1]; | ||
| 269 | } | ||
| 270 | else { | ||
| 271 | $stop = $node->{children}->[1]; | ||
| 272 | } | ||
| 273 | |||
| 274 | if ($step > 0) { | ||
| 275 | for ( my $idx = $start; $idx < $stop; $idx += $step ) { | ||
| 276 | push @$selected, @{$value}[$idx]; | ||
| 277 | last if $idx == scalar(@$value) - 1 | ||
| 278 | } | ||
| 279 | } | ||
| 280 | else { | ||
| 281 | for ( my $idx = $start; $idx > $stop; $idx += $step ) { | ||
| 282 | push @$selected, @{$value}[$idx]; | ||
| 283 | last if $idx == 0; | ||
| 284 | } | ||
| 285 | } | ||
| 286 | return $selected; | ||
| 287 | } | ||
| 288 | |||
| 289 | sub visit_key_val_pair { | ||
| 290 | my ($self, $node, $value) = @_; | ||
| 291 | return $self->visit(@{$node->{children}}[0], $value); | ||
| 292 | } | ||
| 293 | |||
| 294 | sub visit_literal { | ||
| 295 | my ($self, $node, $value) = @_; | ||
| 296 | return $node->{value}; | ||
| 297 | } | ||
| 298 | |||
| 299 | sub visit_multi_select_hash { | ||
| 300 | my ($self, $node, $value) = @_; | ||
| 301 | return if not defined $value; | ||
| 302 | my %merged; | ||
| 303 | foreach my $child (@{$node->{children}}) { | ||
| 304 | my $result = $self->visit($child, $value); | ||
| 305 | return if not defined $child->{value}; | ||
| 306 | %merged = (%merged,( $child->{value} , $result )); | ||
| 307 | } | ||
| 308 | return \%merged; | ||
| 309 | } | ||
| 310 | |||
| 311 | sub visit_multi_select_list { | ||
| 312 | my ($self, $node, $value) = @_; | ||
| 313 | return if not defined $value; | ||
| 314 | return if scalar @{$node->{children}} == 0; | ||
| 315 | |||
| 316 | my $collected = []; | ||
| 317 | foreach my $child ( @{$node->{children}}) { | ||
| 318 | my $result = $self->visit($child, $value); | ||
| 319 | my $value = defined $result ? $self->visit($child, $value) : undef; | ||
| 320 | push @$collected, $value; | ||
| 321 | } | ||
| 322 | return $collected; | ||
| 323 | } | ||
| 324 | |||
| 325 | sub visit_or_expression { | ||
| 326 | my ($self, $node, $value) = @_; | ||
| 327 | my $matched = $self->visit( @{$node->{children}}[0], $value ); | ||
| 328 | if ( $self->_is_false($matched)) { | ||
| 329 | $matched = $self->visit(@{$node->{children}}[1], $value); | ||
| 330 | } | ||
| 331 | return $matched; | ||
| 332 | } | ||
| 333 | |||
| 334 | sub visit_and_expression { | ||
| 335 | my ($self, $node, $value) = @_; | ||
| 336 | my $matched = $self->visit(@{$node->{children}}[0], $value); | ||
| 337 | # return if the left side eval is found to be false | ||
| 338 | return $matched if $self->_is_false($matched); | ||
| 339 | # if this isn't true then the whole evaluation is false | ||
| 340 | $matched = $self->visit(@{$node->{children}}[1], $value); | ||
| 341 | return $matched; | ||
| 342 | } | ||
| 343 | |||
| 344 | sub visit_not_expression { | ||
| 345 | my ($self, $node, $value) = @_; | ||
| 346 | my $original_result = $self->visit(@{$node->{children}}[0], $value); | ||
| 347 | return JSON::true if $self->_is_false($original_result) == 1; | ||
| 348 | return JSON::false; | ||
| 349 | } | ||
| 350 | |||
| 351 | sub visit_pipe { | ||
| 352 | my ($self, $node, $value) = @_; | ||
| 353 | my $result = $value; | ||
| 354 | foreach my $node ( @{$node->{children}}) { | ||
| 355 | $result = $self->visit($node, $result); | ||
| 356 | } | ||
| 357 | return $result; | ||
| 358 | } | ||
| 359 | |||
| 360 | sub visit_projection { | ||
| 361 | my ($self, $node, $value) = @_; | ||
| 362 | my $base = $self->visit(@{$node->{children}}[0], $value); | ||
| 363 | return if ref($base) ne 'ARRAY'; | ||
| 364 | |||
| 365 | my $collected = []; | ||
| 366 | foreach my $element (@$base) { | ||
| 367 | my $current = $self->visit(@{$node->{children}}[1], $element); | ||
| 368 | push (@$collected, $current) if defined $current; | ||
| 369 | } | ||
| 370 | return $collected; | ||
| 371 | } | ||
| 372 | |||
| 373 | sub visit_value_projection { | ||
| 374 | my ($self, $node, $value) = @_; | ||
| 375 | my $base = $self->visit(@{$node->{children}}[0], $value); | ||
| 376 | my @basekeys; | ||
| 377 | try { | ||
| 378 | @basekeys = map { $base->{ $_ } } sort keys %$base ; | ||
| 379 | } catch { | ||
| 380 | return; | ||
| 381 | }; | ||
| 382 | return if scalar @basekeys == 0; | ||
| 383 | my $collected = []; | ||
| 384 | foreach my $element (@basekeys) { | ||
| 385 | my $current = $self->visit(@{$node->{children}}[1], $element); | ||
| 386 | push( @$collected, $current ) if defined $current; | ||
| 387 | } | ||
| 388 | return $collected; | ||
| 389 | } | ||
| 390 | |||
| 391 | sub _is_false { | ||
| 392 | my ($self, $value) = @_; | ||
| 393 | return 1 if not defined $value; | ||
| 394 | # return 1 if JSON::is_bool($value) and $value == JSON::false; | ||
| 395 | # return 0 if JSON::is_bool($value) and $value == JSON::true; | ||
| 396 | return 1 if ref($value) eq 'JSON::PP::Boolean' and $value == JSON::false; | ||
| 397 | return 0 if ref($value) eq 'JSON::PP::Boolean' and $value == JSON::true; | ||
| 398 | return 1 if ref($value) eq 'ARRAY' and scalar @$value == 0; | ||
| 399 | return 1 if ref($value) eq 'HASH' and scalar keys %$value == 0; | ||
| 400 | return 1 if ref($value) eq 'SCALAR' and $value eq ''; | ||
| 401 | return 1 if $value eq ''; | ||
| 402 | return 0; | ||
| 403 | } | ||
| 404 | |||
| 405 | sub _is_true { | ||
| 406 | return ! shift->_is_false(shift); | ||
| 407 | } | ||
| 408 | |||
| 409 | 1; | ||
| 410 | |||
| 411 | __END__ | ||
| 412 | |||
| 413 | =head1 NAME | ||
| 414 | |||
| 415 | Jmespath::TreeInterpreter | ||
| 416 |