Giter VIP home page Giter VIP logo

Comments (3)

jcrist avatar jcrist commented on June 24, 2024 3

msgspec.inspect.type_info is one way, but it's a bit overkill for this use case. And __struct_encode_fields__ is technically non-public (and may be removed in the future). The best way to get this information is to use msgspec.struct.fields (docs). You're looking for the encode_name attribute on FieldInfo in the result:

In [3]: class Example(msgspec.Struct):
   ...:     user_id: int = msgspec.field(name="userId")
   ...: 

In [4]: msgspec.structs.fields(Example)
Out[4]: (FieldInfo(name='user_id', encode_name='userId', type=<class 'int'>, default=NODEFAULT, default_factory=NODEFAULT),)

from msgspec.

uwinx avatar uwinx commented on June 24, 2024 2

@Olegt0rr, I think inspect.type_info will do the right job for you.

from msgspec.inspect import type_info

# to access "user_id" field's metadata
field_meta = type_info(HelpIssue).fields[0]
assert field_meta.name == "user_id"
assert field_meta.encode_name == "userId"

References: https://jcristharif.com/msgspec/inspect.html

from msgspec.

Olegt0rr avatar Olegt0rr commented on June 24, 2024

@uwinx, thanks!

I've also found this way:

for name, encode_name in zip(
    struct.__struct_fields__,
    struct.__struct_encode_fields__, 
):
    ...

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.