UNPKG

@itwin/ecschema-metadata

Version:

ECObjects core concepts in typescript

52 lines 2.36 kB
/*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ import { SchemaItemType } from "../ECObjects"; import { ECClass } from "../Metadata/Class"; import { SchemaPartVisitorDelegate } from "../SchemaPartVisitorDelegate"; /** * The purpose of this class is to traverse a given schema, allowing clients to hook into * the traversal process via Visitors to allow for custom processing of the schema elements. * @internal */ export class SchemaWalker { _visitorHelper; // This is a cache of the schema we are traversing. The schema also exists within the _context but in order // to not have to go back to the context every time we use this cache. _schema; /** * Initializes a new SchemaWalker instance. * @param visitor An ISchemaWalkerVisitor implementation whose methods will be called during schema traversal. */ constructor(visitor) { this._visitorHelper = new SchemaPartVisitorDelegate(visitor); } /** * Traverses the given Schema, calling ISchemaWalkerVisitor methods along the way. * @param schema The Schema to traverse. */ async traverseSchema(schema) { this._schema = schema; await this._visitorHelper.visitSchema(schema); await this._visitorHelper.visitSchemaPart(schema); for (const item of this._schema.getItems()) await this.traverseSchemaItem(item); return schema; } async traverseSchemaItem(schemaItem) { await this._visitorHelper.visitSchemaPart(schemaItem); if (ECClass.isECClass(schemaItem)) await this.traverseClass(schemaItem); } async traverseClass(ecClass) { for (const property of await ecClass.getProperties(true)) { await this._visitorHelper.visitSchemaPart(property); } if (ecClass.schemaItemType === SchemaItemType.RelationshipClass) { await this._visitorHelper.visitSchemaPart(ecClass.source); await this._visitorHelper.visitSchemaPart(ecClass.target); } } } //# sourceMappingURL=SchemaWalker.js.map