Coder Social home page Coder Social logo

Comments (3)

ask avatar ask commented on August 22, 2024 1

Hey Ruby! Sorry, I missed this for so long. This sounds like a good idea,

Note though that DateTimes are already automatically converted to str by the JSON serializer, so it doesn't have to convert it at the as_dict point as when we send it to our JSON serializer that will convert it to ISO8601 strings.

I would like to support arbitrary types, similar to isodates, but I'm not sure of the best way to do that.

We could have an API more like data classes and attrs, by having users specify FieldDescriptors
with more capabilities, like how to serialize/deserialize/verify the data

class Account(Record):
    account_id: UUID = Field(to_value=uuid_to_str, from_value=str_to_uuid, required=True)
    amount: Decimal = DecimalField(max_decimal_places=1000)

Alternatively, we could have optional callbacks with reserved names like this:

class Account(Record):
  account_id: UUID

  def account_id_to_value(self, account_id: UUID) -> str:
      return str(account_id)

 def account_id_from_value(self, account_id: str) -> UUID:
    return UUID(account_id)

I guess the Field is better since you can specify the above as merely:

class Account(Record):
    account_id: UUID = Field(str, UUID, required=True)

and you could subclass it:

class UUIDField(Field):
    to_value = str
    from_value = UUID
    required = True
   

from faust.

ask avatar ask commented on August 22, 2024

For reference here's our custom JSONEncoder class that adds datetime and others as built-in JSON data types:
https://github.com/robinhoodmarkets/faust/blob/61cb72e13d571fdc16274140bdcd2ee3a483d2ae/faust/utils/json.py#L68-L91

It's usually faster to do it this way, as then you don't have to do yet another recursive pass over all the fields. At least that was true when I added it; I think we are at this point already doing two traversals to serialize.

from faust.

vineetgoel avatar vineetgoel commented on August 22, 2024

Closing this for now as this is not something we plan on adding into Faust in the near future.

from faust.

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.