79438101

Date: 2025-02-14 01:03:13
Score: 0.5
Natty:
Report link

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();

Reasons:
  • Blacklisted phrase (0.5): I need
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: 0Nicholas