Skip to content

Commit

Permalink
Turns fromJson into a Product constructor
Browse files Browse the repository at this point in the history
The JsonObject forward declaration seems good enough.
No actual leakage of WinRT types
to be consistent with the production API.
  • Loading branch information
CarlosNihelton committed Sep 22, 2023
1 parent 9d029e4 commit 1615761
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 28 deletions.
36 changes: 18 additions & 18 deletions storeapi/base/impl/WinMockContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <iterator>
#include <sstream>
#include <unordered_map>
#include <utility>

#include "WinRTHelpers.hpp"

Expand All @@ -35,9 +36,6 @@ namespace {
IAsyncOperation<JsonObject> call(winrt::hstring relativePath,
UrlParams const& params = {});

/// Creates a product from a JsonObject containing the relevant information.
WinMockContext::Product fromJson(JsonObject const& obj);

/// Translates a textual representation of a purchase transaction result into an
/// instance of the PurchaseStatus enum.
StoreApi::PurchaseStatus translate(winrt::hstring const& purchaseStatus);
Expand Down Expand Up @@ -69,7 +67,7 @@ std::vector<WinMockContext::Product> WinMockContext::GetProducts(
result.reserve(products.Size());
for (const IJsonValue& product : products) {
JsonObject p = product.GetObject();
result.emplace_back(fromJson(p));
result.emplace_back(WinMockContext::Product{p});
}

return result;
Expand Down Expand Up @@ -131,6 +129,22 @@ void WinMockContext::Product::PromptUserForPurchase(
});
}

WinMockContext::Product::Product(JsonObject const& json)
: storeID{winrt::to_string(json.GetNamedString(L"StoreID"))},
title{winrt::to_string(json.GetNamedString(L"Title"))},
description{winrt::to_string(json.GetNamedString(L"Description"))},
productKind{winrt::to_string(json.GetNamedString(L"ProductKind"))},
expirationDate{},
isInUserCollection{json.GetNamedBoolean(L"IsInUserCollection")}

{
std::chrono::system_clock::time_point tp{};
std::stringstream ss{
winrt::to_string(json.GetNamedString(L"ExpirationDate"))};
ss >> std::chrono::parse("%FT%T%Tz", tp);
expirationDate = tp;
}

namespace {
// Returns the mock server endpoint address and port by reading the environment
// variable UP4W_MS_STORE_MOCK_ENDPOINT or localhost:9 if the variable is unset.
Expand Down Expand Up @@ -177,20 +191,6 @@ IAsyncOperation<JsonObject> call(winrt::hstring relativePath,
co_return JsonObject::Parse(contents);
}

WinMockContext::Product fromJson(JsonObject const& obj) {
std::chrono::system_clock::time_point tp{};
std::stringstream ss{winrt::to_string(obj.GetNamedString(L"ExpirationDate"))};
ss >> std::chrono::parse("%FT%T%Tz", tp);

return WinMockContext::Product{
winrt::to_string(obj.GetNamedString(L"StoreID")),
winrt::to_string(obj.GetNamedString(L"Title")),
winrt::to_string(obj.GetNamedString(L"Description")),
winrt::to_string(obj.GetNamedString(L"ProductKind")),
tp,
obj.GetNamedBoolean(L"IsInUserCollection")};
}

StoreApi::PurchaseStatus translate(winrt::hstring const& purchaseStatus) {
if (purchaseStatus == L"Succeeded") {
return PurchaseStatus::Succeeded;
Expand Down
17 changes: 7 additions & 10 deletions storeapi/base/impl/WinMockContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

#include "../Purchase.hpp"

namespace winrt::Windows::Data::Json {
class JsonObject;
}

namespace StoreApi::impl {

class WinMockContext {
Expand Down Expand Up @@ -50,17 +54,10 @@ class WinMockContext {
void PromptUserForPurchase(PurchaseCallback callback) const;

public:
/// Creates a product from a JsonObject obtained from a call to the mock
/// server containing the relevant information.
explicit Product(winrt::Windows::Data::Json::JsonObject const& json);
Product() = default;
Product(std::string&& storeID, std::string&& title,
std::string&& description, std::string&& productKind,
std::chrono::system_clock::time_point expirationDate,
bool isInUserCollection)
: storeID(std::move(storeID)),
title(std::move(title)),
description(std::move(description)),
productKind(std::move(productKind)),
expirationDate(std::move(expirationDate)),
isInUserCollection(isInUserCollection) {}
};

// Returns a collection of products matching the supplied [kinds] and [ids].
Expand Down

0 comments on commit 1615761

Please sign in to comment.