From 71c1a1d8f4d5b482f371804b852546ba9f8911c7 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Thu, 26 Sep 2024 16:33:35 -0400 Subject: [PATCH] LibWeb: Add a few more helpers to extract WebDriver properties --- .../Libraries/LibWeb/WebDriver/Properties.h | 54 ++++++++++++++++--- 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/Userland/Libraries/LibWeb/WebDriver/Properties.h b/Userland/Libraries/LibWeb/WebDriver/Properties.h index 4f25c1851dc..54062330a08 100644 --- a/Userland/Libraries/LibWeb/WebDriver/Properties.h +++ b/Userland/Libraries/LibWeb/WebDriver/Properties.h @@ -15,12 +15,9 @@ namespace Web::WebDriver { template -static ErrorOr get_property(JsonValue const& payload, StringView key) +static ErrorOr get_property(JsonObject const& payload, StringView key) { - if (!payload.is_object()) - return WebDriver::Error::from_code(ErrorCode::InvalidArgument, "Payload is not a JSON object"); - - auto property = payload.as_object().get(key); + auto property = payload.get(key); if (!property.has_value()) return WebDriver::Error::from_code(ErrorCode::InvalidArgument, ByteString::formatted("No property called '{}' present", key)); @@ -33,9 +30,13 @@ static ErrorOr get_property(JsonValue const& pay if (!property->is_bool()) return WebDriver::Error::from_code(ErrorCode::InvalidArgument, ByteString::formatted("Property '{}' is not a Boolean", key)); return property->as_bool(); - } else if constexpr (IsSame) { - if (auto maybe_u32 = property->get_u32(); maybe_u32.has_value()) - return *maybe_u32; + } else if constexpr (IsIntegral) { + if (auto maybe_number = property->get_integer(); maybe_number.has_value()) + return *maybe_number; + return WebDriver::Error::from_code(ErrorCode::InvalidArgument, ByteString::formatted("Property '{}' is not an Integer", key)); + } else if constexpr (IsSame) { + if (auto maybe_number = property->get_double_with_precision_loss(); maybe_number.has_value()) + return *maybe_number; return WebDriver::Error::from_code(ErrorCode::InvalidArgument, ByteString::formatted("Property '{}' is not a Number", key)); } else if constexpr (IsSame) { if (!property->is_array()) @@ -51,4 +52,41 @@ static ErrorOr get_property(JsonValue const& pay } } +template +static ErrorOr get_property(JsonValue const& payload, StringView key) +{ + if (!payload.is_object()) + return WebDriver::Error::from_code(ErrorCode::InvalidArgument, "Payload is not a JSON object"); + return get_property(payload.as_object(), key); +} + +template +static ErrorOr, WebDriver::Error> get_optional_property(JsonObject const& object, StringView key) +{ + if (!object.has(key)) + return OptionalNone {}; + return get_property(object, key); +} + +template +static ErrorOr get_property_with_limits(JsonObject const& object, StringView key, Optional min, Optional max) +{ + auto value = TRY(get_property(object, key)); + + if (min.has_value() && value < *min) + return WebDriver::Error::from_code(WebDriver::ErrorCode::InvalidArgument, ByteString::formatted("Property '{}' must not be less than {}", key, *min)); + if (max.has_value() && value > *max) + return WebDriver::Error::from_code(WebDriver::ErrorCode::InvalidArgument, ByteString::formatted("Property '{}' must not be greater than {}", key, *max)); + + return value; +} + +template +static ErrorOr, WebDriver::Error> get_optional_property_with_limits(JsonObject const& object, StringView key, Optional min, Optional max) +{ + if (!object.has(key)) + return OptionalNone {}; + return get_property_with_limits(object, key, min, max); +} + }