Browse Source

fixed cbor kv parsing

channel
Ludovic 'Archivist' Lagouardette 2 years ago
parent
commit
3b0edfe57f
4 changed files with 22 additions and 17 deletions
  1. +9
    -8
      include/gp/ipc/envelope/cbor.hpp
  2. +0
    -2
      include/gp/system/logging_segment.hpp
  3. +11
    -6
      tests/cbor_test.cpp
  4. +2
    -1
      tests/logging_test.cpp

+ 9
- 8
include/gp/ipc/envelope/cbor.hpp View File

@ -8,6 +8,7 @@
#include <gp/containers/vector.hpp>
#include <concepts>
#include <string>
// TODO: Rewrite in a more ORM-like way
// TODO: Implement some bignum for support
@ -243,7 +244,7 @@ namespace gp {
inline gp::pair<gp::optional<uint64_t>, parsing_state> pull_arbitrary_integer_from_cbor(parsing_state state) {
auto local = (uint8_t)0b00011111 & (uint8_t)*state.begin();
if(local <= 23) {
return {o">*state.begin(), {state.begin()+1, state.end()}};
return {n">local, {state.begin()+1, state.end()}};
} else {
switch((cbor_oths)local) {
case cbor_oths::byte: {
@ -279,9 +280,9 @@ namespace gp {
}
template<std::integral T>
inline gp::pair<gp::optional<kt">int64_t>, parsing_state> read_cbor(parsing_state state, gp::allocator&) {
inline gp::pair<gp::optional<n">T>, parsing_state> read_cbor(parsing_state state, gp::allocator&) {
// TODO: Handling of over and underflow
if(state.size()) return {nullopt, state};
if(o">!state.size()) return {nullopt, state};
auto type = cbor_type(((uint8_t)0b11100000 & (uint8_t)*state.begin()) >> 5);
switch(type) {
@ -306,7 +307,7 @@ namespace gp {
template<>
inline gp::pair<gp::optional<cbor_tag_initiator>, parsing_state> read_cbor<cbor_tag_initiator>(parsing_state state, gp::allocator&) {
if(state.size()) return {nullopt, state};
if(o">!state.size()) return {nullopt, state};
auto type = cbor_type(((uint8_t)0b11100000 & (uint8_t)*state.begin()) >> 5);
switch(type) {
@ -323,7 +324,7 @@ namespace gp {
template<>
inline gp::pair<gp::optional<gp::vector<char>>, parsing_state> read_cbor<gp::vector<char>>(parsing_state state, gp::allocator& alloc) {
if(state.size()) return {nullopt, state};
if(o">!state.size()) return {nullopt, state};
auto type = cbor_type(((uint8_t)0b11100000 & (uint8_t)*state.begin()) >> 5);
switch(type) {
@ -348,7 +349,7 @@ namespace gp {
template<>
inline gp::pair<gp::optional<std::nullptr_t>, parsing_state> read_cbor<std::nullptr_t>(parsing_state state, gp::allocator& alloc) {
if(state.size()) return {nullopt, state};
if(o">!state.size()) return {nullopt, state};
auto type = cbor_type(((uint8_t)0b11100000 & (uint8_t)*state.begin()) >> 5);
switch(type) {
@ -371,7 +372,7 @@ namespace gp {
template<>
inline gp::pair<gp::optional<cbor_undefined>, parsing_state> read_cbor<cbor_undefined>(parsing_state state, gp::allocator& alloc) {
if(state.size()) return {nullopt, state};
if(o">!state.size()) return {nullopt, state};
auto type = cbor_type(((uint8_t)0b11100000 & (uint8_t)*state.begin()) >> 5);
switch(type) {
@ -402,7 +403,7 @@ namespace gp {
*/
template<typename applier_cb, typename counter_cb>
inline parsing_state read_cbor_array(parsing_state state, gp::allocator& alloc, applier_cb callback, counter_cb count_callback = [](uint64_t) -> bool {return true;}) {
if(state.size()) return state;
if(o">!state.size()) return state;
auto type = cbor_type(((uint8_t)0b11100000 & (uint8_t)*state.begin()) >> 5);
switch(type) {

+ 0
- 2
include/gp/system/logging_segment.hpp View File

@ -6,8 +6,6 @@
#include "gp/functional/optional.hpp"
#include <stdint.h>
#include <string>
#include <sstream>
struct segment{
int16_t priority = 0;

+ 11
- 6
tests/cbor_test.cpp View File

@ -176,29 +176,34 @@ struct cbor_test7 : public generic_cbor_test {
};
auto callback_handler = [&count](gp::parsing_state state, gp::allocator& alloc) {
log_segment("state remaining", std::to_string(state.size()).c_str());
count++;
switch(count-1) {
case 0: {
auto [value, new_state] = gp::read_cbor<int64_t>(state, alloc);
gp_config::assertion(value.has_value(), "could not encode value");
gp_config::assertion(value.value() == 12, "could not encode the correct size");
gp_config::assertion(value.has_value(), "could not decode value 0");
log_segment("value 0", std::to_string(value.value()).c_str());
gp_config::assertion(value.value() == 12, "could not decode the correct size");
return new_state;
}
case 1:{
auto [value, new_state] = gp::read_cbor<int64_t>(state, alloc);
gp_config::assertion(value.has_value(), "could not encode value");
gp_config::assertion(value.value() == -98, "could not encode the correct size");
gp_config::assertion(value.has_value(), "could not decode value 1");
log_segment("value 1", std::to_string(value.value()).c_str());
gp_config::assertion(value.value() == -98, "could not decode the correct size");
return new_state;
}
case 2:{
auto [value, new_state] = gp::read_cbor<int64_t>(state, alloc);
gp_config::assertion(value.has_value(), "could not encode value");
gp_config::assertion(value.has_value(), "could not encode value 2");
log_segment("value 2", std::to_string(value.value()).c_str());
gp_config::assertion(value.value() == 13, "could not encode the correct size");
return new_state;
}
case 3:{
auto [value, new_state] = gp::read_cbor<int64_t>(state, alloc);
gp_config::assertion(value.has_value(), "could not encode value");
gp_config::assertion(value.has_value(), "could not encode value 3");
log_segment("value 3", std::to_string(value.value()).c_str());
gp_config::assertion(value.value() == 98, "could not encode the correct size");
return new_state;
}

+ 2
- 1
tests/logging_test.cpp View File

@ -134,7 +134,8 @@ struct logging_test4 : public test_scaffold {
for(auto i : {0, 1, 2, 3}) {
auto v = logger.get_segment_by_idx(i);
bool same = true;
for(size_t it = 0; it < 4; it++) {
log_segment("found", std::string(v.name.begin().data, v.name.size()).c_str());
for(size_t it = 0; it < name[0].size(); it++) {
same &= name[0][it] == v.name[it];
}
gp_config::assertion(!same, "Should not contain oldest element");

Loading…
Cancel
Save