Skip to content

Commit

Permalink
fix(serialzers): Add types
Browse files Browse the repository at this point in the history
  • Loading branch information
mmelko committed Dec 12, 2024
1 parent 3a34678 commit 21a50ff
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 15 deletions.
5 changes: 4 additions & 1 deletion packages/ui/src/models/camel/camel-k-resource-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import {
} from '@kaoto/camel-catalog/types';

export class CamelKResourceFactory {
static getCamelKResource(json?: unknown, type?: SourceSchemaType): CamelResource | undefined {
static getCamelKResource(
json?: IntegrationType | IKameletDefinition | KameletBindingType | PipeType,
type?: SourceSchemaType,
): CamelResource | undefined {
const jsonRecord = json ? (json as Record<string, unknown>) : {};

if ((jsonRecord && typeof json === 'object' && 'kind' in jsonRecord) || type) {
Expand Down
11 changes: 8 additions & 3 deletions packages/ui/src/models/camel/camel-resource-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { CamelResource } from './camel-resource';
import { CamelResourceSerializer, XmlCamelResourceSerializer, YamlCamelResourceSerializer } from '../../serializers';
import { CamelRouteResource } from './camel-route-resource';
import { CamelKResourceFactory } from './camel-k-resource-factory';
import { CamelYamlDsl, Integration, KameletBinding, Pipe } from '@kaoto/camel-catalog/types';
import { IKameletDefinition } from '../kamelets-catalog';

export class CamelResourceFactory {
/**
Expand All @@ -18,10 +20,13 @@ export class CamelResourceFactory {
? new XmlCamelResourceSerializer()
: new YamlCamelResourceSerializer();

const parsedCode = source ? serializer.parse(source) : source;
const resource = CamelKResourceFactory.getCamelKResource(parsedCode, type);
const parsedCode = typeof source === 'string' ? serializer.parse(source) : source;
const resource = CamelKResourceFactory.getCamelKResource(
parsedCode as Integration | KameletBinding | Pipe | IKameletDefinition,
type,
);

if (resource) return resource;
return new CamelRouteResource(parsedCode, serializer);
return new CamelRouteResource(parsedCode as CamelYamlDsl, serializer);
}
}
11 changes: 7 additions & 4 deletions packages/ui/src/models/camel/camel-route-resource.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RouteDefinition } from '@kaoto/camel-catalog/types';
import { CamelYamlDsl, RouteDefinition } from '@kaoto/camel-catalog/types';
import { TileFilter } from '../../components/Catalog';
import { createCamelPropertiesSorter, isDefined } from '../../utils';
import { CatalogKind } from '../catalog-kind';
Expand Down Expand Up @@ -50,11 +50,11 @@ export class CamelRouteResource implements CamelResource, BeansAwareResource {
private resolvedEntities: BaseVisualCamelEntityDefinition | undefined;
private serializer: CamelResourceSerializer;

constructor(code?: unknown, serializer?: CamelResourceSerializer) {
constructor(rawEntities?: CamelYamlDsl, serializer?: CamelResourceSerializer) {
this.serializer = serializer ?? new YamlCamelResourceSerializer();
if (!code) return;
if (!rawEntities) return;

const entities = Array.isArray(code) ? code : [code];
const entities = Array.isArray(rawEntities) ? rawEntities : [rawEntities];
this.entities = entities.reduce((acc, rawItem) => {
const entity = this.getEntity(rawItem);
if (isDefined(entity) && typeof entity === 'object') {
Expand Down Expand Up @@ -96,7 +96,10 @@ export class CamelRouteResource implements CamelResource, BeansAwareResource {
getSerializer(): CamelResourceSerializer {
return this.serializer;
}

setSerializer(serializer: CamelResourceSerializer): void {
// Preserve comments
serializer.setComments(this.serializer.getComments());
this.serializer = serializer;
}

Expand Down
5 changes: 4 additions & 1 deletion packages/ui/src/serializers/camel-resource-serializer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { CamelResource } from '../models/camel';
import { CamelYamlDsl, Integration, Kamelet, KameletBinding, Pipe } from '@kaoto/camel-catalog/types';

export interface CamelResourceSerializer {
parse: (code: string) => unknown;
parse: (code: string) => CamelYamlDsl | Integration | Kamelet | KameletBinding | Pipe;
serialize: (resource: CamelResource) => string;
getComments: () => string[];
setComments: (comments: string[]) => void;
}
11 changes: 10 additions & 1 deletion packages/ui/src/serializers/xml-camel-resource-serializer.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { CamelResource } from '../models/camel';
import { CamelResourceSerializer } from './camel-resource-serializer';
import { CamelYamlDsl, Integration, Kamelet, KameletBinding, Pipe } from '@kaoto/camel-catalog/types';

export class XmlCamelResourceSerializer implements CamelResourceSerializer {
static isApplicable(_code: unknown): boolean {
return false;
}

parse(_code: unknown): unknown {
parse(_code: string): CamelYamlDsl | Integration | Kamelet | KameletBinding | Pipe {
//TODO implement
return {};
}
Expand All @@ -15,4 +16,12 @@ export class XmlCamelResourceSerializer implements CamelResourceSerializer {
//TODO implement
return '';
}

getComments(): string[] {
return [];
}

setComments(_comments: string[]): void {
//TODO implement
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { YamlCamelResourceSerializer } from './yaml-camel-resource-serializer';
import { camelRouteJson, camelRouteYaml } from '../stubs';
import { CamelRouteResource } from '../models/camel';
import { CamelYamlDsl } from '@kaoto/camel-catalog/types';

describe('YamlCamelResourceSerializer', () => {
let serializer: YamlCamelResourceSerializer;
Expand All @@ -22,11 +23,10 @@ describe('YamlCamelResourceSerializer', () => {

it('includes comments in serialized YAML string', () => {
const entities = serializer.parse('# comment1\n' + camelRouteYaml);
console.log(serializer.comments);
expect(serializer.comments.includes('# comment1')).toBeTruthy();

serializer.comments.push('# Comment2');
const result = serializer.serialize(new CamelRouteResource(entities));
const result = serializer.serialize(new CamelRouteResource(entities as CamelYamlDsl));
expect(result).toContain('# Comment2');
});
});
15 changes: 12 additions & 3 deletions packages/ui/src/serializers/yaml-camel-resource-serializer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CamelResource } from '../models/camel';
import { parse, stringify } from 'yaml';
import { CamelResourceSerializer } from './camel-resource-serializer';
import { CamelYamlDsl, Integration, Kamelet, KameletBinding, Pipe } from '@kaoto/camel-catalog/types';

export class YamlCamelResourceSerializer implements CamelResourceSerializer {
/**
Expand All @@ -13,7 +14,7 @@ export class YamlCamelResourceSerializer implements CamelResourceSerializer {
* ```
* The regular expression should match the first three lines
*/
COMMENTED_LINES_REGEXP = /^\s*#.*$/;
static COMMENTED_LINES_REGEXP = /^\s*#.*$/;
comments: string[] = [];

static isApplicable(_code: unknown): boolean {
Expand All @@ -23,7 +24,7 @@ export class YamlCamelResourceSerializer implements CamelResourceSerializer {
return true;
}

parse(code: string): unknown {
parse(code: string): CamelYamlDsl | Integration | Kamelet | KameletBinding | Pipe {
if (!code || typeof code !== 'string') return [];

this.comments = this.parseComments(code);
Expand All @@ -40,11 +41,19 @@ export class YamlCamelResourceSerializer implements CamelResourceSerializer {
return code;
}

getComments(): string[] {
return this.comments;
}

setComments(comments: string[]): void {
this.comments = comments;
}

private parseComments(code: string): string[] {
const lines = code.split('\n');
const comments: string[] = [];
for (const line of lines) {
if (line.trim() === '' || this.COMMENTED_LINES_REGEXP.test(line)) {
if (line.trim() === '' || YamlCamelResourceSerializer.COMMENTED_LINES_REGEXP.test(line)) {
comments.push(line);
} else {
break;
Expand Down

0 comments on commit 21a50ff

Please sign in to comment.