Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAFT: refactor(entities,resources): Decouple parsing from entities hook into a separate serializer class #1671

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

mmelko
Copy link
Contributor

@mmelko mmelko commented Nov 28, 2024

  • create CamelResourceSerializer interface and YamlCamelResourceSerializer implementantion
  • create CamelResourceFactory and CamelKResourceFactory to creating new resources
  • move parsing out of the entities hook

@mmelko mmelko requested a review from lordrip November 29, 2024 08:31
@lordrip lordrip marked this pull request as draft November 29, 2024 09:40
@lordrip
Copy link
Member

lordrip commented Nov 29, 2024

Thanks @mmelko for putting this together. I'm turning this PR into a draft for now, so we don't consume Chromatic credits until we're ready to merge 😄

Copy link
Member

@lordrip lordrip left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good @mmelko 💪 , let's try to tackle the open comments first

@@ -15,7 +16,6 @@ import { EventNotifier } from '../utils';
* ```
* The regular expression should match the first three lines
*/
const COMMENTED_LINES_REGEXP = /^\s*#.*$/;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[minor] Please let's move this comment as well

packages/ui/src/models/camel/camel-k-resource-factory.ts Outdated Show resolved Hide resolved
packages/ui/src/models/camel/camel-k-resource.ts Outdated Show resolved Hide resolved
Comment on lines 97 to 102
getSerializer(): CamelResourceSerializer {
return this.serializer;
}
setSerializer(serializer: CamelResourceSerializer): void {
this.serializer = serializer;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
getSerializer(): CamelResourceSerializer {
return this.serializer;
}
setSerializer(serializer: CamelResourceSerializer): void {
this.serializer = serializer;
}

packages/ui/src/models/camel/camel-route-resource.ts Outdated Show resolved Hide resolved
Comment on lines -102 to -108
describe('comments', () => {
it('should set and get comments', () => {
const resource = new KameletResource();
resource.setComments(['a', 'b']);
expect(resource.getComments()).toEqual(['a', 'b']);
});
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, can we create a YAML containing some comments and check they are preserved?

packages/ui/src/models/camel/kamelet-resource.ts Outdated Show resolved Hide resolved
resource.setComments(['a', 'b']);
expect(resource.getComments()).toEqual(['a', 'b']);
});
});
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as before :D

@mmelko
Copy link
Contributor Author

mmelko commented Nov 29, 2024

@lordrip thanks for the review! Updated code coming soon.

…o separate serializer class

 * create CamelResourceSerializer interface and YamlCamelResourceSerializer implementantion
 * create CamelResourceFactory and CamelKResourceFactory to creating new resources
 * move parsing out of the entities hook
Copy link

codecov bot commented Dec 2, 2024

Codecov Report

Attention: Patch coverage is 76.00000% with 24 lines in your changes missing coverage. Please review.

Please upload report for BASE (main@78a9529). Learn more about missing BASE report.

Files with missing lines Patch % Lines
...i/src/serializers/xml-camel-resource-serializer.ts 30.00% 7 Missing ⚠️
.../src/serializers/yaml-camel-resource-serializer.ts 80.00% 6 Missing ⚠️
packages/ui/src/models/camel/camel-k-resource.ts 50.00% 5 Missing ⚠️
...ckages/ui/src/models/camel/camel-route-resource.ts 61.53% 5 Missing ⚠️
...ages/ui/src/models/camel/camel-resource-factory.ts 91.66% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1671   +/-   ##
=======================================
  Coverage        ?   78.96%           
  Complexity      ?      365           
=======================================
  Files           ?      464           
  Lines           ?    14541           
  Branches        ?     2780           
=======================================
  Hits            ?    11483           
  Misses          ?     2789           
  Partials        ?      269           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

if (!json) return;
const rawEntities = Array.isArray(json) ? json : [json];
this.entities = rawEntities.reduce((acc, rawItem) => {
constructor(code?: unknown, serializer?: CamelResourceSerializer) {
Copy link
Member

@lordrip lordrip Dec 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try to use the CamelYamlDsl type

Suggested change
constructor(code?: unknown, serializer?: CamelResourceSerializer) {
constructor(code: CamelYamlDsl = [], serializer: CamelResourceSerializer = new YamlCamelResourceSerializer()) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please try to find a better name than the existing code 🙏 📦

* ```
* The regular expression should match the first three lines
*/
COMMENTED_LINES_REGEXP = /^\s*#.*$/;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it is better to use static for this.

export interface CamelResourceSerializer {
parse: (code: string) => unknown;
serialize: (resource: CamelResource) => string;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
getComments: () => string[];
setComments: (comments: string[]) => void;
}

return this.serializer;
}
setSerializer(serializer: CamelResourceSerializer): void {
this.serializer = serializer;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.serializer = serializer;
// Get the existing comments and move them to the new serializer
serializer.setComments(this.serializer.getComments());
this.serializer = serializer;

@mmelko
Copy link
Contributor Author

mmelko commented Dec 12, 2024

@lordrip I incorporated your suggestions and also added types into the CamelResourceFactory, please check.

Copy link

sonarcloud bot commented Dec 12, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants