mahmoud / glom

☄️ Python's nested data operator (and CLI), for all your declarative restructuring needs. Got data? Glom it! ☄️
https://glom.readthedocs.io
Other
1.88k stars 61 forks source link

Serializer / deserializer #90

Open sobolevn opened 5 years ago

sobolevn commented 5 years ago

Hi!

I am using glom as a part of my REST API, it is used to serialize and deserialize requests when API does not 100% match existing objects.

And I have started to find that these objects are direct inverse of each other.

def serialize_data(webhook_data):
     return glom(webhook_data, (
            Assign('object_attributes.project', Spec('project')),
        ))

def deserialize_data(webhook_data):
     return glom(webhook_data, (
            Assign('project', Spec('object_attributes.project')),
        ))

Is it possible to generate deserialization from the serialization spec? Something like: glom(spen).inverse()

kurtbrose commented 5 years ago

I'm really trying to push towards gloms-that-transform-gloms

there are a few bits and pieces to get set up, but the idea would be that "inverse of (certain types of) gloms" would be expressable as a spec

for example in your case it might be

(explode_dict,  # TBD what would do this recursive explosion
   [Coalesce((Check(type=Assign), Assign(T.src, T.dest)), T)],  # if it is an assign, return the inverse assign; otherwise return itself
impolide_dict)  # TBD how to recursively compress dict back down

obviously there is a lot to figure out here; but I'm pushing to get the pieces in place to allow this kind of thing

sobolevn commented 5 years ago

@kurtbrose awesome! Thanks.