-
Notifications
You must be signed in to change notification settings - Fork 72
feat: models only mode #436
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
base: main
Are you sure you want to change the base?
Changes from all commits
7b68be7
b7da2c3
2a87a1d
057ae81
ca063a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,12 @@ | |
|
|
||
| from .client_generators.scalars import ScalarData | ||
| from .exceptions import ConfigFileNotFound, MissingConfiguration | ||
| from .settings import ClientSettings, CommentsStrategy, GraphQLSchemaSettings | ||
| from .settings import ( | ||
| ClientSettings, | ||
| CommentsStrategy, | ||
| GraphQLSchemaSettings, | ||
| ModelsOnlySettings, | ||
| ) | ||
|
|
||
| simplefilter("default", DeprecationWarning) | ||
|
|
||
|
|
@@ -103,6 +108,57 @@ def get_section(config_dict: dict) -> dict: | |
| raise MissingConfiguration(f"Config has no [{tool_key}.{codegen_key}] section.") | ||
|
|
||
|
|
||
| def get_models_only_settings(config_dict: dict) -> ModelsOnlySettings: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please correct me if I'm wrong, but this function is a copy of
I believe there could be a single function which takes the model as parameter and is used for both the client settings and |
||
| """Parse configuration dict and return ModelsOnlySettings instance.""" | ||
| section = get_section(config_dict).copy() | ||
| settings_fields_names = {f.name for f in fields(ModelsOnlySettings)} | ||
| try: | ||
| section["scalars"] = { | ||
| name: ScalarData( | ||
| type_=data["type"], | ||
| serialize=data.get("serialize"), | ||
| parse=data.get("parse"), | ||
| import_=data.get("import"), | ||
| ) | ||
| for name, data in section.get("scalars", {}).items() | ||
| } | ||
| except KeyError as exc: | ||
| raise MissingConfiguration( | ||
| "Missing 'type' field for scalar definition" | ||
| ) from exc | ||
|
|
||
| try: | ||
| if "include_comments" in section and isinstance( | ||
| section["include_comments"], bool | ||
| ): | ||
| section["include_comments"] = ( | ||
| CommentsStrategy.TIMESTAMP.value | ||
| if section["include_comments"] | ||
| else CommentsStrategy.NONE.value | ||
| ) | ||
| options = ", ".join(strategy.value for strategy in CommentsStrategy) | ||
| warn( | ||
| "Support for boolean 'include_comments' value has been deprecated " | ||
| "and will be dropped in future release. " | ||
| f"Instead use one of following options: {options}", | ||
| DeprecationWarning, | ||
| stacklevel=2, | ||
| ) | ||
|
|
||
| return ModelsOnlySettings( | ||
| **{ | ||
| key: value | ||
| for key, value in section.items() | ||
| if key in settings_fields_names | ||
| } | ||
| ) | ||
| except TypeError as exc: | ||
| missing_fields = settings_fields_names.difference(section) | ||
| raise MissingConfiguration( | ||
| f"Missing configuration fields: {', '.join(missing_fields)}" | ||
| ) from exc | ||
|
|
||
|
|
||
| def get_graphql_schema_settings(config_dict: dict) -> GraphQLSchemaSettings: | ||
| """Parse configuration dict and return GraphQLSchemaSettings instance.""" | ||
| section = get_section(config_dict) | ||
|
|
||
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.
You create a fake objects here just to satisfy the requirement to provide
client_generatortoPackageGenerator.However, the strategy you implemented does not need to know anything about client at all - it's the mode without it.
This part of the code and multiple checks
if not self.models_onlyin the generator methods suggest there could be a solution easier to maintain. There should be separatePackageGeneratorsubclasses: for the client mode and models only mode. Key concepts:generatemethod, as the most important function that describes the steps to perform, should be implemented in the subclassesadd_operationcould live in the base class, the "client mode" class can callsuper().add_operationand then call the additionalself.client_generator.add_method()_validate_unique_file_namescan live in the base class, but the subclasses should implement a method to return expected file names_copy_filescan live in the base class, but the subclasses can implement a method to return files to copy__init__can share most of the logic, but the "client mode" class can expect additional arguments likeclient_generatorand other client-related settings, unused for "models only" mode.