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
Open
eupharis
wants to merge
4
commits into
sbrunner:master
Choose a base branch
from
eupharis:custom-get-name
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 """ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from typing import 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 | ||
|
||
|
||
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 file is generated from
jsonschema_gentypes/schema.json
and describes the schema of the configurations file.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?
Like in the test?
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.
@sbrunner ok! I fixed the two other issues you pointed out.
I can totally get the definition out and instead put it in the
configuration
file.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
custom_get_name
, you wind up with the.title
version likeSubresourceuri
as the name and the test fails. I need the only-first-letter-capitalized version likeSubresourceUri
.I also invoke this by importing the
process_config
function from a Python script, not from the cli.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.
@sbrunner want me to pull that definition out of the
configuration
file? any other changes?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.
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 :-)