From 2e051f947d7b683409a9a496cad8ff8fd5a9c19c Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 18 Feb 2023 17:07:14 -0600 Subject: [PATCH] [ox/mc,oc] Add support for ox::Array serialization --- deps/ox/src/ox/mc/read.hpp | 17 +++++++++++++++-- deps/ox/src/ox/mc/write.hpp | 2 +- deps/ox/src/ox/oc/read.hpp | 8 ++++++++ deps/ox/src/ox/oc/write.hpp | 2 +- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/deps/ox/src/ox/mc/read.hpp b/deps/ox/src/ox/mc/read.hpp index 9174c62b..fdf76fb2 100644 --- a/deps/ox/src/ox/mc/read.hpp +++ b/deps/ox/src/ox/mc/read.hpp @@ -281,7 +281,20 @@ constexpr Error MetalClawReaderTemplate::field(const char *name, T return field(name, val->data(), val->size()); } ++m_field; - return OxError(0); + return {}; + } else if constexpr(isArray_v) { + if (m_unionIdx == -1 || m_unionIdx == m_field) { + // set size of val if the field is present, don't worry about it if not + if (m_fieldPresence.get(static_cast(m_field))) { + oxRequire(len, arrayLength(name, false)); + if (len > val->size()) { + return OxError(1, "Input array is too long"); + } + } + return field(name, val->data(), val->size()); + } + ++m_field; + return {}; } else { if ((m_unionIdx == -1 || m_unionIdx == m_field) && val) { if (m_fieldPresence.get(static_cast(m_field))) { @@ -291,7 +304,7 @@ constexpr Error MetalClawReaderTemplate::field(const char *name, T } } ++m_field; - return OxError(0); + return {}; } } diff --git a/deps/ox/src/ox/mc/write.hpp b/deps/ox/src/ox/mc/write.hpp index d384e7e6..6e344f10 100644 --- a/deps/ox/src/ox/mc/write.hpp +++ b/deps/ox/src/ox/mc/write.hpp @@ -290,7 +290,7 @@ constexpr Error MetalClawWriter::fieldCString(const char*, const char *val, std: template constexpr Error MetalClawWriter::field(const char*, T *val) noexcept { - if constexpr(isVector_v) { + if constexpr(isVector_v || isArray_v) { return field(nullptr, val->data(), val->size()); } else { bool fieldSet = false; diff --git a/deps/ox/src/ox/oc/read.hpp b/deps/ox/src/ox/oc/read.hpp index 828e933f..14ab6ea6 100644 --- a/deps/ox/src/ox/oc/read.hpp +++ b/deps/ox/src/ox/oc/read.hpp @@ -147,6 +147,14 @@ Error OrganicClawReader::field(const char *key, T *val) noexcept { const auto srcSize = srcVal.size(); val->resize(srcSize); err = field(key, val->data(), val->size()); + } else if constexpr(isArray_v) { + const auto &srcVal = value(key); + const auto srcSize = srcVal.size(); + if (srcSize > val->size()) { + err = OxError(1, "Input array is too long"); + } else { + err = field(key, val->data(), val->size()); + } } else if (targetValid()) { const auto &jv = value(key); if (jv.empty() || jv.isObject()) { diff --git a/deps/ox/src/ox/oc/write.hpp b/deps/ox/src/ox/oc/write.hpp index 05cd333d..d5f7be3a 100644 --- a/deps/ox/src/ox/oc/write.hpp +++ b/deps/ox/src/ox/oc/write.hpp @@ -214,7 +214,7 @@ Error OrganicClawWriter::field(const char *key, T *val, std::size_t len) noexcep template Error OrganicClawWriter::field(const char *key, T *val) noexcept { - if constexpr(isVector_v) { + if constexpr(isVector_v || isArray_v) { return field(key, val->data(), val->size()); } else if (val && targetValid()) { OrganicClawWriter w;