Skip to content

Commit

Permalink
feat(gh_112) - BaseVisualCamelEntity for Pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
tplevko authored and lordrip committed Sep 21, 2023
1 parent f9613d2 commit b5090ef
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 6 deletions.
2 changes: 2 additions & 0 deletions packages/ui/src/camel-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export * from './camel-to-tile.adapter';
export * from './camel-to-table.adapter';
export * from './camel-to-tabs.adapter';
export * from './is-camel-route';
export * from './is-kamelet-binding';
export * from './is-pipe';
15 changes: 15 additions & 0 deletions packages/ui/src/camel-utils/is-pipe.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { isPipe } from './is-pipe';
import { pipeJson } from '../stubs/pipe-route';

describe('isPipe', () => {
it.each([
[{ apiVersion: 'camel.apache.org/v1', kind: 'Pipe' }, true],
[pipeJson, true],
[undefined, false],
[null, false],
[true, false],
[false, false],
])('should mark %s as isPipe: %s', (route, result) => {
expect(isPipe(route)).toEqual(result);
});
});
13 changes: 13 additions & 0 deletions packages/ui/src/camel-utils/is-pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Pipe } from '@kaoto-next/camel-catalog/types';
import { isDefined } from '../utils';

/** Very basic check to determine whether this object is a Pipe */
export const isPipe = (rawEntity: unknown): rawEntity is Pipe => {
if (!isDefined(rawEntity) || Array.isArray(rawEntity) || typeof rawEntity !== 'object') {
return false;
}

return (
typeof rawEntity === 'object' && 'apiVersion' in rawEntity! && 'kind' in rawEntity! && rawEntity.kind == 'Pipe'
);
};
9 changes: 5 additions & 4 deletions packages/ui/src/hooks/entities.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { useCallback, useMemo, useState } from 'react';
import { parse } from 'yaml';
import { isCamelRoute } from '../camel-utils';
import { BaseCamelEntity, BaseVisualCamelEntity, CamelRoute, KameletBinding } from '../models/camel-entities';
import { isCamelRoute, isPipe, isKameletBinding } from '../camel-utils';
import { BaseCamelEntity, BaseVisualCamelEntity, CamelRoute, KameletBinding, Pipe } from '../models/camel-entities';
import { isDefined } from '../utils';
import { isKameletBinding } from '../camel-utils/is-kamelet-binding';

export const useEntities = () => {
const [sourceCode, setSourceCode] = useState<string>('');
Expand All @@ -20,7 +19,7 @@ export const useEntities = () => {
(acc, rawEntity) => {
const entity = getEntity(rawEntity);

if (entity instanceof CamelRoute || entity instanceof KameletBinding) {
if (entity instanceof CamelRoute || entity instanceof KameletBinding || entity instanceof Pipe) {
acc.visualEntities.push(entity);
} else if (isDefined(entity) && typeof entity === 'object') {
acc.entities.push(entity);
Expand Down Expand Up @@ -65,6 +64,8 @@ function getEntity(rawEntity: unknown): BaseCamelEntity | BaseVisualCamelEntity
return new CamelRoute(rawEntity.route);
} else if (isKameletBinding(rawEntity)) {
return new KameletBinding(rawEntity);
} else if (isPipe(rawEntity)) {
return new Pipe(rawEntity);
}

return rawEntity as BaseCamelEntity;
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/models/camel-entities/flows/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './kamelet-binding';
export * from './pipe';
export * from './route';
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ import { KameletBinding as KameletBindingModel } from '@kaoto-next/camel-catalog
import { v4 as uuidv4 } from 'uuid';
import { VisualizationNode } from '../../visualization';
import { BaseVisualCamelEntity, EntityType } from '../base-entity';
import { KameletBindingSink, KameletBindingSource, KameletBindingStep, KameletBindingSteps } from '../kamelet-binding';
import {
KameletBindingSink,
KameletBindingSource,
KameletBindingStep,
KameletBindingSteps,
} from '../kamelet-binding-overrides';

export class KameletBinding implements BaseVisualCamelEntity {
readonly id = uuidv4();
readonly type = EntityType.KameletBinding;
type = EntityType.KameletBinding;

constructor(public route: Partial<KameletBindingModel> = {}) {}

Expand Down
13 changes: 13 additions & 0 deletions packages/ui/src/models/camel-entities/flows/pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Pipe as PipeModel } from '@kaoto-next/camel-catalog/types';
import { v4 as uuidv4 } from 'uuid';
import { BaseVisualCamelEntity, EntityType } from '../base-entity';
import { KameletBinding } from './kamelet-binding';

export class Pipe extends KameletBinding implements BaseVisualCamelEntity {
readonly id = uuidv4();
type = EntityType.Pipe;

constructor(public route: Partial<PipeModel> = {}) {
super();
}
}
66 changes: 66 additions & 0 deletions packages/ui/src/stubs/pipe-route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Pipe } from '../models/camel-entities';

/**
* This is a stub Pipe in YAML format.
* It is used to test the Canvas component.
*/
export const pipeYaml = `
apiVersion: camel.apache.org/v1
kind: Pipe
metadata:
name: webhook-binding
spec:
source:
ref:
kind: Kamelet
apiVersion: camel.apache.org/v1
name: webhook-source
steps:
- ref:
kind: Kamelet
apiVersion: camel.apache.org/v1
name: delay-action
sink:
ref:
kind: Kamelet
apiVersion: camel.apache.org/v1
name: log-sink`;

/**
* This is a stub Pipe in JSON format.
* It is used to test the Canvas component.
*/
export const pipeJson = {
apiVersion: 'camel.apache.org/v1',
kind: 'Pipe',
metadata: {
name: 'webhook-binding',
},
spec: {
source: {
ref: {
kind: 'Kamelet',
apiVersion: 'camel.apache.org/v1',
name: 'webhook-source',
},
},
steps: [
{
ref: {
kind: 'Kamelet',
apiVersion: 'camel.apache.org/v1',
name: 'delay-action',
},
},
],
sink: {
ref: {
kind: 'Kamelet',
apiVersion: 'camel.apache.org/v1alpha1',
name: 'log-sink',
},
},
},
};

export const pipe = new Pipe(pipeJson);

0 comments on commit b5090ef

Please sign in to comment.