Change return value of OpaqueVal::DoSerialize.

Now it returns a broker::expected<broker::data>, instead of directly
returning a broker::data before. This means that broker::data does no
longer have to be abused to convey error information.

In a way it would be kind of neat to have more fine-granular broker error
types for this use-case - at the moment everything returns
broker::ec::invalid_data, which seems to be the only reasonable choice.
This commit is contained in:
Johanna Amann 2019-06-18 15:23:02 -07:00
parent 53cde131e9
commit e0f10fd6d3
6 changed files with 40 additions and 36 deletions

View file

@ -58,10 +58,10 @@ broker::expected<broker::data> OpaqueVal::Serialize() const
auto type = OpaqueMgr::mgr()->TypeID(this);
auto d = DoSerialize();
if ( d == broker::none() )
return broker::ec::invalid_data; // Cannot serialize
if ( !d )
return d.error();
return {broker::vector{std::move(type), std::move(d)}};
return {broker::vector{std::move(type), std::move(*d)}};
}
OpaqueVal* OpaqueVal::Unserialize(const broker::data& data)
@ -288,7 +288,7 @@ StringVal* MD5Val::DoGet()
IMPLEMENT_OPAQUE_VALUE(MD5Val)
broker::data MD5Val::DoSerialize() const
broker::expected<broker::data> MD5Val::DoSerialize() const
{
if ( ! IsValid() )
return broker::vector{false};
@ -429,7 +429,7 @@ StringVal* SHA1Val::DoGet()
IMPLEMENT_OPAQUE_VALUE(SHA1Val)
broker::data SHA1Val::DoSerialize() const
broker::expected<broker::data> SHA1Val::DoSerialize() const
{
if ( ! IsValid() )
return broker::vector{false};
@ -573,7 +573,7 @@ StringVal* SHA256Val::DoGet()
IMPLEMENT_OPAQUE_VALUE(SHA256Val)
broker::data SHA256Val::DoSerialize() const
broker::expected<broker::data> SHA256Val::DoSerialize() const
{
if ( ! IsValid() )
return broker::vector{false};
@ -659,7 +659,7 @@ bool EntropyVal::Get(double *r_ent, double *r_chisq, double *r_mean,
IMPLEMENT_OPAQUE_VALUE(EntropyVal)
broker::data EntropyVal::DoSerialize() const
broker::expected<broker::data> EntropyVal::DoSerialize() const
{
broker::vector d =
{
@ -872,7 +872,7 @@ BloomFilterVal::~BloomFilterVal()
IMPLEMENT_OPAQUE_VALUE(BloomFilterVal)
broker::data BloomFilterVal::DoSerialize() const
broker::expected<broker::data> BloomFilterVal::DoSerialize() const
{
broker::vector d;
@ -880,7 +880,7 @@ broker::data BloomFilterVal::DoSerialize() const
{
auto t = SerializeType(type);
if ( t == broker::none() )
return broker::none();
return broker::ec::invalid_data;
d.emplace_back(t);
}
@ -889,7 +889,7 @@ broker::data BloomFilterVal::DoSerialize() const
auto bf = bloom_filter->Serialize();
if ( ! bf )
return broker::none();
return broker::ec::invalid_data; // Cannot serialize;
d.emplace_back(*bf);
return d;
@ -976,7 +976,7 @@ void CardinalityVal::Add(const Val* val)
IMPLEMENT_OPAQUE_VALUE(CardinalityVal)
broker::data CardinalityVal::DoSerialize() const
broker::expected<broker::data> CardinalityVal::DoSerialize() const
{
broker::vector d;
@ -984,7 +984,7 @@ broker::data CardinalityVal::DoSerialize() const
{
auto t = SerializeType(type);
if ( t == broker::none() )
return broker::none();
return broker::ec::invalid_data;
d.emplace_back(t);
}
@ -993,7 +993,7 @@ broker::data CardinalityVal::DoSerialize() const
auto cs = c->Serialize();
if ( ! cs )
return broker::none();
return broker::ec::invalid_data;
d.emplace_back(*cs);
return d;

View file

@ -64,7 +64,7 @@ private:
/** Macro to insert into an OpaqueVal-derived class's declaration. */
#define DECLARE_OPAQUE_VALUE(T) \
friend class OpaqueMgr::Register<T>; \
broker::data DoSerialize() const override; \
broker::expected<broker::data> DoSerialize() const override; \
bool DoUnserialize(const broker::data& data) override; \
const char* OpaqueName() const override { return #T; } \
static OpaqueVal* OpaqueInstantiate() { return new T(); }
@ -97,7 +97,7 @@ public:
* Reinstantiates a value from its serialized Broker representation.
*
* @param data Broker representation as returned by *Serialize()*.
* @return unserialized instances with referecnce count at +1
* @return unserialized instances with reference count at +1
*/
static OpaqueVal* Unserialize(const broker::data& data);
@ -107,15 +107,19 @@ protected:
OpaqueVal() { }
/**
* Must be overriden to provide a serialized version of the derived
* class' state. Returns 'broker::none()' if serialization fails, or
* is not supported.
* Must be overridden to provide a serialized version of the derived
* class' state.
*
* @return the serialized data or an error if serialization
* isn't supported or failed.
*/
virtual broker::data DoSerialize() const = 0;
virtual broker::expected<broker::data> DoSerialize() const = 0;
/**
* Must be overriden to recreate the the derived class' state from a
* serialization. Returns true if successfull.
* Must be overridden to recreate the the derived class' state from a
* serialization.
*
* @return true if successful.
*/
virtual bool DoUnserialize(const broker::data& data) = 0;

View file

@ -1114,7 +1114,7 @@ Val* bro_broker::DataVal::castTo(BroType* t)
IMPLEMENT_OPAQUE_VALUE(bro_broker::DataVal)
broker::data bro_broker::DataVal::DoSerialize() const
broker::expected<broker::data> bro_broker::DataVal::DoSerialize() const
{
return data;
}
@ -1127,7 +1127,7 @@ bool bro_broker::DataVal::DoUnserialize(const broker::data& data_)
IMPLEMENT_OPAQUE_VALUE(bro_broker::SetIterator)
broker::data bro_broker::SetIterator::DoSerialize() const
broker::expected<broker::data> bro_broker::SetIterator::DoSerialize() const
{
return broker::vector{dat, *it};
}
@ -1154,7 +1154,7 @@ bool bro_broker::SetIterator::DoUnserialize(const broker::data& data)
IMPLEMENT_OPAQUE_VALUE(bro_broker::TableIterator)
broker::data bro_broker::TableIterator::DoSerialize() const
broker::expected<broker::data> bro_broker::TableIterator::DoSerialize() const
{
return broker::vector{dat, it->first};
}
@ -1181,7 +1181,7 @@ bool bro_broker::TableIterator::DoUnserialize(const broker::data& data)
IMPLEMENT_OPAQUE_VALUE(bro_broker::VectorIterator)
broker::data bro_broker::VectorIterator::DoSerialize() const
broker::expected<broker::data> bro_broker::VectorIterator::DoSerialize() const
{
broker::integer difference = it - dat.begin();
return broker::vector{dat, difference};
@ -1203,7 +1203,7 @@ bool bro_broker::VectorIterator::DoUnserialize(const broker::data& data)
IMPLEMENT_OPAQUE_VALUE(bro_broker::RecordIterator)
broker::data bro_broker::RecordIterator::DoSerialize() const
broker::expected<broker::data> bro_broker::RecordIterator::DoSerialize() const
{
broker::integer difference = it - dat.begin();
return broker::vector{dat, difference};

View file

@ -51,10 +51,10 @@ void StoreHandleVal::ValDescribe(ODesc* d) const
IMPLEMENT_OPAQUE_VALUE(StoreHandleVal)
broker::data StoreHandleVal::DoSerialize() const
broker::expected<broker::data> StoreHandleVal::DoSerialize() const
{
// Cannot serialize.
return broker::none();
return broker::ec::invalid_data;
}
bool StoreHandleVal::DoUnserialize(const broker::data& data)

View file

@ -491,13 +491,13 @@ Val* X509Val::DoClone(CloneState* state)
IMPLEMENT_OPAQUE_VALUE(X509Val)
broker::data X509Val::DoSerialize() const
broker::expected<broker::data> X509Val::DoSerialize() const
{
unsigned char *buf = NULL;
int length = i2d_X509(certificate, &buf);
if ( length < 0 )
return broker::none();
return broker::ec::invalid_data;
auto d = std::string(reinterpret_cast<const char*>(buf), length);
OPENSSL_free(buf);

View file

@ -408,7 +408,7 @@ void TopkVal::IncrementCounter(Element* e, unsigned int count)
IMPLEMENT_OPAQUE_VALUE(TopkVal)
broker::data TopkVal::DoSerialize() const
broker::expected<broker::data> TopkVal::DoSerialize() const
{
broker::vector d = {size, numElements, pruned};
@ -416,7 +416,7 @@ broker::data TopkVal::DoSerialize() const
{
auto t = SerializeType(type);
if ( t == broker::none() )
return broker::none();
return broker::ec::invalid_data;
d.emplace_back(t);
}
@ -440,7 +440,7 @@ broker::data TopkVal::DoSerialize() const
d.emplace_back(element->epsilon);
auto v = bro_broker::val_to_data(element->value);
if ( ! v )
return broker::none();
return broker::ec::invalid_data;
d.emplace_back(*v);