-
Notifications
You must be signed in to change notification settings - Fork 64
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
Rework inheritance #159
base: master
Are you sure you want to change the base?
Rework inheritance #159
Conversation
I skimmed through it quickly, and I see two major points atm:
|
|
@JohnWeisz, I've appended this PR with @jsonInheritance(discriminatorProperty({
property: 'type',
types: () => ({
Employee: Employee,
Investor: Investor,
PartTimeEmployee: PartTimeEmployee,
}),
}))
@jsonObject
class Person {
name: string;
} This functionality enables the following flow: Does this approach work for your use case? |
eb5e533
to
41be7f5
Compare
I'm currently investigating some issues with circular references and multiple |
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.
Couple of points:
- Can we get the old tests to have the same exceptions, but with just the new decorator? I saw some were changed, and we would like to support the same use cases
- I saw some tests completely removed, why is that?
- We need some docs, and explanation for the circular dependency issue. Would be nice to link to the original blog post
- I think we will need to document an upgrade path too
src/json-inheritance.ts
Outdated
/** | ||
* Function to be used to mutate the resulting serialization given the source object. | ||
*/ | ||
onSerializeType?: (source: any, result: {[k: string]: any}) => void; |
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.
Instead of mutation can it return a value that should be used as a result? Does not change much, but allows to use pure functions.
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.
Done.
src/json-inheritance.ts
Outdated
if (reverseMapping === undefined) { | ||
const reverseMapItems: Array<[Function, string]> = Object.keys(types()) | ||
.map(discriminator => { | ||
return [types()[discriminator].prototype.constructor, discriminator]; |
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.
types() is evaluated for each element of the dictionary. You could save it in a variable
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.
Does not make a difference IMO as I have to call a function regardless after the map is made, it is no longer called. I'll add a commit to show it. The result is less readable with minimal benefit IMO. I could save it in a variable and make it even less readable.
src/json-inheritance.ts
Outdated
result[property] = getReverseMapping().get(source.constructor); | ||
}, | ||
resolveType: data => { | ||
return types()[data[property]]; |
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.
This is maybe a nit, but onSerializeType evaluates types once and stores the result, while here it will evaluated every time. It could mean the behaviour is not consistent across both
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.
Same comment here: Does not make a difference IMO as I have to call a function regardless so no gain. I'll add a commit to show it. The result is less readable with minimal benefit IMO.
b992d10
to
836ec19
Compare
836ec19
to
89e2169
Compare
cfcbe91
to
7dbe9ca
Compare
Rebase complete |
Implemented in fork @Decoverto
This PR continues on top of #158 by reworking inheritance.
The PR removes
knownTypes
,typeHintEmitter
,typeResolver
, andnameResolver
with a more comprehensive@jsonObjectInheritance
decorator. The new implementation is also a lot less complex. 330 lines of source code get deleted 🚀.Since this PR is branched from #158, the diff will include changes from that PR. For only the inheritance rework, see: MatthiasKunnen/TypedJSON@issue-139-define-type-lazily...MatthiasKunnen:rework-inheritance.
Todo: