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

[FR] Decorator to automate field aliasing #56

Open
NiklasRosenstein opened this issue Dec 9, 2023 · 0 comments
Open

[FR] Decorator to automate field aliasing #56

NiklasRosenstein opened this issue Dec 9, 2023 · 0 comments

Comments

@NiklasRosenstein
Copy link
Owner

NiklasRosenstein commented Dec 9, 2023

The snake_case convention is common for Python code, but that makes modeling data structures using another convention more difficult. There are two approaches one could take:

  1. Name the dataclass fields exactly like in the data structure
  2. Annotate each field with the databind Alias() setting

Often these naming conventions can be converted to and from using a simple rule-set however. It would be nice to have a decorator that can be applied to a class that injects the Alias() settings for each field, deriving the alias by some specified rule.

Example

from dataclasses import dataclass
from databind.core import field_auto_aliasing

@dataclass
@field_auto_aliasing("camelCase")
class MyStruct:
    some_field: int  # Behaves as if we used Annotated[int, Alias("someField")]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant