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
Add custom_get_name
config option
#889
base: master
Are you sure you want to change the base?
Changes from 2 commits
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,3 +7,4 @@ __pycache__ | |
/dist | ||
/.github/changelog-generator-cache | ||
/tests/recursion.py | ||
/.vscode | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,12 @@ | |
Automatically generated file from a JSON schema. | ||
""" | ||
|
||
from typing import Any, Literal, TypedDict, Union | ||
from typing import Any, Callable, Literal, Optional, TypedDict, Union | ||
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. This file is generated from Then if we want to be able to configure it only with python code we should get the function definition out of this file :-) But in fact, I didn't see well how you plan to use it? 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. @sbrunner ok! I fixed the two other issues you pointed out. I can totally get the definition out and instead put it in the what's the command to generate the schema.json from the configuration file? I plan to use this exactly how I use it in the test. Without the I also invoke this by importing the 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. @sbrunner want me to pull that definition out of the 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.
It's the inverse, this file is generated from this schema https://github.com/sbrunner/jsonschema-gentypes/blob/7c7fe0d6f5539c13334580762bfacef292c6edba/jsonschema_gentypes/schema.json It's done by pre-commit :-) |
||
|
||
from typing_extensions import Required | ||
|
||
from jsonschema_gentypes import jsonschema_draft_04, jsonschema_draft_2019_09_meta_data | ||
|
||
AdditionalProperties = Union[Literal["Always"], Literal["Only explicit"]] | ||
""" | ||
Additional properties. | ||
|
@@ -18,6 +20,21 @@ | |
"""The values for the 'Additional properties' enum""" | ||
|
||
|
||
GetNameFunction = Callable[ | ||
[ | ||
Optional[ | ||
Union[ | ||
jsonschema_draft_04.JSONSchemaD4, | ||
jsonschema_draft_2019_09_meta_data.JSONSchemaItemD2019, | ||
] | ||
], | ||
Optional[str], | ||
bool, | ||
], | ||
str, | ||
] | ||
|
||
|
||
class ApiArguments(TypedDict, total=False): | ||
""" | ||
API arguments. | ||
|
@@ -26,6 +43,7 @@ class ApiArguments(TypedDict, total=False): | |
""" | ||
|
||
additional_properties: "AdditionalProperties" | ||
custom_get_name: GetNameFunction | ||
""" | ||
Additional properties. | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-06/schema#", | ||
"definitions": { | ||
"SubresourceUris": { | ||
"type": "object", | ||
"properties": { | ||
"feedback": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": ["feedback"], | ||
"title": "SubresourceUris" | ||
} | ||
}, | ||
"type": "object", | ||
"properties": { | ||
"subresource_uris": { | ||
"$ref": "#/definitions/SubresourceUris" | ||
} | ||
}, | ||
"required": ["subresource_uris"], | ||
"title": "ResponseType" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from typing import TypedDict | ||
|
||
from typing_extensions import Required | ||
|
||
|
||
class ResponseType(TypedDict, total=False): | ||
"""ResponseType.""" | ||
|
||
subresource_uris: Required["SubresourceUris"] | ||
""" | ||
SubresourceUris. | ||
|
||
Required property | ||
""" | ||
|
||
|
||
class SubresourceUris(TypedDict, total=False): | ||
"""SubresourceUris.""" | ||
|
||
feedback: Required[str] | ||
""" Required property """ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from typing import Callable, Optional, Union | ||
|
||
from jsonschema_gentypes import jsonschema_draft_04, jsonschema_draft_2019_09_meta_data, normalize | ||
from jsonschema_gentypes.cli import process_config | ||
from jsonschema_gentypes.configuration import Configuration | ||
|
||
GetNameFunction = Callable[ | ||
[ | ||
Optional[ | ||
Union[ | ||
jsonschema_draft_04.JSONSchemaD4, | ||
jsonschema_draft_2019_09_meta_data.JSONSchemaItemD2019, | ||
] | ||
], | ||
Optional[str], | ||
bool, | ||
], | ||
str, | ||
] | ||
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. Why this copy? |
||
|
||
|
||
def custom_get_name( | ||
schema: Optional[ | ||
Union[ | ||
jsonschema_draft_04.JSONSchemaD4, | ||
jsonschema_draft_2019_09_meta_data.JSONSchemaItemD2019, | ||
] | ||
], | ||
proposed_name: Optional[str] = None, | ||
upper: bool = False, | ||
) -> str: | ||
""" | ||
Custom get the name for an element. | ||
|
||
Just capitalize first Letter, don't do `.title` | ||
|
||
Parameter: | ||
schema: the concerned schema | ||
proposed_name: a name that we will use it the schema hasn't any title | ||
upper: should we use an upper case (For constants) | ||
""" | ||
# Get the base name | ||
has_title = isinstance(schema, dict) and "title" in schema | ||
name = schema["title"] if has_title else proposed_name # type: ignore | ||
assert name is not None | ||
name = normalize(name) | ||
|
||
prefix = "" if has_title else "_" | ||
if upper: | ||
# Upper case | ||
name = name.upper() | ||
# Remove spaces | ||
return prefix + "".join(["_" if char.isspace() else char for char in name]) | ||
else: | ||
# Title case | ||
name = name[0].upper() + name[1:] | ||
# Remove spaces | ||
return prefix + "".join([char for char in name if not char.isspace()]) | ||
|
||
|
||
def test_empty_array() -> None: | ||
config: Configuration = Configuration( | ||
generate=[ | ||
{ | ||
"source": "tests/custom_get_name.json", | ||
"destination": "tests/custom_get_name.py", | ||
"api_arguments": {"custom_get_name": custom_get_name}, | ||
} | ||
], | ||
) | ||
process_config( | ||
config, | ||
["tests/custom_get_name.json"], | ||
) | ||
|
||
with open("tests/custom_get_name.py") as f: | ||
content = f.read() | ||
assert "class SubresourceUris" in content |
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 should be on your own
~/.gitignore
file...