-
Notifications
You must be signed in to change notification settings - Fork 148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unions of Complex Objects #174
Comments
Hey @cisaacson,
Yes to both questions. Unwrapped and wrapped unions are
I think both options are valid but have slightly different trade-offs; for example:
It's hard for me to say much without knowing more about your use-case; but I would recommend keeping things as simple as possible: starting with wrapped unions and introducing logical types if/when you have an explicit need for them (e.g. when you are certain the convenience outweighs the complexity). |
Matthiue, thanks for the quick response.
To make things easier for finding the type to wrap I added a field like this to each record that occurs in ambiguous unions:
Then it's in the record itself, and can easily be found searching for The avsc library is really great work, it's well designed and very impressive! |
We are using avsc to serialize/deserialize to Avro objects using NodeJS and C++, your library works very well. I am working only on the NodeJS side so far. We have a need to support unions of record types. I read another issue that suggested using either Wrapped types, or a custom LogicalType. I have a couple of questions related to the best approach for this:
fromBuffer
method also returns wrapped unions. First question:If we used wrapped unions, is that just internal to
avsc
related code in Javascript? Would a C++ Avro implementation using codegen be able to read the normal union serialization?Thanks in advance for any feedback, great job on the module.
`
const avro = require('avsc');
interface A {
amount: number,
dec_amount: Decimal
}
interface B {
user_name: string,
email: string
}
interface C {
a_or_b: {A: A}|{B: B}
}
const schemaA: any = {
name: 'A',
type: 'record',
fields: [
{name: 'amount', type: 'int'},
{name: 'dec_amount', type: {type: 'bytes', logicalType: 'decimal', precision: 9, scale: 2}}
]
}
const schemaB: any = {
name: 'B',
type: 'record',
fields: [
{name: 'user_name', type: 'string'},
{name: 'email', type: 'string'}
]
}
const avroTypeA: any = avroWrapper.forSchema(schemaA);
const avroTypeB: any = avroWrapper.forSchema(schemaB);
const schemaC: any = {
name: 'C',
type: 'record',
fields: [
{
name: 'a_or_b',
type: [ schemaA, schemaB]
}
]
}
const avroTypeC: any = avroWrapper.forSchema(schemaC);
const dataA: A = {
amount: 5,
dec_amount: new Decimal(12.0)
}
const dataB: B = {
user_name: 'joe',
email: '[email protected]'
}
const dataC_A: C = {
a_or_b: {A: dataA}
}
const dataC_B: C = {
a_or_b: {B: dataB}
}
const bufC_B: Buffer = avroTypeC.toBuffer(dataC_B);
const bufC_A: Buffer = avroTypeC.toBuffer(dataC_A);
const dataC_BResult: C = avroTypeC.fromBuffer(bufC_B);
`
The text was updated successfully, but these errors were encountered: