Giter VIP home page Giter VIP logo

Comments (5)

Fatal1ty avatar Fatal1ty commented on May 27, 2024

Hi @tkukushkin

Iā€™m currently on my vacation and will answer you next week. Before that you can read an example of using MultiDict with SerializationStrategy here https://github.com/Fatal1ty/mashumaro#third-party-generic-types

from mashumaro.

tkukushkin avatar tkukushkin commented on May 27, 2024

Hi! I've seen this example, but if I'm not mistaken, it is not about reading dataclass from multidict, but about reading some data to multidict.
Have a good vacation)

from mashumaro.

Fatal1ty avatar Fatal1ty commented on May 27, 2024

Ok, I'm here. The first thing I want to say is that different web frameworks have different multidict implementations for working with query string. The package multidict is not the only implementation. So, since there is no standard implementation that would dictate how dunder methods should work and what additional methods should exist, I'm not sure that mashumaro should have native support for multidict structures. However, you can make it work with multidict structures using a pre-deserialize hook. In the following example, I'll show you how to do this with multidict package that has getall method:

from dataclasses import dataclass
from typing import get_type_hints

import multidict

from mashumaro import DataClassDictMixin
from mashumaro.core.meta.helpers import get_type_origin


class DataClassMultiDictMixin(DataClassDictMixin):
    def __init_subclass__(cls, **kwargs):
        super().__init_subclass__(**kwargs)
        list_fields = set()
        for fname, ftype in get_type_hints(cls).items():
            if issubclass(get_type_origin(ftype), list):
            # you can check not only for lists but for other collection types
                list_fields.add(fname)
        cls.__list_fields__ = list_fields

    @classmethod
    def __pre_deserialize__(cls, d):
        # d.copy() can be used if necessary
        for name in cls.__list_fields__:
            d[name] = d.getall(name)
        return d


@dataclass
class Foo(DataClassMultiDictMixin):
    x: float
    y: list[int]


o = Foo.from_dict(multidict.MultiDict([("x", "1.2"), ("y", "2"), ("y", "3")]))
assert o == Foo(x=1.2, y=[2, 3])

The idea is simple. The string contains either a single value or a multiple value. Here we use the dataclass type hints to collect all keys that are supposed to have multiple values and get all the values in the hook. We could use this reflection at runtime in the hook but it's more performant to do it once on dataclass creation.

from mashumaro.

Fatal1ty avatar Fatal1ty commented on May 27, 2024

@tkukushkin can we close this issue now?

from mashumaro.

Fatal1ty avatar Fatal1ty commented on May 27, 2024

Iā€™m closing this one. Feel free to reopen if you have further questions.

from mashumaro.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. šŸ“ŠšŸ“ˆšŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.