Skip to content

Commit

Permalink
Added union methods to the Reader
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzicheng1987 committed Dec 1, 2024
1 parent 8db12dc commit 1f22054
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
27 changes: 27 additions & 0 deletions include/rfl/avro/Reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@ struct Reader {
const avro_value_t* val_;
};

struct AVROInputUnion {
const avro_value_t* val_;
};

struct AVROInputVar {
const avro_value_t* val_;
};

using InputArrayType = AVROInputArray;
using InputObjectType = AVROInputObject;
using InputUnionType = AVROInputUnion;
using InputVarType = AVROInputVar;

template <class T>
Expand All @@ -45,6 +50,9 @@ struct Reader {
rfl::Result<InputVarType> get_field_from_object(
const std::string& _name, const InputObjectType& _obj) const noexcept;

rfl::Result<InputVarType> get_field_from_union(
const size_t _index, const InputUnionType& _union) const noexcept;

bool is_empty(const InputVarType& _var) const noexcept;

template <class T>
Expand Down Expand Up @@ -133,6 +141,8 @@ struct Reader {
rfl::Result<InputObjectType> to_object(
const InputVarType& _var) const noexcept;

rfl::Result<InputUnionType> to_union(const InputVarType& _var) const noexcept;

template <class ArrayReader>
std::optional<Error> read_array(const ArrayReader& _array_reader,
const InputArrayType& _arr) const noexcept {
Expand Down Expand Up @@ -163,6 +173,23 @@ struct Reader {
return std::nullopt;
}

template <class VariantType, class VariantParserType>
rfl::Result<VariantType> to_variant(
const size_t _index, const InputUnionType& _union) const noexcept {
int disc = 0;
auto err = avro_value_get_discriminant(_union.val_, &disc);
if (err) {
return Error("Could not get the discriminant.");
}
avro_value_t value;
err = avro_value_get_current_branch(_union.val_, &value);
if (err) {
return Error("Could not cast the union type.");
}
return VariantParserType::parse(static_cast<size_t>(disc),
InputVarType{&value});
}

template <class T>
rfl::Result<T> use_custom_constructor(
const InputVarType& _var) const noexcept {
Expand Down
13 changes: 11 additions & 2 deletions src/rfl/avro/Reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ namespace rfl::avro {

rfl::Result<Reader::InputVarType> Reader::get_field_from_array(
const size_t _idx, const InputArrayType& _arr) const noexcept {
return Error("TODO");
return Error("This should never be called - Avro has explicit union types.");
}

rfl::Result<Reader::InputVarType> Reader::get_field_from_object(
const std::string& _name, const InputObjectType& _obj) const noexcept {
return Error("TODO");
return Error("This should never be called - Avro has explicit union types.");
}

bool Reader::is_empty(const InputVarType& _var) const noexcept {
Expand All @@ -24,6 +24,15 @@ rfl::Result<Reader::InputArrayType> Reader::to_array(
return InputArrayType{_var.val_};
}

rfl::Result<Reader::InputUnionType> Reader::to_union(
const InputVarType& _var) const noexcept {
const auto type = avro_value_get_type(_var.val_);
if (type != AVRO_UNION) {
return Error("Could not cast to a union.");
}
return InputUnionType{_var.val_};
}

rfl::Result<Reader::InputObjectType> Reader::to_object(
const InputVarType& _var) const noexcept {
const auto type = avro_value_get_type(_var.val_);
Expand Down

0 comments on commit 1f22054

Please sign in to comment.