Skip to content

Commit

Permalink
Add backwards util conversion to pre-load cache_object:[UUID] when go…
Browse files Browse the repository at this point in the history
…ing from JSON to pydict
  • Loading branch information
drobison00 committed Dec 4, 2023
1 parent 2845adb commit 5b2265e
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion python/mrc/_pymrc/src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <pyerrors.h>
#include <warnings.h>

#include <regex>
#include <string>
#include <utility>

Expand Down Expand Up @@ -80,6 +81,7 @@ py::object cast_from_json(const json& source)
{
return py::none();
}

if (source.is_array())
{
py::list list_;
Expand All @@ -94,18 +96,22 @@ py::object cast_from_json(const json& source)
{
return py::bool_(source.get<bool>());
}

if (source.is_number_float())
{
return py::float_(source.get<double>());
}

if (source.is_number_integer())
{
return py::int_(source.get<json::number_integer_t>());
}

if (source.is_number_unsigned())
{
return py::int_(source.get<json::number_unsigned_t>()); // std::size_t ?
}

if (source.is_object())
{
py::dict dict;
Expand All @@ -116,9 +122,27 @@ py::object cast_from_json(const json& source)

return std::move(dict);
}

if (source.is_string())
{
return py::str(source.get<std::string>());
std::string str_val = source.get<std::string>();
std::regex uuid_regex("cache_object:([0-9a-fA-F-]{36})");
std::smatch uuid_match;

if (std::regex_search(str_val, uuid_match, uuid_regex))
{
std::string uuid = uuid_match[1];

Check warning on line 134 in python/mrc/_pymrc/src/utils.cpp

View check run for this annotation

Codecov / codecov/patch

python/mrc/_pymrc/src/utils.cpp#L134

Added line #L134 was not covered by tests

auto& cache = PythonObjectCache::get_handle();
if (cache.contains(uuid))

Check warning on line 137 in python/mrc/_pymrc/src/utils.cpp

View check run for this annotation

Codecov / codecov/patch

python/mrc/_pymrc/src/utils.cpp#L136-L137

Added lines #L136 - L137 were not covered by tests
{
return cache.get(uuid);

Check warning on line 139 in python/mrc/_pymrc/src/utils.cpp

View check run for this annotation

Codecov / codecov/patch

python/mrc/_pymrc/src/utils.cpp#L139

Added line #L139 was not covered by tests
} else {
throw std::runtime_error("Cached object id not found in cache: " + uuid);

Check warning on line 141 in python/mrc/_pymrc/src/utils.cpp

View check run for this annotation

Codecov / codecov/patch

python/mrc/_pymrc/src/utils.cpp#L141

Added line #L141 was not covered by tests
}
}

return py::str(str_val);
}

return py::none();
Expand Down

0 comments on commit 5b2265e

Please sign in to comment.