msgpack seems to work

Specify cacheFormat: 1 (SerializeFormat::MessagePack) in initializationOptions to write to .mpack cache files.
This commit is contained in:
Fangrui Song 2018-01-06 21:08:25 -08:00
parent a0a52e50a2
commit 286a0649c1
6 changed files with 25 additions and 17 deletions

View File

@ -154,7 +154,8 @@ inline bool operator!=(const IndexFuncRef& a, const IndexFuncRef& b) {
}
inline void Reflect(Reader& visitor, IndexFuncRef& value) {
const char* str_value = visitor.GetCString();
std::string s = visitor.GetString();
const char* str_value = s.c_str();
if (str_value[0] == '~') {
value.is_implicit = true;
++str_value;

View File

@ -148,8 +148,10 @@ bool Range::operator<(const Range& that) const {
void Reflect(Reader& visitor, Position& value) {
if (!visitor.IsString())
value = Position();
else
value = Position(visitor.GetCString());
else {
std::string s = visitor.GetString();
value = Position(s.c_str());
}
}
void Reflect(Writer& visitor, Position& value) {
std::string output = value.ToString();
@ -160,8 +162,10 @@ void Reflect(Writer& visitor, Position& value) {
void Reflect(Reader& visitor, Range& value) {
if (!visitor.IsString())
value = Range();
else
value = Range(visitor.GetCString());
else {
std::string s = visitor.GetString();
value = Range(s.c_str());
}
}
void Reflect(Writer& visitor, Range& value) {
std::string output = value.ToString();

View File

@ -6,6 +6,7 @@
#include "indexer.h"
#include <doctest/doctest.h>
#include <loguru.hpp>
namespace {
bool gTestOutputMode = false;
@ -252,7 +253,8 @@ std::unique_ptr<IndexFile> Deserialize(SerializeFormat format,
Reflect(reader, *file);
if (file->version != expected_version)
return nullptr;
} catch (msgpack::insufficient_bytes&) {
} catch (msgpack::unpack_error& ex) {
LOG_S(ERROR) << "msgpack::unpack_err for '" << path << "' " << ex.what();
return nullptr;
}
break;

View File

@ -27,8 +27,7 @@ class Reader {
virtual int GetInt() = 0;
virtual int64_t GetInt64() = 0;
virtual uint64_t GetUint64() = 0;
virtual const char* GetCString() = 0;
virtual std::string GetString() { return GetCString(); }
virtual std::string GetString() = 0;
virtual bool HasMember(const char* x) = 0;
virtual std::unique_ptr<Reader> operator[](const char* x) = 0;
@ -163,8 +162,12 @@ void Reflect(Reader& visitor, optional<T>& value) {
}
template <typename T>
void Reflect(Writer& visitor, optional<T>& value) {
// We omit optional fields for JsonWriter to reduce output.
// But don't omit them for other serialization formats.
if (value)
Reflect(visitor, value.value());
Reflect(visitor, *value);
else if (visitor.Format() != SerializeFormat::Json)
visitor.Null();
}
// std::variant (Writer only)
@ -179,8 +182,6 @@ void Reflect(Writer& visitor, std::variant<T0, T1>& value) {
// std::vector
template <typename T>
void Reflect(Reader& visitor, std::vector<T>& values) {
if (!visitor.IsArray())
return;
visitor.IterArray([&](Reader& entry) {
T entry_value;
Reflect(entry, entry_value);
@ -224,10 +225,10 @@ void ReflectMember(Writer& visitor, const char* name, std::vector<T>& values) {
}
template <typename T>
void ReflectMember(Writer& visitor, const char* name, optional<T>& value) {
if (!value)
return;
if (value || visitor.Format() != SerializeFormat::Json) {
visitor.Key(name);
Reflect(visitor, value);
}
}
void ReflectMember(Writer& visitor, const char* name, std::string& value);

View File

@ -24,7 +24,7 @@ class JsonReader : public Reader {
int GetInt() override { return m_->GetInt(); }
int64_t GetInt64() override { return m_->GetInt64(); }
uint64_t GetUint64() override { return m_->GetUint64(); }
const char* GetCString() override { return m_->GetString(); }
std::string GetString() override { return m_->GetString(); }
bool HasMember(const char* x) override { return m_->HasMember(x); }
std::unique_ptr<Reader> operator[](const char* x) override {

View File

@ -25,7 +25,7 @@ class MessagePackReader : public Reader {
int GetInt() override { return o_.as<int>(); }
int64_t GetInt64() override { return o_.as<int64_t>(); }
uint64_t GetUint64() override { return o_.as<uint64_t>(); }
const char* GetCString() override { return o_.as<char*>(); }
std::string GetString() override { return o_.as<std::string>(); }
bool HasMember(const char* x) override { return true; }
std::unique_ptr<Reader> operator[](const char* x) override {