Class GenericDatum
GenericDatum which can hold any Avro type. The datum has a type and a value.
class GenericDatum
;
The type is one of the Avro data types. The D type for value corresponds to the Avro type.
- An avro null
corresponds to no D type. It is illegal to try to access values for null
.
- Avro boolean
maps to D bool
- Avro int
maps to D int
.
- Avro long
maps to D long
.
- Avro float
maps to D float
.
- Avro double
maps to D double
.
- Avro string
maps to D string
.
- Avro bytes
maps to D ubyte[]
.
- Avro fixed
maps to D class GenericFixed
.
- Avro enum
maps to D class GenericEnum
.
- Avro array
maps to D class GenericArray
.
- Avro map
maps to D class GenericMap
.
- There is no D type corresponding to Avro union
. The object should have the D type
corresponding to one of the constituent types of the union.
Each GenericDatum holds a value which is set using the
method and retrieved
via the `.getValue!T() method. Because a GenericDatum can store any type, the caller must provide
the desired type while calling
, and this type must match the type of the schema.
Schema schema = parser .parseText(q"EOS
{"namespace": "example.avro",
"type": "record",
"name": "User",
"fields": [
{"name": "name", "type": "string"},
{"name": "favorite_number", "type": ["int", "null"]},
{"name": "scores", "type": {"type": "array", "items": "float"}},
{"name": "m", "type": {"type": "map", "values": "long"}}
]
}
EOS");
// Initializes the GenericDatum according to the schema with default values.
GenericDatum datum = new GenericDatum(schema);
assert(datum .getType == Type .RECORD);
// Primitive values can be set and retrieved.
datum .getValue!(GenericRecord) .getField("name") .setValue("bob");
// Convenience shortcut using opIndex() and opAssign() for primitive types.
datum["name"] = "bob";
assert(datum["name"] .getValue!string == "bob");
// Unions have convenience functions directly on GenericData.
datum["favorite_number"] .setUnionIndex(0);
assert(datum["favorite_number"] .getUnionIndex() == 0);
// Arrays also have convenience functions.
datum["scores"] ~= 1.23f;
datum["scores"] ~= 4.56f;
assert(datum["scores"] .length == 2);
// Maps do as well.
datum["m"]["m1"] = 10L;
datum["m"]["m2"] = 20L;
assert(datum["m"]["m1"] .getValue!long == 10L);
Constructors
Name | Description |
---|---|
this
()
|
Makes a new NULL GenericDatum. |
this
(val)
|
A constructor allowing GenericDatum to be created for primitive schemas from D equivalents. |
Methods
Name | Description |
---|---|
getUnionIndex
()
|
Returns the index of the current branch, if this is a union. |
getValue
()
|
Returns the value held by this datum. |
isUnion
()
|
Returns true if an only if this datum is a union. |
length
()
|
A shortcut for .getValue!(GenericType).getValue().length where GenericType is one of GenericArray or GenericMap. |
opAssign
(val)
|
Sets the value of the GenericDatum to a value corresponding with its type. |
opCast
()
|
Implementing opCast allows values to be retrieved using std .
|
opIndex
(name)
|
For records/maps, looks up a record with name .
|
opIndexAssign
(val, name)
|
For records/maps, assign a value to a given key. |
opIndexAssign
(val, idx)
|
For arrays, assign a value to a given index. |
setUnionIndex
(branch)
|
Selects a new branch in the union if this is a union. |
setValue
(val)
|
Sets the value of the GenericDatum to a value corresponding with its type. |