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

feat: allow named types in unions #469

Merged
merged 24 commits into from
Sep 29, 2024
Merged
Changes from 3 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class Type {
this.name = undefined;
this.aliases = undefined;
this.doc = (schema && schema.doc) ? '' + schema.doc : undefined;
this.opts = Object.assign({}, opts);

if (schema) {
// This is a complex (i.e. non-primitive) type.
Expand Down Expand Up @@ -351,7 +352,7 @@ class Type {
// Group types by category, similar to the logic for unwrapped unions.
let bucketized = {};
expanded.forEach((type) => {
let bucket = getTypeBucket(type);
let bucket = getTypeBucket(type, true);
let bucketTypes = bucketized[bucket];
if (!bucketTypes) {
bucketized[bucket] = bucketTypes = [];
Expand Down Expand Up @@ -1258,7 +1259,7 @@ class UnwrappedUnionType extends UnionType {
}

_getIndex (val) {
let index = this._bucketIndices[getValueBucket(val)];
let index = this._bucketIndices[getValueBucket(val, this.opts.namespace)];
if (this._dynamicBranches) {
// Slower path, we must run the value through all branches.
index = this._getBranchIndex(val, index);
Expand Down Expand Up @@ -3027,8 +3028,9 @@ function maybeQualify(name, ns) {
* Get a type's bucket when included inside an unwrapped union.
*
* @param type {Type} Any type.
* @param isExpanded {Boolean} Whether the union is expanded - if yes, we don't use the name to determine the bucket, as the parent type is already in the right bucket.
*/
function getTypeBucket(type) {
function getTypeBucket(type, isExpanded) {
let typeName = type.typeName;
switch (typeName) {
case 'double':
Expand All @@ -3043,8 +3045,13 @@ function getTypeBucket(type) {
return 'string';
case 'map':
case 'error':
joscha marked this conversation as resolved.
Show resolved Hide resolved
case 'record':
return 'object';
case 'record':
if (isExpanded || !type.name) {
return 'object';
} else {
return maybeQualify(type.name, type.namespace)
}
default:
return typeName;
}
Expand All @@ -3054,8 +3061,9 @@ function getTypeBucket(type) {
* Infer a value's bucket (see unwrapped unions for more details).
*
* @param val {...} Any value.
* @param namespace {String} Optional namespace.
*/
function getValueBucket(val) {
function getValueBucket(val, namespace) {
if (val === null) {
return 'null';
}
Expand All @@ -3066,6 +3074,12 @@ function getValueBucket(val) {
return 'array';
} else if (Buffer.isBuffer(val)) {
return 'buffer';
} else if(typeof val.constructor !== 'undefined' && val.constructor.name) {
joscha marked this conversation as resolved.
Show resolved Hide resolved
const isBuiltin = Object.values(module.exports.builtins).some(b => val instanceof b);
if (isBuiltin || val.constructor === Object) {
return bucket;
}
return maybeQualify(val.constructor.name, namespace);
}
}
return bucket;
Expand Down