19 #ifndef avro_GenericDatum_hh__
20 #define avro_GenericDatum_hh__
27 #if __cplusplus >= 201703L
30 #include "boost/any.hpp"
33 #include "LogicalType.hh"
35 #include "ValidSchema.hh"
65 #if __cplusplus >= 201703L
72 : type_(t), logicalType_(LogicalType::NONE) {}
75 : type_(t), logicalType_(logicalType) {}
79 : type_(t), logicalType_(logicalType), value_(v) {}
81 void init(
const NodePtr &schema);
100 const T &value()
const;
123 size_t unionBranch()
const;
129 void selectBranch(
size_t branch);
193 GenericDatum(
const NodePtr &schema,
const T &v) : type_(schema->type()), logicalType_(schema->logicalType()) {
195 #if __cplusplus >= 201703L
196 *std::any_cast<T>(&value_) = v;
198 *boost::any_cast<T>(&value_) = v;
216 static void assertType(
const NodePtr &schema,
Type type);
260 if (curBranch_ != branch) {
287 std::vector<GenericDatum> fields_;
300 return fields_.size();
308 if (!schema()->nameIndex(name, index)) {
309 throw Exception(
"Invalid field name: " + name);
320 return schema()->nameIndex(name, index);
327 return fieldAt(fieldIndex(name));
335 return fieldAt(fieldIndex(name));
370 typedef std::vector<GenericDatum>
Value;
405 typedef std::vector<std::pair<std::string, GenericDatum>>
Value;
438 static size_t index(
const NodePtr &schema,
const std::string &symbol) {
440 if (schema->nameIndex(symbol, result)) {
462 if (n < schema()->names()) {
463 return schema()->nameAt(n);
472 size_t index(
const std::string &symbol)
const {
473 return index(schema(), symbol);
479 size_t set(
const std::string &symbol) {
480 return value_ = index(symbol);
487 if (n < schema()->names()) {
505 return schema()->nameAt(value_);
513 std::vector<uint8_t> value_;
521 value_.resize(schema->fixedSize());
524 GenericFixed(
const NodePtr &schema,
const std::vector<uint8_t> &v);
529 const std::vector<uint8_t> &
value()
const {
543 #if __cplusplus >= 201703L
544 std::any_cast<GenericUnion>(&value_)->datum().type()
547 boost::any_cast<GenericUnion>(&value_)->datum().type()
555 #if __cplusplus >= 201703L
556 std::any_cast<GenericUnion>(&value_)->datum().logicalType() :
558 boost::any_cast<GenericUnion>(&value_)->datum().logicalType() :
566 #if __cplusplus >= 201703L
567 std::any_cast<GenericUnion>(&value_)->datum().value<T>()
568 : *std::any_cast<T>(&value_);
570 boost::any_cast<GenericUnion>(&value_)->datum().value<T>()
571 : *boost::any_cast<T>(&value_);
578 #if __cplusplus >= 201703L
579 std::any_cast<GenericUnion>(&value_)->datum().value<T>()
580 : *std::any_cast<T>(&value_);
582 boost::any_cast<GenericUnion>(&value_)->datum().value<T>()
583 : *boost::any_cast<T>(&value_);
588 #if __cplusplus >= 201703L
589 return std::any_cast<GenericUnion>(&value_)->currentBranch();
591 return boost::any_cast<GenericUnion>(&value_)->currentBranch();
596 #if __cplusplus >= 201703L
597 std::any_cast<GenericUnion>(&value_)->selectBranch(branch);
599 boost::any_cast<GenericUnion>(&value_)->selectBranch(branch);
604 #endif // avro_GenericDatum_hh__
@ AVRO_NULL
Definition: Types.hh:40
Type type() const
The avro data type this datum holds.
Definition: GenericDatum.hh:541
std::vector< GenericDatum > Value
The contents type for the array.
Definition: GenericDatum.hh:370
GenericDatum & field(const std::string &name)
Returns the reference to the field with the given name name, which can be used to change the contents...
Definition: GenericDatum.hh:334
GenericDatum(double v)
Makes a new AVRO_DOUBLE datum whose value is of type double.
Definition: GenericDatum.hh:161
const GenericDatum & field(const std::string &name) const
Returns the field with the given name name.
Definition: GenericDatum.hh:326
GenericDatum(int64_t v)
Makes a new AVRO_LONG datum whose value is of type int64_t.
Definition: GenericDatum.hh:149
@ AVRO_LONG
Definition: Types.hh:36
bool isUnion() const
Returns true if and only if this datum is a union.
Definition: GenericDatum.hh:117
const std::vector< uint8_t > & value() const
Returns the contents of this fixed.
Definition: GenericDatum.hh:529
void selectBranch(size_t branch)
Selects a new branch.
Definition: GenericDatum.hh:259
@ AVRO_ENUM
Definition: Types.hh:43
GenericDatum(int32_t v)
Makes a new AVRO_INT datum whose value is of type int32_t.
Definition: GenericDatum.hh:143
const NodePtr & schema() const
Returns the schema for this object.
Definition: GenericDatum.hh:228
@ AVRO_FLOAT
Definition: Types.hh:37
Generic container for Avro fixed.
Definition: GenericDatum.hh:512
size_t index(const std::string &symbol) const
Returns the cardinal for the given symbol symbol.
Definition: GenericDatum.hh:472
size_t currentBranch() const
Returns the index of the current branch.
Definition: GenericDatum.hh:253
size_t unionBranch() const
Returns the index of the current branch, if this is a union.
Definition: GenericDatum.hh:587
size_t set(const std::string &symbol)
Set the value for this enum corresponding to the given symbol symbol.
Definition: GenericDatum.hh:479
@ AVRO_BOOL
Definition: Types.hh:39
Generic container for unions.
Definition: GenericDatum.hh:236
@ AVRO_STRING
Definition: Types.hh:33
LogicalType logicalType() const
The avro logical type that augments the main data type this datum holds.
Definition: GenericDatum.hh:553
void set(size_t n)
Set the value for this enum corresponding to the given cardinal n.
Definition: GenericDatum.hh:486
Generic container for Avro enum.
Definition: GenericDatum.hh:435
Value & value()
Returns the reference to the contents of this array.
Definition: GenericDatum.hh:389
@ AVRO_BYTES
Definition: Types.hh:34
GenericEnum(const NodePtr &schema)
Constructs a generic enum corresponding to the given schema schema, which should be of Avro type enum...
Definition: GenericDatum.hh:451
const GenericDatum & datum() const
Returns the datum corresponding to the currently selected branch in this union.
Definition: GenericDatum.hh:278
size_t value() const
Returns the cardinal for the current value of this enum.
Definition: GenericDatum.hh:497
GenericArray(const NodePtr &schema)
Constructs a generic array corresponding to the given schema schema, which should be of Avro type arr...
Definition: GenericDatum.hh:376
The generic container for Avro maps.
Definition: GenericDatum.hh:400
Generic datum which can hold any Avro type.
Definition: GenericDatum.hh:61
const GenericDatum & fieldAt(size_t pos) const
Returns the field at the given position pos.
Definition: GenericDatum.hh:341
Definition: LogicalType.hh:28
void setFieldAt(size_t pos, const GenericDatum &v)
Replaces the field at the given position pos with v.
Definition: GenericDatum.hh:356
GenericDatum(float v)
Makes a new AVRO_FLOAT datum whose value is of type float.
Definition: GenericDatum.hh:155
void selectBranch(size_t branch)
Selects a new branch in the union if this is a union.
Definition: GenericDatum.hh:595
Value & value()
Returns the reference to the contents of this map.
Definition: GenericDatum.hh:424
const Value & value() const
Returns the contents of this array.
Definition: GenericDatum.hh:382
GenericDatum & fieldAt(size_t pos)
Returns the reference to the field at the given position pos, which can be used to change the content...
Definition: GenericDatum.hh:349
GenericDatum(bool v)
Makes a new AVRO_BOOL datum whose value is of type bool.
Definition: GenericDatum.hh:137
@ AVRO_INT
Definition: Types.hh:35
A bunch of templates and specializations for encoding and decoding specific types.
Definition: AvroParse.hh:30
GenericUnion(const NodePtr &schema)
Constructs a generic union corresponding to the given schema schema, and the given value.
Definition: GenericDatum.hh:246
bool hasField(const std::string &name) const
Returns true if a field with the given name name is located in this r false otherwise.
Definition: GenericDatum.hh:318
GenericDatum(const std::string &v)
Makes a new AVRO_STRING datum whose value is of type std::string.
Definition: GenericDatum.hh:167
A ValidSchema is basically a non-mutable Schema that has passed some minimum of sanity checks.
Definition: ValidSchema.hh:40
@ AVRO_ARRAY
Definition: Types.hh:44
@ AVRO_DOUBLE
Definition: Types.hh:38
std::vector< uint8_t > & value()
Returns the reference to the contents of this fixed.
Definition: GenericDatum.hh:536
@ AVRO_FIXED
Definition: Types.hh:47
The generic container for Avro records.
Definition: GenericDatum.hh:286
GenericDatum(const std::vector< uint8_t > &v)
Makes a new AVRO_BYTES datum whose value is of type std::vector<uint8_t>.
Definition: GenericDatum.hh:174
GenericMap(const NodePtr &schema)
Constructs a generic map corresponding to the given schema schema, which should be of Avro type map.
Definition: GenericDatum.hh:411
const T & value() const
Returns the value held by this datum.
Definition: GenericDatum.hh:576
GenericDatum(const NodePtr &schema, const T &v)
Constructs a datum corresponding to the given avro type and set the value.
Definition: GenericDatum.hh:193
GenericContainer(Type type, const NodePtr &s)
Constructs a container corresponding to the given schema.
Definition: GenericDatum.hh:222
const Value & value() const
Returns the contents of this map.
Definition: GenericDatum.hh:417
size_t fieldIndex(const std::string &name) const
Returns index of the field with the given name name.
Definition: GenericDatum.hh:306
Type
The "type" for the schema.
Definition: Types.hh:31
size_t fieldCount() const
Returns the number of fields in the current record.
Definition: GenericDatum.hh:299
The generic container for Avro arrays.
Definition: GenericDatum.hh:365
@ AVRO_UNION
Definition: Types.hh:46
GenericDatum()
Makes a new AVRO_NULL datum.
Definition: GenericDatum.hh:132
The base class for all generic type for containers.
Definition: GenericDatum.hh:214
GenericFixed(const NodePtr &schema)
Constructs a generic enum corresponding to the given schema schema, which should be of Avro type fixe...
Definition: GenericDatum.hh:520
std::vector< std::pair< std::string, GenericDatum > > Value
The contents type for the map.
Definition: GenericDatum.hh:405
GenericDatum & datum()
Returns the datum corresponding to the currently selected branch in this union.
Definition: GenericDatum.hh:270
@ AVRO_MAP
Definition: Types.hh:45
const std::string & symbol(size_t n)
Returns the symbol corresponding to the cardinal n.
Definition: GenericDatum.hh:461
Wrapper for std::runtime_error that provides convenience constructor for boost::format objects.
Definition: Exception.hh:31
const std::string & symbol() const
Returns the symbol for the current value of this enum.
Definition: GenericDatum.hh:504