Skip to content

Commit

Permalink
Feature/cbor map (#542)
Browse files Browse the repository at this point in the history
* respect cbor map order

Signed-off-by: Alexey Chernyshov <alexey.n.chernyshov@gmail.com>
  • Loading branch information
Alexey-N-Chernyshov authored Dec 1, 2021
1 parent 6693708 commit 9dc996a
Show file tree
Hide file tree
Showing 3 changed files with 545 additions and 461 deletions.
32 changes: 32 additions & 0 deletions core/codec/cbor/cbor_encode_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
#include "common/cmp.hpp"

namespace fc::codec::cbor {
CborEncodeStream &CborOrderedMap::operator[](const std::string &key) {
// remove old value if present and push new one
erase(std::remove_if(
begin(), end(), [&key](const auto &p) { return p.first == key; }),
end());
emplace_back(key, CborEncodeStream{});
return back().second;
}

CborEncodeStream &CborEncodeStream::operator<<(const Bytes &bytes) {
return *this << gsl::make_span(bytes);
}
Expand Down Expand Up @@ -100,6 +109,21 @@ namespace fc::codec::cbor {
return *this;
}

CborEncodeStream &CborEncodeStream::operator<<(const CborOrderedMap &map) {
addCount(1);
writeMap(data_, map.size());
const auto count{count_};
for (const auto &pair : map) {
if (pair.second.count() != 1) {
outcome::raise(CborEncodeError::kExpectedMapValueSingle);
}
*this << common::span::bytestr(common::span::cbytes(pair.first));
*this << pair.second;
}
count_ = count;
return *this;
}

CborEncodeStream &CborEncodeStream::operator<<(std::nullptr_t) {
addCount(1);
writeNull(data_);
Expand All @@ -115,6 +139,10 @@ namespace fc::codec::cbor {
return result;
}

size_t CborEncodeStream::count() const {
return count_;
}

CborEncodeStream CborEncodeStream::list() {
CborEncodeStream stream;
stream.is_list_ = true;
Expand All @@ -125,6 +153,10 @@ namespace fc::codec::cbor {
return {};
}

CborOrderedMap CborEncodeStream::orderedMap() {
return {};
}

CborEncodeStream CborEncodeStream::wrap(BytesIn data, size_t count) {
CborEncodeStream s;
copy(s.data_, data);
Expand Down
16 changes: 15 additions & 1 deletion core/codec/cbor/cbor_encode_stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
#include "primitives/cid/cid.hpp"

namespace fc::codec::cbor {
class CborEncodeStream;

/** Cbor map with respect for key order */
struct CborOrderedMap
: public std::vector<std::pair<std::string, CborEncodeStream>> {
CborEncodeStream &operator[](const std::string &key);
};

/** Encodes CBOR */
class CborEncodeStream {
public:
Expand Down Expand Up @@ -98,17 +106,23 @@ namespace fc::codec::cbor {
CborEncodeStream &operator<<(const BlockParentCbCids &parents);
/** Encodes list container encode substream */
CborEncodeStream &operator<<(const CborEncodeStream &other);
/** Encodes map container encode substream map */
/** Encodes map container encode substream map with canonical order */
CborEncodeStream &operator<<(
const std::map<std::string, CborEncodeStream> &map);
/** Encodes map container encode substream map with order respect */
CborEncodeStream &operator<<(const CborOrderedMap &map);
/** Encodes null */
CborEncodeStream &operator<<(std::nullptr_t);
/** Returns CBOR bytes of encoded elements */
Bytes data() const;
/** Returns the number of elements */
size_t count() const;
/** Creates list container encode substream */
static CborEncodeStream list();
/** Creates map container encode substream map */
static std::map<std::string, CborEncodeStream> map();
/** Creates map container encode substream map */
static CborOrderedMap orderedMap();
/** Wraps CBOR bytes */
static CborEncodeStream wrap(BytesIn data, size_t count);

Expand Down
Loading

0 comments on commit 9dc996a

Please sign in to comment.