Code Monkey home page Code Monkey logo

Comments (4)

condemil avatar condemil commented on August 17, 2024

I have two use-cases that I wouldn't be able to handle anymore if old transform is gonna gone and a new one will be there.

First one: transform missing string value to default enum field

class SomeEnum(Enum):
    one = auto()
    two = auto()
    default_value = auto()

@dataclass
class SomeData:
    some_field: SomeEnum

def _transform_some_field(some_field: Optional[str]):
    if not some_field:
        return SomeEnum.default_value
    return SomeEnum[some_field]

dacite.from_dict(SomeData, data, dacite.Config(
    transform={'some_field': _transform_some_field}
))

Second one: transform Optional[List[str]] to Optional[List[UUID]]:

@dataclass
class SomeData:
    some_list: Optional[List[UUID]]

def _transform_some_list(some_list: Optional[List[str]]):
    if not some_list:
        return []
    return [UUID(value) for value in some_list]

dacite.from_dict(SomeData, data, dacite.Config(
    transform={'some_list': _transform_some_list}
))

In theory, second use-case can be solved with smarter cast logic (or maybe already solved), but for the first one it requires some transformation. It would be nice to have a config option to pre-process the whole data object like this:

def _preprocess_data(data: dict):
    if not 'some_field' in data:
        return data['some_field'] = SomeEnum.default_value
    return data

dacite.from_dict(SomeData, data, dacite.Config(
    preprocess=_preprocess_data
))

This way the old transformation logic wouldn't be needed for my use-cases.

from dacite.

konradhalas avatar konradhalas commented on August 17, 2024

@condemil thank you for your feedback, could provide me also example of data dicts? It will be easier for me to check how it works with new implementation. I think that both cases could be covered (first one with default value for field, second with new implementation of transform)

from dacite.

condemil avatar condemil commented on August 17, 2024

You can think of a third-party forum or chat http API where you receive information about the user, the cases can be that user_type either moderator, or admin or can be not present at all in the json object for normal user. In this case in your enum you would like to have all three options.

from dacite.

konradhalas avatar konradhalas commented on August 17, 2024

@rominf I think that you will be able to handle both cases with new transform implementation (check #55), simple like that: Config({UUID: UUID}) or Config({SomeEnum: _transform_some_field}).

from dacite.

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.