-
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
Union of Enum and String types throws an error #345
Comments
Did some debugging and narrowed in on the following function: WrappedUnionType.prototype._write = function (tap, val) {
var index, keys, name;
if (val === null) {
index = this._branchIndices["null"];
if (index === undefined) {
throwInvalidError(val, this)
}
tap.writeLong(index)
} else {
keys = Object.keys(val);
if (keys.length === 1) {
name = keys[0];
index = this._branchIndices[name]
}
if (index === undefined) {
throwInvalidError(val, this)
}
tap.writeLong(index);
this.types[index]._write(tap, val[name])
}
}; This function/method throws an error for unions with |
Came up with a workaround. I abandoned trying to get a union of types Updated the // add a new regex pattern specific to enum symbols that matches names and the empty string
var SYMBOL_PATTERN = /^$|^[A-Za-z_][A-Za-z0-9_]*$/;
.
.
.
// add a new function to validate symbol names
/** Check whether a string is a valid Avro enum symbol identifier.
function isValidSymbol(str) { return SYMBOL_PATTERN.test(str); }
.
.
.
module.exports = {
.
.
.
isValidSymbol: isValidSymbol
.
.
.
}; And finally, update the function EnumType(schema, opts) {
Type.call(this, schema, opts);
if (!Array.isArray(schema.symbols) || !schema.symbols.length) {
throw new Error(f("invalid enum symbols: %j", schema.symbols))
}
this.symbols = Object.freeze(schema.symbols.slice());
this._indices = {};
this.symbols.forEach(function (symbol, i) {
if (!utils.isValidSymbol(symbol)) {
throw new Error(f("invalid %s symbol: %j", this, symbol))
}
if (this._indices[symbol] !== undefined) {
throw new Error(f("duplicate %s symbol: %j", this, symbol))
}
this._indices[symbol] = i
}, this);
this._branchConstructor = this._createBranchConstructor();
Object.freeze(this)
} Again, this is a hack but it will suffice until the issue with the union of |
Unions with Here are examples of valid values for the union above: const str = {string: ''}; // Empty string.
const day = {WeekDay: 'monday'}; // Enum value. Take a look at this comment for more context as well as a couple examples. |
Thank you for the solutions! The explanation about logical types from the comment you referenced should really come in handy for more complex types. |
I created an avro schema with an enumerated type, but in some cases, JSON data for the property associated with the type is set as an empty string. I figured I could work around that by creating a union as follows:
But I got the following error when parsing the source data, where the
weekday
property was set to an empty string:Is that the intended behavior?
I read the Avro spec and it appears that it should be possible to create a union of types
enum
andstring
.The text was updated successfully, but these errors were encountered: