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

key alias for jsonclasses #40

Open
ViktorSky opened this issue Jun 12, 2023 · 6 comments
Open

key alias for jsonclasses #40

ViktorSky opened this issue Jun 12, 2023 · 6 comments

Comments

@ViktorSky
Copy link

Is it possible to get the value from aliases?
this happens because the key value is too long to call the attributes

eg: here api returns something with repetitive or long text

data = {
    'sticker': {
        'stickerCollectionSummary': {
            'bannerUrl': str,
            'collectionId': str,
            # ...
        },
        'stickerId': str,
        # ...
    },
    # ...
}

and making the jsonclass of this is tedious and repetitive with the name

@jsonclass
class StickerCollectionSummary:
    bannerUrl: str
    collectionId: str
    # ...

@jsonclass
class Sticker:
    stickerCollectionSummary: StickerCollectionSummary
    stickerId: str
    # ...
sticker: Sticker = ... # get the object
# for get banner
sticker.stickerCollectionSummary.bannerUrl #too long
# for get collectionId
sticker.stickerCollectionSummary.collectionId # too long
# for get sticker id
sticker.stickerId # repetitive

If suggestions are accepted, I would ask that a method be added to get the key from the dict, to use the value in an alias as shown below.

# In this case, I decided to assume that there is a method fkey
# that, when starting the class, assigns the value from what is obtained from the key
# info: fkey is 'fetch key' or 'from key'

@jsonclass
class StickerCollectionSummary:
    bannerUrl: str
    id: str = types.str.fkey('collectionId')

@jsonclass
class Sticker:
    collection: StickerCollectionSummary = types.fkey('stickerCollectionSummary')
    id: str = types.str.fkey('stickerId')
@victorteokw
Copy link
Contributor

Hi @ViktorSky, I guess we can.

@ViktorSky
Copy link
Author

Hi @ViktorSky, I guess we can.

How is it done? I have been reading the documentation but I have only found the references with ´linkedBy/linkedTo´, I have also read the source code, but it is a bit confusing to handle the modifiers

@victorteokw
Copy link
Contributor

Hi @ViktorSky, do you mean nested models?

@ViktorSky
Copy link
Author

Hi @ViktorSky, do you mean nested models?

No, I meant to specify the key for the field.

@jsonclass
class User:
    name: str = types.str.alias('userName')
    type: int = types.int.alias('userType')

data = {
    'userName': 'John',
    'userType': 0
}

user = User(**data)
print(user)
"""
User(userName='John')
"""
print(user.name)
"""
John
"""

@ViktorSky
Copy link
Author

Hi @ViktorSky, do you mean nested models?

#40 (comment)
Is it possible to use a field from a key alias?

@ViktorSky
Copy link
Author

Is there anyone reading?

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

No branches or pull requests

2 participants