| 456 | // Parses all the non-service specific parameters |
| 457 | template <typename ParamType> |
| 458 | inline bool argumentsToParameter(const Napi::CallbackInfo &args, |
| 459 | ParamType ¶ms, |
| 460 | bool requires_multiple_coordinates) |
| 461 | { |
| 462 | Napi::HandleScope scope(args.Env()); |
| 463 | |
| 464 | if (args.Length() < 2) |
| 465 | { |
| 466 | ThrowTypeError(args.Env(), "Two arguments required"); |
| 467 | return false; |
| 468 | } |
| 469 | |
| 470 | if (!args[0].IsObject()) |
| 471 | { |
| 472 | ThrowTypeError(args.Env(), "First arg must be an object"); |
| 473 | return false; |
| 474 | } |
| 475 | |
| 476 | Napi::Object obj = args[0].As<Napi::Object>(); |
| 477 | |
| 478 | Napi::Value coordinates = obj.Get("coordinates"); |
| 479 | if (coordinates.IsEmpty()) |
| 480 | return false; |
| 481 | |
| 482 | if (coordinates.IsUndefined()) |
| 483 | { |
| 484 | ThrowError(args.Env(), "Must provide a coordinates property"); |
| 485 | return false; |
| 486 | } |
| 487 | else if (coordinates.IsArray()) |
| 488 | { |
| 489 | auto coordinates_array = coordinates.As<Napi::Array>(); |
| 490 | if (coordinates_array.Length() < 2 && requires_multiple_coordinates) |
| 491 | { |
| 492 | ThrowError(args.Env(), "At least two coordinates must be provided"); |
| 493 | return false; |
| 494 | } |
| 495 | else if (!requires_multiple_coordinates && coordinates_array.Length() != 1) |
| 496 | { |
| 497 | ThrowError(args.Env(), "Exactly one coordinate pair must be provided"); |
| 498 | return false; |
| 499 | } |
| 500 | auto maybe_coordinates = parseCoordinateArray(coordinates_array); |
| 501 | if (maybe_coordinates) |
| 502 | { |
| 503 | std::copy(maybe_coordinates->begin(), |
| 504 | maybe_coordinates->end(), |
| 505 | std::back_inserter(params->coordinates)); |
| 506 | } |
| 507 | else |
| 508 | { |
| 509 | return false; |
| 510 | } |
| 511 | } |
| 512 | else if (!coordinates.IsUndefined()) |
| 513 | { |
| 514 | BOOST_ASSERT(!coordinates.IsArray()); |
| 515 | ThrowError(args.Env(), "Coordinates must be an array of (lon/lat) pairs"); |
no test coverage detected