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

refactor: Generator parts from interfaces into zod objects #42

Merged
merged 9 commits into from
Jul 19, 2024

Conversation

Llandy3d
Copy link
Member

@Llandy3d Llandy3d commented Jul 18, 2024

The refactoring was done to have validation for loading and possibly also for saving the file 🤔

I opted to have all the inferring together at the end of the file, if you prefer another approach feel free to raise it ( I just think it looks more readable this way)

Feel free to ignore the loading generator file logic, that will be completed in a separate PR

This is part of https://github.com/grafana/k6-cloud/issues/2520

Comment on lines +6 to +16
export const GeneratorFileData = z.object({
name: z.string(),
version: z.string(),
recordingPath: z.string(),
options: TestOptions,
testData: TestData,
rules: TestRule.array(),
allowlist: z.string().array(),
})

export type GeneratorFileData = z.infer<typeof GeneratorFileData>
Copy link
Collaborator

Choose a reason for hiding this comment

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

I know there are no conflicts between exported type and const of the same name, but I would still prefer zod objects to be appended by Schema to be more explicit:

export const GeneratorFileDataSchema = z.object({ .. })
export type GeneratorFileData = z.infer<typeof GeneratorFileData>

WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think I like that the names are shared so depending on where you use it you get the right "object" 🤔

In theory you know that it's a schema because it will be imported from src/schemas/... and inspecting the object also clearly gives it away.
I find it really nice and reduces repetition/verbosity.

It's also true that I come from Python where it's done the same way, you use the object both as the object to construct and as a type, so maybe for typescript let's wait for more feedback before taking a decision 💭

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it's a bit magical and I was surprised to learn TypeScript doesn't throw an error in such cases.
Another downside of using the same names is that it's harder to understand what's being imported when you look at the import statements (it's probably a nitpick though)

I think I agree with Edgar that we should append Schema to the schema consts names.
I also suggest we keep the types in src/types - for example, in src/types/rules.ts you'd just import the schema and export the inferred type like this:

import { TestRuleSchema } from '@schemas/rules.ts'

export type TestRuleSchema = z.infer<typeof TestRuleSchema>

This way we'll end up keeping schemas and types in separate folders and will be importing types from src/types (among other benefits, schema files will only schema files only have schema definitions in them, which is minor, but I think it'll improve readability)

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it makes sense, once you "compile" the code types are stripped out so if you are importing and using something it's the actual object.

I have a preference for that approach probably because I'm coming from python and it's less verbose but if the majority of the team thinks that separating schemas & types aids with readability, I think it's worth the cost of a few more lines 🙌

Comment on lines +251 to +252
// TODO: we might want to send an error on wrong file, a system to send and show errors from the main process
// could be levereged for many things mmmm
Copy link
Collaborator

Choose a reason for hiding this comment

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

Either messaging between render and main OR do the parsing on render side and let it handle there 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

oh, I left the comment there! 😄

Regardless of leveraging it in here or not, that system would be useful for having a channel between main/renderer to just send errors on any operations, the item has already been added on the todo list.

In the future we might move more operations on the main process, so handling the parsing on the renderer side would just be probably a temporary solution 🤔

Copy link
Collaborator

@going-confetti going-confetti 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!

@Llandy3d Llandy3d merged commit 20501e4 into main Jul 19, 2024
1 check passed
@Llandy3d Llandy3d deleted the load_generator_file branch July 19, 2024 12:11
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.

3 participants