| 1 | package Jmespath::Functions; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use parent 'Exporter'; | ||
| 5 | use JSON; | ||
| 6 | use Try::Tiny; | ||
| 7 | use POSIX qw(ceil floor); | ||
| 8 | use Jmespath::Expression; | ||
| 9 | use Jmespath::ValueException; | ||
| 10 | use Jmespath::JMESPathTypeException; | ||
| 11 | use Jmespath::String; | ||
| 12 | use Scalar::Util qw(looks_like_number isdual blessed); | ||
| 13 | use v5.12; | ||
| 14 | |||
| 15 | our @EXPORT = qw( jp_abs | ||
| 16 | jp_avg | ||
| 17 | jp_contains | ||
| 18 | jp_ceil | ||
| 19 | jp_ends_with | ||
| 20 | jp_eq | ||
| 21 | jp_floor | ||
| 22 | jp_gt | ||
| 23 | jp_gte | ||
| 24 | jp_join | ||
| 25 | jp_keys | ||
| 26 | jp_length | ||
| 27 | jp_lt | ||
| 28 | jp_lte | ||
| 29 | jp_map | ||
| 30 | jp_max | ||
| 31 | jp_max_by | ||
| 32 | jp_merge | ||
| 33 | jp_min | ||
| 34 | jp_min_by | ||
| 35 | jp_ne | ||
| 36 | jp_not_null | ||
| 37 | jp_reverse | ||
| 38 | jp_sort | ||
| 39 | jp_sort_by | ||
| 40 | jp_starts_with | ||
| 41 | jp_sum | ||
| 42 | jp_to_array | ||
| 43 | jp_to_string | ||
| 44 | jp_to_number | ||
| 45 | jp_type | ||
| 46 | jp_values ); | ||
| 47 | |||
| 48 | # jp_abs | ||
| 49 | # | ||
| 50 | # Absolute value of provided value. Throws exception if value is not | ||
| 51 | # a signed integer. | ||
| 52 | sub jp_abs { | ||
| 53 | my @args = @_; | ||
| 54 | Jmespath::ValueException | ||
| 55 | ->new({ message => 'abs() requires one argument' }) | ||
| 56 | ->throw | ||
| 57 | if scalar @args < 1 or scalar @args > 1; | ||
| 58 | my $arg = shift @args; | ||
| 59 | Jmespath::ValueException | ||
| 60 | ->new({ message => 'contains() illegal boolean value' }) | ||
| 61 | ->throw | ||
| 62 | if JSON::is_bool($arg); | ||
| 63 | |||
| 64 | Jmespath::ValueException | ||
| 65 | ->new({ message => 'Not a number: [' . $arg . ']'}) | ||
| 66 | ->throw | ||
| 67 | if not looks_like_number($arg); | ||
| 68 | return abs($arg); | ||
| 69 | } | ||
| 70 | |||
| 71 | sub jp_avg { | ||
| 72 | my ($values) = @_; | ||
| 73 | Jmespath::ValueException->new({ message => 'Required argument not array ref' })->throw | ||
| 74 | if ref $values ne 'ARRAY'; | ||
| 75 | |||
| 76 | foreach (@$values) { | ||
| 77 | Jmespath::ValueException->new({ message => 'Not a number: ' . $_ })->throw | ||
| 78 | if not looks_like_number($_); | ||
| 79 | } | ||
| 80 | return jp_sum($values) / scalar(@$values); | ||
| 81 | } | ||
| 82 | |||
| 83 | |||
| 84 | sub jp_contains { | ||
| 85 | my ( $subject, $search ) = @_; | ||
| 86 | Jmespath::ValueException | ||
| 87 | ->new({ message => 'contains() illegal boolean value' }) | ||
| 88 | ->throw | ||
| 89 | if JSON::is_bool($subject); | ||
| 90 | if ( ref $subject eq 'ARRAY' ) { | ||
| 91 | foreach (@$subject) { | ||
| 92 | return JSON::true if ( $_ eq $search ); #must be exact string match | ||
| 93 | } | ||
| 94 | return JSON::false; | ||
| 95 | } | ||
| 96 | elsif ( ref $subject eq '' ) { # straight string | ||
| 97 | return JSON::true if $subject =~ /$search/; | ||
| 98 | } | ||
| 99 | return JSON::false; | ||
| 100 | } | ||
| 101 | |||
| 102 | |||
| 103 | sub jp_ceil { | ||
| 104 | my ($value) = @_; | ||
| 105 | Jmespath::ValueException | ||
| 106 | ->new({ message => 'ceil() requires one argument' }) | ||
| 107 | ->throw | ||
| 108 | if not defined $value; | ||
| 109 | Jmespath::ValueException | ||
| 110 | ->new({ message => 'ceil() requires one number' }) | ||
| 111 | ->throw | ||
| 112 | if not looks_like_number($value); | ||
| 113 | return ceil($value); | ||
| 114 | } | ||
| 115 | |||
| 116 | sub jp_ends_with { | ||
| 117 | my ( $subject, $prefix ) = @_; | ||
| 118 | Jmespath::ValueException | ||
| 119 | ->new({ message => 'ends_with() allows strings only' }) | ||
| 120 | ->throw | ||
| 121 | if looks_like_number($prefix); | ||
| 122 | return JSON::true if $subject =~ /$prefix$/; | ||
| 123 | return JSON::false; | ||
| 124 | } | ||
| 125 | |||
| 126 | sub jp_eq { | ||
| 127 | my ($left, $right) = @_; | ||
| 128 | return JSON::true if not defined $left and not defined $right; | ||
| 129 | return JSON::false if not defined $left or not defined $right; | ||
| 130 | |||
| 131 | # If $left or $right is a boolean, they both must be boolean | ||
| 132 | if (ref($left) eq 'JSON::PP::Boolean' or | ||
| 133 | ref($right) eq 'JSON::PP::Boolean') { | ||
| 134 | if (ref($left) eq 'JSON::PP::Boolean' and | ||
| 135 | ref($right) eq 'JSON::PP::Boolean' ) { | ||
| 136 | return JSON::true if $left == $right; | ||
| 137 | return JSON::false; | ||
| 138 | } | ||
| 139 | return JSON::false; | ||
| 140 | } | ||
| 141 | |||
| 142 | #If $left or $right is a HASH or ARRAY reference, they need to be | ||
| 143 | #compared because comparison of json objects must be handled. | ||
| 144 | if (ref($left) eq 'HASH' or ref($right) eq 'HASH') { | ||
| 145 | if (ref($left) eq 'HASH' and ref($right) eq 'HASH') { | ||
| 146 | return JSON::true if hashes_equal($left, $right); | ||
| 147 | } | ||
| 148 | return JSON::false; | ||
| 149 | } | ||
| 150 | |||
| 151 | if (ref($left) eq 'ARRAY' or ref($right) eq 'ARRAY') { | ||
| 152 | if (ref($left) eq 'ARRAY' and ref($right) eq 'ARRAY') { | ||
| 153 | return JSON::true if arrays_equal($left, $right); | ||
| 154 | } | ||
| 155 | return JSON::false; | ||
| 156 | } | ||
| 157 | |||
| 158 | if (looks_like_number($left) and | ||
| 159 | looks_like_number($right)) { | ||
| 160 | return JSON::true if $left == $right; | ||
| 161 | } | ||
| 162 | elsif (not looks_like_number($left) and | ||
| 163 | not looks_like_number($right)) { | ||
| 164 | return JSON::true if $left eq $right; | ||
| 165 | } | ||
| 166 | return JSON::false; | ||
| 167 | } | ||
| 168 | |||
| 169 | sub jp_floor { | ||
| 170 | my ($value) = @_; | ||
| 171 | Jmespath::ValueException | ||
| 172 | ->new({ message => 'floor() requires one argument' }) | ||
| 173 | ->throw | ||
| 174 | if not defined $value; | ||
| 175 | Jmespath::ValueException | ||
| 176 | ->new({ message => 'floor() requires one number' }) | ||
| 177 | ->throw | ||
| 178 | if not looks_like_number($value); | ||
| 179 | return floor($value); | ||
| 180 | } | ||
| 181 | |||
| 182 | |||
| 183 | sub jp_gt { | ||
| 184 | my ($left, $right) = @_; | ||
| 185 | # According to the JMESPath Specification, this function returns | ||
| 186 | # undef if the variants are not numbers. | ||
| 187 | return if not looks_like_number($left); | ||
| 188 | return if not looks_like_number($right); | ||
| 189 | return JSON::true if $left > $right; | ||
| 190 | return JSON::false; | ||
| 191 | } | ||
| 192 | |||
| 193 | sub jp_gte { | ||
| 194 | my ($left, $right) = @_; | ||
| 195 | # According to the JMESPath Specification, this function returns | ||
| 196 | # undef if the variants are not numbers. | ||
| 197 | return if not looks_like_number($left); | ||
| 198 | return if not looks_like_number($right); | ||
| 199 | return JSON::true if $left >= $right; | ||
| 200 | return JSON::false; | ||
| 201 | } | ||
| 202 | |||
| 203 | sub jp_lt { | ||
| 204 | my ($left, $right) = @_; | ||
| 205 | # According to the JMESPath Specification, this function returns | ||
| 206 | # undef if the variants are not numbers. | ||
| 207 | return if not looks_like_number($left); | ||
| 208 | return if not looks_like_number($right); | ||
| 209 | return JSON::true if $left < $right; | ||
| 210 | return JSON::false; | ||
| 211 | } | ||
| 212 | |||
| 213 | sub jp_lte { | ||
| 214 | my ($left, $right) = @_; | ||
| 215 | # According to the JMESPath Specification, this function returns | ||
| 216 | # undef if the variants are not numbers. | ||
| 217 | return if not looks_like_number($left); | ||
| 218 | return if not looks_like_number($right); | ||
| 219 | return JSON::true if $left <= $right; | ||
| 220 | return JSON::false; | ||
| 221 | } | ||
| 222 | |||
| 223 | sub jp_join { | ||
| 224 | my ( $glue, $array ) = @_; | ||
| 225 | Jmespath::ValueException | ||
| 226 | ->new({ message => 'Not an array: ' . $array }) | ||
| 227 | ->throw | ||
| 228 | if ref $array ne 'ARRAY'; | ||
| 229 | Jmespath::ValueException | ||
| 230 | ->new({ message => 'Glue not a string: ' . $glue }) | ||
| 231 | ->throw | ||
| 232 | if jp_type($glue) ne 'string'; | ||
| 233 | |||
| 234 | foreach (@$array) { | ||
| 235 | Jmespath::ValueException | ||
| 236 | ->new({message => "Cannot join " . jp_type($_) . " $_"}) | ||
| 237 | ->throw | ||
| 238 | if jp_type($_) ne 'string' and ref $_ ne 'Jmespath::String'; | ||
| 239 | } | ||
| 240 | return join ( $glue, @$array ); | ||
| 241 | } | ||
| 242 | |||
| 243 | |||
| 244 | sub jp_keys { | ||
| 245 | my ( $obj ) = @_; | ||
| 246 | Jmespath::ValueException | ||
| 247 | ->new({ message => 'array keys(object obj) argument illegal' }) | ||
| 248 | ->throw | ||
| 249 | if ref $obj ne 'HASH'; | ||
| 250 | my @objkeys = keys %$obj; | ||
| 251 | return \@objkeys; | ||
| 252 | } | ||
| 253 | |||
| 254 | sub jp_length { | ||
| 255 | my ( $subject ) = @_; | ||
| 256 | my ( $length ) = 0; | ||
| 257 | |||
| 258 | Jmespath::ValueException | ||
| 259 | ->new({ message => 'number length(string|array|object subject) argument illegal' }) | ||
| 260 | ->throw | ||
| 261 | if JSON::is_bool($subject) or looks_like_number($subject); | ||
| 262 | return scalar @$subject if ref $subject eq 'ARRAY'; | ||
| 263 | return scalar keys %$subject if ref $subject eq 'HASH'; | ||
| 264 | return length $subject; | ||
| 265 | } | ||
| 266 | |||
| 267 | sub jp_map { | ||
| 268 | my ($expr, $elements) = @_; | ||
| 269 | # return [] if ref $elements ne 'ARRAY'; | ||
| 270 | Jmespath::ValueException | ||
| 271 | ->new({ message => 'array[any] map(expression->any->any expr, array[any] elements) undefined elements' }) | ||
| 272 | ->throw | ||
| 273 | if not defined $elements; | ||
| 274 | my $result = []; | ||
| 275 | foreach my $element (@$elements) { | ||
| 276 | use Data::Dumper; | ||
| 277 | Jmespath::ValueException | ||
| 278 | ->new({ message => 'array[any] map(expression->any->any expr, array[any] elements) undefined elements' }) | ||
| 279 | ->throw | ||
| 280 | if not defined $element; | ||
| 281 | my $res = $expr->visit($expr->{expression}, $element); | ||
| 282 | push @$result, $res; | ||
| 283 | } | ||
| 284 | return $result; | ||
| 285 | } | ||
| 286 | |||
| 287 | # must be all numbers or strings in order to work | ||
| 288 | # perhaps consider List::Util max() | ||
| 289 | sub jp_max { | ||
| 290 | my ( $collection ) = @_; | ||
| 291 | Jmespath::ValueException->new({message=>'max(string|number array) argument not an array'})->throw | ||
| 292 | if ref $collection ne 'ARRAY'; | ||
| 293 | my ($current_type, $current_max); | ||
| 294 | foreach my $arg (@$collection) { | ||
| 295 | |||
| 296 | my $type = jp_type($arg); | ||
| 297 | $current_type = $type if not defined $current_type; | ||
| 298 | $current_max = $arg if not defined $current_max; | ||
| 299 | |||
| 300 | Jmespath::ValueException | ||
| 301 | ->new({message=>"max(string|number array) mixed types not allowed"}) | ||
| 302 | ->throw | ||
| 303 | if $type ne $current_type; | ||
| 304 | |||
| 305 | Jmespath::ValueException | ||
| 306 | ->new({message=>"max(string|number array) $type not allowed"}) | ||
| 307 | ->throw | ||
| 308 | if $type ne 'number' and $type ne 'string'; | ||
| 309 | |||
| 310 | if (looks_like_number($arg) and $arg > $current_max) { $current_max = $arg; } | ||
| 311 | if (not looks_like_number($arg) and $arg gt $current_max) { $current_max = $arg; } | ||
| 312 | $current_type = $type; | ||
| 313 | } | ||
| 314 | return $current_max; | ||
| 315 | } | ||
| 316 | |||
| 317 | sub jp_max_by { | ||
| 318 | my ($array, $expref) = @_; | ||
| 319 | my $values = {}; | ||
| 320 | foreach my $item (@$array) { | ||
| 321 | my $result = $expref->visit($expref->{expression}, $item); | ||
| 322 | Jmespath::ValueException | ||
| 323 | ->new({message=>"min(string|number array) mixed types not allowed"}) | ||
| 324 | ->throw | ||
| 325 | if not defined $result or JSON::is_bool($result); | ||
| 326 | $values->{ $result } = $item; | ||
| 327 | } | ||
| 328 | my @keyed_on = keys %$values; | ||
| 329 | return $values->{ jp_max( \@keyed_on ) }; | ||
| 330 | } | ||
| 331 | |||
| 332 | # this needs to be a comparison function based on the "type" that is | ||
| 333 | # being sorted so the correct min/max will be taken by type. | ||
| 334 | |||
| 335 | sub jp_keyed_max { | ||
| 336 | my ($array, $keyfunc) = @_; | ||
| 337 | return $array; | ||
| 338 | } | ||
| 339 | |||
| 340 | sub _create_key_func { | ||
| 341 | my ($expref, $allowed_types, $function_name) = @_; | ||
| 342 | my $keyfunc = sub { | ||
| 343 | my $result = $expref->visit($expref->{expression}, shift); | ||
| 344 | }; | ||
| 345 | return $keyfunc; | ||
| 346 | } | ||
| 347 | |||
| 348 | sub jp_merge { | ||
| 349 | my @objects = @_; | ||
| 350 | my $merged = {}; | ||
| 351 | foreach my $object (@objects) { | ||
| 352 | Jmespath::ValueException | ||
| 353 | ->new({message=>"object merge([object *argument, [, object $...]])"}) | ||
| 354 | ->throw | ||
| 355 | if ref $object ne 'HASH'; | ||
| 356 | foreach my $key (keys %$object) { | ||
| 357 | $merged->{$key} = $object->{$key}; | ||
| 358 | } | ||
| 359 | } | ||
| 360 | return $merged; | ||
| 361 | } | ||
| 362 | |||
| 363 | sub jp_min { | ||
| 364 | my ($collection) = @_; | ||
| 365 | Jmespath::ValueException->new({message=>'min(string|number array) argument not an array'})->throw | ||
| 366 | if ref $collection ne 'ARRAY'; | ||
| 367 | my ($current_type, $current_min); | ||
| 368 | foreach my $arg (@$collection) { | ||
| 369 | |||
| 370 | my $type = jp_type($arg); | ||
| 371 | $current_type = $type if not defined $current_type; | ||
| 372 | $current_min = $arg if not defined $current_min; | ||
| 373 | |||
| 374 | Jmespath::ValueException->new({message=>"min(string|number array) mixed types not allowed"})->throw | ||
| 375 | if $type ne $current_type; | ||
| 376 | |||
| 377 | Jmespath::ValueException->new({message=>"min(string|number array) $type not allowed"})->throw | ||
| 378 | if $type ne 'number' and $type ne 'string'; | ||
| 379 | |||
| 380 | if (looks_like_number($arg) and $arg < $current_min) { $current_min = $arg; } | ||
| 381 | if (not looks_like_number($arg) and $arg lt $current_min) { $current_min = $arg; } | ||
| 382 | $current_type = $type; | ||
| 383 | } | ||
| 384 | return $current_min; | ||
| 385 | } | ||
| 386 | |||
| 387 | sub jp_min_by { | ||
| 388 | my ($array, $expref) = @_; | ||
| 389 | my $values = {}; | ||
| 390 | foreach my $item (@$array) { | ||
| 391 | my $result = $expref->visit($expref->{expression}, $item); | ||
| 392 | Jmespath::ValueException | ||
| 393 | ->new({message=>"min(string|number array) mixed types not allowed"}) | ||
| 394 | ->throw | ||
| 395 | if not defined $result or JSON::is_bool($result); | ||
| 396 | $values->{ $result } = $item; | ||
| 397 | } | ||
| 398 | my @keyed_on = keys %$values; | ||
| 399 | return $values->{ jp_min( \@keyed_on ) }; | ||
| 400 | } | ||
| 401 | |||
| 402 | sub jp_ne { | ||
| 403 | my ($left, $right) = @_; | ||
| 404 | return JSON::false if not defined $left and not defined $right; | ||
| 405 | return JSON::true if not defined $left or not defined $right; | ||
| 406 | |||
| 407 | if (ref($left) eq 'JSON::PP::Boolean' or | ||
| 408 | ref($right) eq 'JSON::PP::Boolean') { | ||
| 409 | if (ref($left) eq 'JSON::PP::Boolean' and | ||
| 410 | ref($right) eq 'JSON::PP::Boolean' ) { | ||
| 411 | return JSON::true if $left != $right; | ||
| 412 | return JSON::false; | ||
| 413 | } | ||
| 414 | # else { | ||
| 415 | # return JSON::false; | ||
| 416 | # } | ||
| 417 | return JSON::true; | ||
| 418 | } | ||
| 419 | |||
| 420 | if (ref($left) eq 'HASH' or ref($right) eq 'HASH') { | ||
| 421 | if (ref($left) eq 'HASH' and ref($right) eq 'HASH') { | ||
| 422 | return JSON::false if hashes_equal($left, $right); | ||
| 423 | } | ||
| 424 | return JSON::true; | ||
| 425 | } | ||
| 426 | |||
| 427 | if (ref($left) eq 'ARRAY' or ref($right) eq 'ARRAY') { | ||
| 428 | if (ref($left) eq 'ARRAY' and ref($right) eq 'ARRAY') { | ||
| 429 | return JSON::false if arrays_equal($left, $right); | ||
| 430 | } | ||
| 431 | return JSON::true; | ||
| 432 | } | ||
| 433 | |||
| 434 | |||
| 435 | if (looks_like_number($left) and | ||
| 436 | looks_like_number($right)) { | ||
| 437 | return JSON::true if $left != $right; | ||
| 438 | return JSON::false; | ||
| 439 | } | ||
| 440 | |||
| 441 | return JSON::false; | ||
| 442 | } | ||
| 443 | |||
| 444 | # | ||
| 445 | # | ||
| 446 | sub jp_not_null { | ||
| 447 | my @arguments = @_; | ||
| 448 | Jmespath::ValueException | ||
| 449 | ->new({ message => 'not_null() requires at least one argument' }) | ||
| 450 | ->throw | ||
| 451 | if not @arguments; | ||
| 452 | my $result = []; | ||
| 453 | foreach my $argument (@arguments) { | ||
| 454 | next if not defined $argument; | ||
| 455 | return $argument if defined $argument; | ||
| 456 | } | ||
| 457 | return; | ||
| 458 | } | ||
| 459 | |||
| 460 | sub jp_reverse { | ||
| 461 | my $arg = shift; | ||
| 462 | |||
| 463 | if (ref $arg eq 'ARRAY') { | ||
| 464 | my $result = []; | ||
| 465 | for ( my $idx = scalar @$arg - 1; $idx >= 0; $idx--) { | ||
| 466 | push @$result, @$arg[$idx]; | ||
| 467 | } | ||
| 468 | return $result; | ||
| 469 | } | ||
| 470 | elsif (ref $arg eq '') { | ||
| 471 | return reverse $arg; | ||
| 472 | } | ||
| 473 | return; | ||
| 474 | } | ||
| 475 | |||
| 476 | sub jp_sort { | ||
| 477 | my $list = shift; | ||
| 478 | Jmespath::ValueException | ||
| 479 | ->new({ message=>'array sort(array[number]|array[string] $list) illegal argument' }) | ||
| 480 | ->throw | ||
| 481 | if not defined $list or ref $list ne 'ARRAY'; | ||
| 482 | my $current_type; | ||
| 483 | foreach (@$list) { | ||
| 484 | Jmespath::ValueException | ||
| 485 | ->new({ message=>'array sort(array[number]|array[string] $list) illegal argument' }) | ||
| 486 | ->throw | ||
| 487 | if ref $_ ne ''; | ||
| 488 | $current_type = jp_type($_) if not defined $current_type; | ||
| 489 | Jmespath::ValueException | ||
| 490 | ->new({ message=>'array sort(array[number]|array[string] $list) illegal argument' }) | ||
| 491 | ->throw | ||
| 492 | if $current_type ne 'string' and $current_type ne 'number'; | ||
| 493 | Jmespath::ValueException | ||
| 494 | ->new({ message=>'array sort(array[number]|array[string] $list) mixed types' }) | ||
| 495 | ->throw | ||
| 496 | if $current_type ne jp_type($_); | ||
| 497 | } | ||
| 498 | |||
| 499 | my @result = sort { $a cmp $b } @$list ; | ||
| 500 | return \@result; | ||
| 501 | } | ||
| 502 | |||
| 503 | sub jp_sort_by { | ||
| 504 | my ($array, $expref) = @_; | ||
| 505 | my $values = {}; | ||
| 506 | my $keyed = []; | ||
| 507 | my $current_type; | ||
| 508 | # create "symbol map" for items | ||
| 509 | Jmespath::ValueException | ||
| 510 | ->new({message=>"sort_by(array elements, expression->number|expression->string expr) undefined expr not allowed"}) | ||
| 511 | ->throw | ||
| 512 | if not defined $expref; | ||
| 513 | for (my $idx = 0; $idx < scalar @$array; $idx++) { | ||
| 514 | $values->{$idx} = @{$array}[$idx]; | ||
| 515 | my $evaled = $expref->visit($expref->{expression}, @{$array}[$idx]); | ||
| 516 | $current_type = jp_type($evaled) if not defined $current_type; | ||
| 517 | |||
| 518 | Jmespath::ValueException | ||
| 519 | ->new({message=>"sort_by(array elements, expression->number|expression->string expr) undefined expr not allowed"}) | ||
| 520 | ->throw | ||
| 521 | if jp_type($evaled) ne $current_type; | ||
| 522 | Jmespath::ValueException | ||
| 523 | ->new({message=>"min(string|number array) mixed types not allowed"}) | ||
| 524 | ->throw | ||
| 525 | if not defined $evaled or JSON::is_bool($evaled); | ||
| 526 | push @$keyed, [ $evaled, $idx ]; | ||
| 527 | } | ||
| 528 | my @sorted = sort { $a->[0] cmp $b->[0] } @$keyed; | ||
| 529 | my $res = []; | ||
| 530 | foreach my $item (@sorted) { | ||
| 531 | push @$res, $values->{$item->[1]}; | ||
| 532 | } | ||
| 533 | return $res; | ||
| 534 | } | ||
| 535 | |||
| 536 | sub jp_starts_with { | ||
| 537 | my ($subject, $prefix) = @_; | ||
| 538 | Jmespath::ValueException->new({message=>'starts_with(subject, prefix) requires two arguments'})->throw | ||
| 539 | if not defined $subject or not defined $prefix; | ||
| 540 | Jmespath::ValueException->new({message=>'starts_with(subject, prefix) not a string'})->throw | ||
| 541 | if looks_like_number($prefix); | ||
| 542 | return JSON::true if $subject =~ /^$prefix/; | ||
| 543 | return JSON::false; | ||
| 544 | } | ||
| 545 | |||
| 546 | sub jp_sum { | ||
| 547 | my $data = shift; | ||
| 548 | my $result = 0; | ||
| 549 | foreach my $value (@$data) { | ||
| 550 | Jmespath::ValueException | ||
| 551 | ->new({message=>'sum(numbers) member not a number'}) | ||
| 552 | ->throw | ||
| 553 | if not looks_like_number($value); | ||
| 554 | $result += $value; | ||
| 555 | } | ||
| 556 | return $result; | ||
| 557 | } | ||
| 558 | |||
| 559 | sub jp_to_array { | ||
| 560 | my ($arg) = shift; | ||
| 561 | return [$arg] if JSON::is_bool($arg); | ||
| 562 | return [$arg] if ref $arg eq 'HASH'; | ||
| 563 | return $arg if ref $arg eq 'ARRAY'; | ||
| 564 | return [$arg]; | ||
| 565 | } | ||
| 566 | |||
| 567 | sub jp_to_string { | ||
| 568 | my ($arg) = @_; | ||
| 569 | $arg = JSON->new->pretty(0)->allow_nonref->encode( $arg ) | ||
| 570 | if jp_type($arg) eq 'object' | ||
| 571 | or jp_type($arg) eq 'array'; | ||
| 572 | $arg = do { bless \( my $dummy = $arg), "Jmespath::String" } | ||
| 573 | if jp_type($arg) eq 'number'; | ||
| 574 | return $arg; | ||
| 575 | } | ||
| 576 | |||
| 577 | sub jp_to_number { | ||
| 578 | my ($arg) = @_; | ||
| 579 | |||
| 580 | return if not looks_like_number($arg); | ||
| 581 | return if JSON::is_bool($arg); | ||
| 582 | $arg += 0; # remove trailing 0's | ||
| 583 | return $arg; | ||
| 584 | } | ||
| 585 | |||
| 586 | sub jp_type { | ||
| 587 | my ($item) = @_; | ||
| 588 | return 'null' if not defined $item; | ||
| 589 | if (JSON::is_bool($item)) { return 'boolean'; } | ||
| 590 | if (looks_like_number($item)) { | ||
| 591 | return 'string' if ref $item eq 'Jmespath::String'; | ||
| 592 | return 'number'; | ||
| 593 | } | ||
| 594 | if (ref($item) eq 'ARRAY') { return 'array';} | ||
| 595 | if (ref($item) eq 'HASH' ) {return 'object';} | ||
| 596 | return 'string'; | ||
| 597 | } | ||
| 598 | |||
| 599 | sub jp_values { | ||
| 600 | my $obj = shift; | ||
| 601 | Jmespath::ValueException | ||
| 602 | ->new({message =>'array values(object $obj): illegal argument'}) | ||
| 603 | ->throw | ||
| 604 | if ref $obj ne 'HASH'; | ||
| 605 | my $result = []; | ||
| 606 | foreach my $item (keys %$obj) { | ||
| 607 | push @$result, $obj->{$item}; | ||
| 608 | } | ||
| 609 | return $result; | ||
| 610 | } | ||
| 611 | |||
| 612 | |||
| 613 | sub arrays_equal { | ||
| 614 | my ( $a, $b ) = @_; | ||
| 615 | if ( scalar @$a != scalar @$b ) { | ||
| 616 | return 0; | ||
| 617 | } | ||
| 618 | for my $i ( 0 .. $#{$a} ) { | ||
| 619 | my $va = $a->[$i]; | ||
| 620 | my $vb = $b->[$i]; | ||
| 621 | if ( ref $va ne ref $vb ) { | ||
| 622 | return 0; | ||
| 623 | } | ||
| 624 | elsif ( ref $va eq '' and ref $vb eq '' and $va ne $vb) { | ||
| 625 | return 0; | ||
| 626 | } | ||
| 627 | elsif ( ref $va eq 'SCALAR' && $va ne $vb ) { | ||
| 628 | return 0; | ||
| 629 | } | ||
| 630 | elsif ( ref $va eq 'ARRAY' && !arrays_equal( $va, $vb ) ) { | ||
| 631 | return 0; | ||
| 632 | } | ||
| 633 | elsif ( ref $va eq 'HASH' && !hashes_equal( $va, $vb ) ) { | ||
| 634 | return 0; | ||
| 635 | } | ||
| 636 | } | ||
| 637 | return 1; | ||
| 638 | } | ||
| 639 | |||
| 640 | sub hashes_equal { | ||
| 641 | my ( $a, $b ) = @_; | ||
| 642 | if ( scalar( keys %$a ) != scalar( keys %$b ) ) { | ||
| 643 | return 0; | ||
| 644 | } | ||
| 645 | for my $k ( keys %$a ) { | ||
| 646 | if ( exists $b->{$k} ) { | ||
| 647 | my $va = $a->{$k}; | ||
| 648 | my $vb = $b->{$k}; | ||
| 649 | if ( ref $va ne ref $vb ) { | ||
| 650 | return 0; | ||
| 651 | } | ||
| 652 | elsif ( ref $va eq '' and ref $vb eq '' and $va ne $vb ) { | ||
| 653 | return 0; | ||
| 654 | } | ||
| 655 | elsif ( ref $va eq 'SCALAR' && $va ne $vb ) { | ||
| 656 | return 0; | ||
| 657 | } | ||
| 658 | elsif ( ref $va eq 'ARRAY' && !arrays_equal( $va, $vb ) ) { | ||
| 659 | return 0; | ||
| 660 | } | ||
| 661 | elsif ( ref $va eq 'HASH' && !hashes_equal( $va, $vb ) ) { | ||
| 662 | return 0; | ||
| 663 | } | ||
| 664 | } | ||
| 665 | else { | ||
| 666 | return 0; | ||
| 667 | } | ||
| 668 | } | ||
| 669 | return 1; | ||
| 670 | } | ||
| 671 | |||
| 672 | 1; | ||
| 673 | |||
| 674 | __END__ | ||
| 675 | |||
| 676 | =head1 NAME | ||
| 677 | |||
| 678 | Functions.pm : JMESPath Built-In Functions | ||
| 679 | |||
| 680 | =head1 EXPORTED FUNCTIONS | ||
| 681 | |||
| 682 | |||
| 683 | =head2 jp_map($expr, $elements) | ||
| 684 | |||
| 685 | Implements the L<JMESPath Built-In | ||
| 686 | Function|http://jmespath.org/specification.html#built-in-functions> | ||
| 687 | L<map()|http://jmespath.org/specification.html#map> | ||
| 688 |