After reading more code in the repo, I realized that Avro is using template class specialization. All I need to do is define the right encoding / decoding logic for the struct, and it will call it correctly.
template<> struct codec_traits<UserEntry> {
static void encode(Encoder& e, const UserEntry& user) {
avro::encode(e, user.user_id);
avro::encode(e, user.user_name);
...
...
...
}
static void decode(Decoder& d, UserEntry& user) {
avro::decode(d, user.user_id);
avro::decode(d, user.name);
...
...
...
}
}
};
Note: If UserEntry
is made of other struct types, they also need to have their encoders defined.
To write the data
avro::DataFileWriter<UserEntry> writer(file_name, schema);
UserEntry user;
...
// populate
...
writer.write(user);
writer.close();