Skip to content
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

ambiguous unwrapped union #52

Closed
Osmosis311 opened this issue May 19, 2016 · 8 comments
Closed

ambiguous unwrapped union #52

Osmosis311 opened this issue May 19, 2016 · 8 comments
Labels

Comments

@Osmosis311
Copy link

I get an error that says "ambiguous unwrapped union" when I try process one of my company's Avro log files.

Is there any way to somehow support this?

Thanks!

@mtth
Copy link
Owner

mtth commented May 19, 2016

Yes, you can use parse's wrapUnions option. For example:

var type = avro.parse(['int', 'double'], {wrapUnions: true});

This will change the decoded representation of the union's branches to include their type: this is required to not risk corrupting data since Avro types don't map perfectly to JavaScript types. Continuing the above example:

type.isValid(12); // false
type.isValid({int: 12}); // true
type.isValid({double: 48}); // true

Finally, the wrapUnions option will apply to all unions in your schema. If you wish to wrap only certain unions, you can do so by instantiating the unions directly using parse's typeHook option instead (perhaps even implementing your own union type).

Does this answer your question? You can also read more about which unions are ambiguous and why on the API page. The issues referenced in #42 might be helpful too.

@mtth mtth added the question label May 19, 2016
@Osmosis311
Copy link
Author

Thanks for the fast response!

I'm not sure this does answer the question, or, more specifically, I need some more help.

I have an Avro data file which contains both data and schema. I don't know the schema "ahead of time." Is there a way to pass the wrapUnions option to the file decoder somehow? Or, is the only way to use that option is if I know the schema before trying to process a data file?

Thanks.

@mtth
Copy link
Owner

mtth commented May 19, 2016

I see. Then you would still use the same option, applying it via BlockDecoder's parseHook option. For example, assuming you're reading a local file:

// Hook called when the schema inside a file gets parsed.
function hook(attrs) { return avro.parse(attrs, {wrapUnions: true}); }

// A readable stream where all unions are wrapped.
var readable = avro.createFileDecoder('data.avro', {parseHook: hook});

@Osmosis311
Copy link
Author

That worked! Thank you very, very much!!!

@Osmosis311
Copy link
Author

@mtth is there a way to use the code above, but from a buffer instead of from a file? Can't seem to figure that out....

Thanks!

@Osmosis311 Osmosis311 reopened this Jan 12, 2017
@mtth
Copy link
Owner

mtth commented Jan 14, 2017

Do you mean decoding a buffer with a single encoded value, decoding a buffer with an entire container file inside of it, something else? If the first, the example in my first reply above should work, if the second you can "wrap" the buffer in a readable stream and pipe it to a BlockDecoder.

@Osmosis311
Copy link
Author

It's a buffer containing a group of values (e.g. multiple "sets" of data).

Can you give me an example of that syntax? Is it something like:

var readable = avro.createBlockDecoder(buffer_value, {parseHook: hook});?

Thanks!

@Osmosis311
Copy link
Author

I got it! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants