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

add array of nested objects with guaranteed order #1957

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,7 @@ export abstract class NamespaceBase extends ReflectionObject {

/** Nested objects of this namespace as an array for iteration. */
public readonly nestedArray: ReflectionObject[];
public readonly orderedNestedMessages: ReflectionObject[];

/**
* Converts this namespace to a namespace descriptor.
Expand Down
7 changes: 7 additions & 0 deletions src/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ function Namespace(name, options) {
* @private
*/
this._nestedArray = null;

/**
* Array of nested messages in the order they were parsed from the schema.
* @type {ReflectionObject[]}
*/
this.orderedNestedMessages = null;
}

function clearCache(namespace) {
Expand Down Expand Up @@ -239,6 +245,7 @@ Namespace.prototype.add = function add(object) {
throw Error("duplicate name '" + object.name + "' in " + this);
}
}

this.nested[object.name] = object;
object.onAdd(this);
return clearCache(this);
Expand Down
8 changes: 7 additions & 1 deletion src/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,12 +306,18 @@ Root.prototype._handleAdd = function _handleAdd(object) {

} else if (!(object instanceof OneOf)) /* everything else is a namespace */ {

if (object instanceof Type) // Try to handle any deferred extensions
if (object instanceof Type) { // Try to handle any deferred extensions
if (!object.parent.orderedNestedMessages) {
object.parent.orderedNestedMessages = [];
}
object.parent.orderedNestedMessages.push(object);

for (var i = 0; i < this.deferred.length;)
if (tryHandleExtension(this, this.deferred[i]))
this.deferred.splice(i, 1);
else
++i;
}
for (var j = 0; j < /* initializes */ object.nestedArray.length; ++j) // recurse into the namespace
this._handleAdd(object._nestedArray[j]);
if (exposeRe.test(object.name))
Expand Down