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

feat: allow no key transformations #94

Merged
merged 2 commits into from
Feb 1, 2024
Merged

Conversation

cquick01
Copy link
Contributor

Allows the ability to define keys in JSON/dataclass that do not undergo transformation.

@dataclass
class MyDataclass(JSONWizard):
    VAR_1: int
    vAr__2: int
    VAr3__: int

    class _(JSONWizard.Meta):
        key_transform_with_dump = "NONE"

data = """{
    "VAR_1": 1,
    "vAr__2": 2,
    "VAr3__": 3
}
"""

foo = MyDataclass.from_json(data)
print("JSON -> Dataclass", foo)
print("Dataclass -> JSON", foo.to_json())

prints with no transformations

JSON -> Dataclass {
  "VAR_1": 1,
  "vAr__2": 2,
  "VAr3__": 3
}
Dataclass -> JSON {"VAR_1": 1, "vAr__2": 2, "VAr3__": 3}

@@ -27,6 +27,9 @@ class LetterCase(Enum):
# Converts strings (generally in camel case) to snake case.
# ex: `myFieldName` -> `my_field_name`
SNAKE = FuncWrapper(to_snake_case)
# Perfoms no conversion on strings.
# ex: `MY_FIELD_NAME` -> `MY_FIELD_NAME`
NONE = FuncWrapper(str)
Copy link
Owner

Choose a reason for hiding this comment

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

hmm... just wondering... wouldn't lambda o: o be slightly more optimal here? assuming that we are loading from JSON, where all keys are a str anyway.

I don't have any problem with str() but it seems to just do unnecessary conversion when in 99% of cases, one could expect a key to be a str anyway. Let me know your thoughts @cquick01 .

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That sounds reasonable, I'll push that change shortly. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tested briefly with the example script in the PR, all seems to be working 👍

Copy link
Owner

Choose a reason for hiding this comment

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

LGTM @cquick01 . I kicked off the CI workflow, will take a look and merge / deploy version later today if all seems good.

@rnag
Copy link
Owner

rnag commented Jan 30, 2024

Closes #92 - just linking issue directly here.

@rnag rnag merged commit 40ea64d into rnag:main Feb 1, 2024
7 checks passed
@adamcunnington-mlg
Copy link

@rnag Did this change get reversed? I see that NONE is not allowed :( and this code isn't in main? https://github.com/cquick01/dataclass-wizard/blob/main/dataclass_wizard/enums.py

@cquick01
Copy link
Contributor Author

@adamcunnington-mlg I see it in main - https://github.com/rnag/dataclass-wizard/blob/main/dataclass_wizard/enums.py#L32

But I don't think a new tag/release was done

@adamcunnington-mlg
Copy link

@rnag please can you make a release?

@nathan-beam
Copy link

Bump @rnag - would like this feature. Thanks!

@rnag
Copy link
Owner

rnag commented Sep 18, 2024

Sorry for the delay -- I just released v0.23.0 with the update to allow no key transformations.

Thanks for your work on this @cquick01 -- I credited you in the release notes.

@adamcunnington-mlg
Copy link

@rnag thank you for this! Please can you also review, PR, https://github.com/rnag/dataclass-wizard/pull/125/files which address 1 other open bug issue

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.

4 participants