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 825455a4 CamelDefinitionApiExt.findElementById
825455a4 is described below

commit 825455a421df2726da52ef92df02c27b8a5401f0
Author: Marat Gubaidullin <ma...@talismancloud.io>
AuthorDate: Wed Dec 18 20:06:33 2024 -0500

    CamelDefinitionApiExt.findElementById
---
 karavan-core/src/core/api/CamelDefinitionApiExt.ts | 21 +++++++++++++++++++++
 karavan-core/test/findStep.spec.ts                 | 17 ++++++++++++++++-
 2 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/karavan-core/src/core/api/CamelDefinitionApiExt.ts 
b/karavan-core/src/core/api/CamelDefinitionApiExt.ts
index 36c2032b..81818259 100644
--- a/karavan-core/src/core/api/CamelDefinitionApiExt.ts
+++ b/karavan-core/src/core/api/CamelDefinitionApiExt.ts
@@ -242,6 +242,27 @@ export class CamelDefinitionApiExt {
         return counter;
     };
 
+    static findElementById = (integration: Integration, id: string): 
CamelElement | undefined => {
+        return CamelDefinitionApiExt.findElementsById(integration, id, 
[])?.at(0);
+    };
+
+    static findElementsById = (obj: Object, id: string, elements: 
CamelElement[]): CamelElement[] => {
+        for (const propName in obj) {
+            let prop = (obj as any)[propName];
+            if (propName === 'id' && id === prop) {
+                elements.push(obj as CamelElement)
+                elements = CamelDefinitionApiExt.findElementsById(prop, id, 
elements);
+            } else if (typeof prop === 'object' && prop !== null) {
+                elements = CamelDefinitionApiExt.findElementsById(prop, id, 
elements);
+            } else if (Array.isArray(prop)) {
+                for (const element of prop) {
+                    elements = CamelDefinitionApiExt.findElementsById(element, 
id, elements);
+                }
+            }
+        }
+        return elements;
+    };
+
     static moveRouteElement = (integration: Integration, source: string, 
target: string, asChild: boolean,): Integration => {
         const sourceFindStep = 
CamelDefinitionApiExt.findElementMetaInIntegration(integration, source);
         const sourceStep = sourceFindStep.step;
diff --git a/karavan-core/test/findStep.spec.ts 
b/karavan-core/test/findStep.spec.ts
index 6fc0226e..ab593cad 100644
--- a/karavan-core/test/findStep.spec.ts
+++ b/karavan-core/test/findStep.spec.ts
@@ -97,7 +97,7 @@ describe('Find Step', () => {
         expect(log.message).to.equal(log2.message);
     });
 
-    it('Find Steps in YAML by Id', () => {
+    it('Find if Step exists YAML by Id', () => {
         const yaml = fs.readFileSync('test/findStep.yaml',{encoding:'utf8', 
flag:'r'});
         const i = CamelDefinitionYaml.yamlToIntegration("demo.yaml", yaml);
         const yaml2 = CamelDefinitionYaml.integrationToYaml(i);
@@ -109,4 +109,19 @@ describe('Find Step', () => {
         expect(res1).to.equal(1);
         expect(res2).to.equal(0);
     });
+
+    it('Find Steps in YAML by Id', () => {
+        const yaml = fs.readFileSync('test/findStep.yaml',{encoding:'utf8', 
flag:'r'});
+        const i = CamelDefinitionYaml.yamlToIntegration("demo.yaml", yaml);
+        const yaml2 = CamelDefinitionYaml.integrationToYaml(i);
+        expect(yaml.replaceAll("\r\n", "\n")).to.equal(yaml2); // replace for 
Windows compatibility
+
+        const res1 = CamelDefinitionApiExt.findElementById(i, 'to-6a8b');
+        const res2 = CamelDefinitionApiExt.findElementById(i, 
'otherwise-4843');
+        const res3 = CamelDefinitionApiExt.findElementById(i, 'to-6a81');
+
+        expect(res1?.dslName).to.equal('ToDefinition');
+        expect(res2?.dslName).to.equal('OtherwiseDefinition');
+        expect(res3).to.equal(undefined);
+    });
 });
\ No newline at end of file

Reply via email to