Code Monkey home page Code Monkey logo

Comments (6)

jcrist avatar jcrist commented on May 16, 2024 1

Cool, yeah, I can definitely add support for this. Adding it to the queue, thanks for the suggestion!

from msgspec.

CamDavidsonPilon avatar CamDavidsonPilon commented on May 16, 2024 1

Already trying it :)

Thanks for devoting time to this, @jcrist!

from msgspec.

jcrist avatar jcrist commented on May 16, 2024

Hmmm, we could definitely make that work. Can you expand a bit on your use case here?

Note that if you're just trying to restrict a field to a smaller set of valid values, you can kind of do this with enums today. Enum types are serialized as strings of their field names, and IntEnum types are serialized as their integer values, so as far as decoders are concerned, these are roughly equivalent:

class Foo(msgspec.Struct):
     x: Literal[1, 2, 3]

class MyEnum(enum.IntEnum):
    a = 1
    b = 2
    c = 3

class Foo2(msgspec.Struct):
    x: MyEnum

msgspec.json.decode(b'{"x": 3}', type=Foo)  # Ok, returns 3 (not implemented currently)
msgspec.json.decode(b'{"x": 3}', type=Foo2)  # Ok, returns MyEnum.c

msgspec.json.decode(b'{"x": 4}', type=Foo)  # Errors, int not in set of valid literals (not implemented currently)
msgspec.json.decode(b'{"x": 4}', type=Foo2)  # Errors, int not in set of valid enum values

from msgspec.

CamDavidsonPilon avatar CamDavidsonPilon commented on May 16, 2024

Simply: I have existing types that use Literal:

PdChannel = Literal["1", "2"]

...

class ODReadings(Struct):
    timestamp: str
    od_raw: dict[PdChannel, ODReading]

msgspec.json.decode(..., type=ODReadings) # TypeError: Type 'typing.Literal['1', '2']' is not supported

I could use Enums, but I kinda like literals in this case!

from msgspec.

jcrist avatar jcrist commented on May 16, 2024

Thanks for the issue request, this has been fixed in #71. I have a few small fixups I hope to get in before releasing, but feel free to try this out from the main branch if you're interested:

pip install git+https://github.com/jcrist/msgspec

from msgspec.

jcrist avatar jcrist commented on May 16, 2024

This has now been released. Up on pypi now, should be on conda-forge shortly.

from msgspec.

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.