-
-
Notifications
You must be signed in to change notification settings - Fork 52
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import deserialize from "../core/deserialize"; | ||
import serialize from "../core/serialize"; | ||
import { SKIP } from "../constants"; | ||
import custom from "./custom"; | ||
import { ClazzOrModelSchema } from "../api/types"; | ||
|
||
/** | ||
* This allows to embed the property values in the resulting json output | ||
* and vice-versa. | ||
* | ||
* @param type {ClazzOrModelSchema<T>} Some class or model schema. | ||
*/ | ||
export default function embedded<T>(type: ClazzOrModelSchema<T>) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Other built-in types all accept an For this type to be flexible, we probably need a "prefix" option. |
||
return custom( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
(value, _key, _sourceObject, jsonOutput) => { | ||
const serialized = serialize(value) | ||
Object.assign(jsonOutput, serialized) | ||
return SKIP | ||
}, | ||
(_, context) => { | ||
return deserialize(type, context.json) | ||
}, | ||
{ | ||
beforeDeserialize(callback, jsonValue, jsonParentValue) { | ||
callback(null, null) | ||
} | ||
Comment on lines
+24
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this necessary? It doesn't seem to do anything. |
||
} | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ import { | |
serializable, | ||
alias, | ||
date, | ||
embedded, | ||
list, | ||
map, | ||
mapAsArray, | ||
|
@@ -18,6 +19,7 @@ import { | |
custom, | ||
AdditionalPropArgs, | ||
SKIP, | ||
createModelSchema, | ||
} from "../../" | ||
|
||
import { observable, autorun } from "mobx" | ||
|
@@ -715,3 +717,44 @@ test("list(custom(...)) with SKIP", (t) => { | |
|
||
t.end() | ||
}) | ||
|
||
test("embedded(type)", (t) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
class PhoneNumber { | ||
constructor( | ||
public number: string, | ||
public extension?: string) { | ||
|
||
} | ||
} | ||
|
||
class Company { | ||
constructor( | ||
public name: string, | ||
public phone: PhoneNumber) { | ||
|
||
} | ||
} | ||
|
||
createModelSchema(PhoneNumber, { | ||
number: primitive(), | ||
extension: optional(primitive()) | ||
}); | ||
|
||
createModelSchema(Company, { | ||
name: primitive(), | ||
phone: embedded(PhoneNumber) | ||
}) | ||
|
||
const person = new Company('The Company', new PhoneNumber('+55123456789')) | ||
const serialized = serialize(person) | ||
|
||
t.deepEqual(serialized, { | ||
name: person.name, | ||
number: person.phone.number | ||
}) | ||
|
||
const deserialized = deserialize(Company, serialized) | ||
|
||
t.deepEqual(deserialized, person) | ||
t.end() | ||
}) |
There was a problem hiding this comment.
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.