Add custom formatters (#1733)

* Support custom formatters for CodeFormatter

* Add custom formatters argument

* Add graphql to docs/supported-data-types.md

* Add test

custom formatter for custom-scalar-types.graphql;

* Run poetry run scripts/format.sh

* Add simple doc
This commit is contained in:
Denis Artyushin
2023-11-24 18:59:32 +03:00
committed by GitHub
parent 3e0f0aaea5
commit a36ce94f2d
21 changed files with 351 additions and 5 deletions

23
docs/custom-formatters.md Normal file
View File

@@ -0,0 +1,23 @@
# Custom Code Formatters
New features of the `datamodel-code-generator` it is custom code formatters.
## Usage
To use the `--custom-formatters` option, you'll need to pass the module with your formatter. For example
**your_module.py**
```python
from datamodel_code_generator.format import CustomCodeFormatter
class CodeFormatter(CustomCodeFormatter):
def apply(self, code: str) -> str:
# processed code
return ...
```
and run the following command
```sh
$ datamodel-codegen --input {your_input_file} --output {your_output_file} --custom-formatters "{path_to_your_module}.your_module"
```

View File

@@ -435,8 +435,11 @@ Template customization:
Wrap string literal by using black `experimental-
string-processing` option (require black 20.8b0 or
later)
--additional-imports Custom imports for output (delimited list input)
--additional-imports Custom imports for output (delimited list input).
For example "datetime.date,datetime.datetime"
--custom-formatters List of modules with custom formatter (delimited list input).
--custom-formatters-kwargs A file with kwargs for custom formatters.
OpenAPI-only options:
--openapi-scopes {schemas,paths,tags,parameters} [{schemas,paths,tags,parameters} ...]
Scopes of OpenAPI model generation (default: schemas)

View File

@@ -6,6 +6,7 @@ This code generator supports the following input formats:
- JSON Schema ([JSON Schema Core](http://json-schema.org/draft/2019-09/json-schema-validation.html) /[JSON Schema Validation](http://json-schema.org/draft/2019-09/json-schema-validation.html))
- JSON/YAML Data (it will be converted to JSON Schema)
- Python dictionary (it will be converted to JSON Schema)
- GraphQL schema ([GraphQL Schemas and Types](https://graphql.org/learn/schema/))
## Implemented data types and features