Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for embedded types #140

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

danfma
Copy link
Contributor

@danfma danfma commented Aug 26, 2020

These changes will allow support for embedded types or maybe more powerful custom serializers.
It allows the user to change the outputted object during the serialization/deserialization.

  • A new custom type was created too (embedded), which embed the object data into the parent resulting JSON (and vice-versa).

@danfma
Copy link
Contributor Author

danfma commented Aug 26, 2020

@NaridaL @mweststrate Should I update the README manually or did I miss any step? Locally, it looks to be working (build and tests). :)

Copy link
Collaborator

@NaridaL NaridaL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to run yarn build-docs to rebuild the README.

* @param type {ClazzOrModelSchema<T>} Some class or model schema.
*/
export default function embedded<T>(type: ClazzOrModelSchema<T>) {
return custom(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other built-in types don't use custom, rather they build a PropSchema manually. Unless there is a specific reason, we should do the same here.

*
* @param type {ClazzOrModelSchema<T>} Some class or model schema.
*/
export default function embedded<T>(type: ClazzOrModelSchema<T>) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other built-in types all accept an additionalArgs?: AdditionalPropArgs argument. We should do the same here.

For this type to be flexible, we probably need a "prefix" option.

Comment on lines +24 to +26
beforeDeserialize(callback, jsonValue, jsonParentValue) {
callback(null, null)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary? It doesn't seem to do anything.

@@ -715,3 +717,44 @@ test("list(custom(...)) with SKIP", (t) => {

t.end()
})

test("embedded(type)", (t) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other typescript tests all use decorators. Can we move these to simple.js?

Comment on lines +7 to +12
/**
* This allows to embed the property values in the resulting json output
* and vice-versa.
*
* @param type {ClazzOrModelSchema<T>} Some class or model schema.
*/
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an example of the usage here. See the other built in types for how to format it so that it shows up properly in the README.

@pyrogenic
Copy link
Collaborator

This is an interesting idea. I think inline might suggest its purpose more readily? As a decorator it would then read @serialize(inline(ClassName)).

I do see a lot of potential for property collisions or otherwise acting on a JSON element more than once, which gives me pause.

Interestingly, in my own work, I could use the opposite: I have to deal with wiretypes full of totally unnecessary object nesting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants