Skip to content

Commit

Permalink
add array of nested objects with guaranteed order
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgrisham committed Dec 26, 2023
1 parent 4436cc7 commit 833d56c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
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

0 comments on commit 833d56c

Please sign in to comment.