-
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
Difficulty serializing recursive structure with unions of record types. #129
Comments
Hey, good question, this is definitely one of the less intuitive parts of
However, in this case, the union contains two different const wrappedRecursiveNodeData = {
value: {A: nodeValueA}, // Wrapped here.
children: {
childOne: {
value: {B: nodeValueB}, // And here.
children: {}
},
childTwo: {
value: {A: nodeValueA}, // And here.
children: {}
}
}
};
failingRecursiveNodeType.toBuffer(wrappedRecursiveNodeData); // OK. (You can control how At this point you might be wondering whether there is a way to avoid wrapping values since it's easy to tell which type they belong to from their definition (via their class RecursiveType extends avsc.types.LogicalType {
_fromValue(val) {
// This is the easy case, we convert the wrapped value to our representation.
return {
value: val.value.unwrap(),
children: val.children
};
}
_toValue(any) {
// Here we do a little more work to wrap the value based on its `type` field.
return {
value: {[any.value.type === 'TYPE_A' ? 'A' : 'B']: any.value},
children: any.children
};
}
}
const recursiveNodeType = avsc.Type.forSchema({
type: 'record',
name: 'R',
logicalType: 'recursive', // Apply our logical type, its name here...
fields: [
{ name: 'value', type: [nodeValueTypeA, nodeValueTypeB] },
{ name: 'children', type: { type: 'map', values: 'R' } }
]
}, {logicalTypes: {recursive: RecursiveType}}); // ...must match the key here.
recursiveNodeType.toBuffer(goalRecursiveNodeData) // Works! Would either of these solutions work out for your use-case? |
Thank you very much for the excellent description and sorry for the delay in replying. It looks like Logical Types will be exactly what we need. Cheers! |
This was very helpful, in my case. I think it would be great to include it in the documentation, somewhere. |
First of all, thanks for this project; it's an extremely useful library.
I'm a bit new to Avro so my apologies if this problem is a function of my ignorance about it.
I'm trying to create a schema that describes a simple recursive tree structure where the contents of each node in the tree contain one of some known set of records (contained in
value
fields in example below). With just one type in this union (workingRecursiveNodeType
below), serialization works fine. However, adding an additional type (failingRecursiveNodeType
below) causes what appear to be schema violations.Am I doing something wrong in declaring that a field can have a union of record types? If so, is there another way to accomplish this?
Example:
The text was updated successfully, but these errors were encountered: