This is an automated email from the ASF dual-hosted git repository. marat pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-karavan.git
The following commit(s) were added to refs/heads/main by this push: new 6ab97595 Implementation of #631 (#637) 6ab97595 is described below commit 6ab9759506801260889ab2139bbcb5acf886645c Author: Artur Gubaidullin <artur.gubaidul...@gmail.com> AuthorDate: Fri Jan 27 13:42:58 2023 -0500 Implementation of #631 (#637) * Added functional hasElementWithId() method. * Optimized the type matching for Arrays * Removed redundant console log --- karavan-core/src/core/api/CamelDefinitionApiExt.ts | 25 +++++++++++++++++++--- karavan-core/test/findStep.spec.ts | 1 + 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/karavan-core/src/core/api/CamelDefinitionApiExt.ts b/karavan-core/src/core/api/CamelDefinitionApiExt.ts index 22057e6e..ec88228f 100644 --- a/karavan-core/src/core/api/CamelDefinitionApiExt.ts +++ b/karavan-core/src/core/api/CamelDefinitionApiExt.ts @@ -27,7 +27,6 @@ import { Integration } from "../model/IntegrationDefinition"; import {CamelDefinitionApi} from "./CamelDefinitionApi"; -import {CamelDefinitionYaml} from "./CamelDefinitionYaml"; export class ChildElement { name: string = '' @@ -149,8 +148,28 @@ export class CamelDefinitionApiExt { } static hasElementWithId = (integration: Integration, id: string): boolean => { - const yaml = CamelDefinitionYaml.integrationToYaml(integration); - return yaml.includes("id: " + id); + let hasId = false; + return CamelDefinitionApiExt.checkIfHasId(integration, id, hasId); + } + + static checkIfHasId = (obj: Object, id: string, hasId: boolean): boolean => { + + Object.keys(obj).forEach( (propName) => { + let prop = (obj as any)[propName]; + if (hasId || (propName === 'id' && id === prop)) { + hasId = true; + return true; + } + else if (typeof prop === 'object' && prop !== null) { + hasId = CamelDefinitionApiExt.checkIfHasId(prop, id, hasId); + } + else if (Array.isArray(prop)) { + prop.forEach((element) => { + CamelDefinitionApiExt.checkIfHasId(element, id, hasId); + }); + } + }); + return hasId; } static moveRouteElement = (integration: Integration, source: string, target: string, asChild: boolean): Integration => { diff --git a/karavan-core/test/findStep.spec.ts b/karavan-core/test/findStep.spec.ts index 5515f57b..b1a98fc0 100644 --- a/karavan-core/test/findStep.spec.ts +++ b/karavan-core/test/findStep.spec.ts @@ -105,6 +105,7 @@ describe('Find Step', () => { const res1 = CamelDefinitionApiExt.hasElementWithId(i, 'to-6a8b'); const res2 = CamelDefinitionApiExt.hasElementWithId(i, 'to-6a81'); + expect(res1).to.equal(true); expect(res2).to.equal(false); });