-
Notifications
You must be signed in to change notification settings - Fork 29
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
base: main
Are you sure you want to change the base?
Conversation
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
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 😄 |
There was a problem hiding this 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*#.*$/; |
There was a problem hiding this comment.
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
getSerializer(): CamelResourceSerializer { | ||
return this.serializer; | ||
} | ||
setSerializer(serializer: CamelResourceSerializer): void { | ||
this.serializer = serializer; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getSerializer(): CamelResourceSerializer { | |
return this.serializer; | |
} | |
setSerializer(serializer: CamelResourceSerializer): void { | |
this.serializer = serializer; | |
} |
describe('comments', () => { | ||
it('should set and get comments', () => { | ||
const resource = new KameletResource(); | ||
resource.setComments(['a', 'b']); | ||
expect(resource.getComments()).toEqual(['a', 'b']); | ||
}); | ||
}); |
There was a problem hiding this comment.
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?
resource.setComments(['a', 'b']); | ||
expect(resource.getComments()).toEqual(['a', 'b']); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as before :D
@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
Codecov ReportAttention: Patch coverage is
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. |
if (!json) return; | ||
const rawEntities = Array.isArray(json) ? json : [json]; | ||
this.entities = rawEntities.reduce((acc, rawItem) => { | ||
constructor(code?: unknown, serializer?: CamelResourceSerializer) { |
There was a problem hiding this comment.
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
constructor(code?: unknown, serializer?: CamelResourceSerializer) { | |
constructor(code: CamelYamlDsl = [], serializer: CamelResourceSerializer = new YamlCamelResourceSerializer()) { |
There was a problem hiding this comment.
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*#.*$/; |
There was a problem hiding this comment.
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; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} | |
getComments: () => string[]; | |
setComments: (comments: string[]) => void; | |
} |
return this.serializer; | ||
} | ||
setSerializer(serializer: CamelResourceSerializer): void { | ||
this.serializer = serializer; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this.serializer = serializer; | |
// Get the existing comments and move them to the new serializer | |
serializer.setComments(this.serializer.getComments()); | |
this.serializer = serializer; |
@lordrip I incorporated your suggestions and also added types into the CamelResourceFactory, please check. |
…f unknown update tests to reflect types
Quality Gate passedIssues Measures |