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

Polymorphism #125

Merged
merged 6 commits into from
Oct 21, 2018
Merged

Polymorphism #125

merged 6 commits into from
Oct 21, 2018

Conversation

janis91
Copy link
Contributor

@janis91 janis91 commented Jan 26, 2018

Added the possibility to provide an array of discriminator functions in case of an array of different types.

If you have any other proposal for doing this, comment here or in the related ticket: #51

Copy link
Member

@NoNameProvided NoNameProvided left a comment

Choose a reason for hiding this comment

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

Before I can review, please write some docs about it in the readme with examples. So I can understand from the user point of perspective what you tried to achieve.

@janis91
Copy link
Contributor Author

janis91 commented Jan 27, 2018

Ok, I will provide it asap. One question: travis is complaining, but I cannot reproduce it locally, is this a wrong configuration of CI or did I do something wrong?

@NoNameProvided
Copy link
Member

NoNameProvided commented Jan 27, 2018

One question: travis is complaining, but I cannot reproduce it locally

Only Node 4 is failing with

SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode

Don't care about it, it's already removed from routing-controllers, so I will remove it from here as well.

Update: Done, you can merge master into your branch.

@janis91 janis91 force-pushed the polymorph branch 2 times, most recently from a510218 to 14aff20 Compare January 29, 2018 06:45
@janis91
Copy link
Contributor Author

janis91 commented Jan 29, 2018

@NoNameProvided I wrote the doc part and rebased the commit.

@janis91
Copy link
Contributor Author

janis91 commented Feb 2, 2018

@NoNameProvided any thoughts on this already?

@NoNameProvided NoNameProvided self-assigned this Feb 4, 2018
@janis91
Copy link
Contributor Author

janis91 commented Mar 15, 2018

It has been quite some time since I updated. Is there anything left why this does not get integrated?
@NoNameProvided

@NoNameProvided
Copy link
Member

Hi @janis91

It's because I have 100+ Github issue, and short on time. I am also not fully engaged with class-transformer yet, as I want to kick class-validator to 1.0 first then working on class-transformer.

We can ask a review from @pleerock, if he finds it ok, we can merge.

@NoNameProvided
Copy link
Member

Will try to go through the PRs during the easter. :) No promises tho.

@janis91
Copy link
Contributor Author

janis91 commented Mar 28, 2018

@NoNameProvided Please do not get me wrong. I just wondered if I have to to do something else or if it's just because you are busy (no problem with that, I have that problem for my projects as well). Still I would appreciate it if you have the time during easter :-)

@mustafaekim
Copy link

Hi @janis91, the PR seems to cover only arrays having different type of objects. However a single property can also have a value of different object instance. Maybe my first example mislead-ed.

@janis91
Copy link
Contributor Author

janis91 commented Apr 10, 2018

@mustafaekim You're right, the PR covers arrays only. But this is because it is already possible to give a function that determines the type based on the given TypeOptions for single properties. The only thing that was missing, was the possibility to differentiate objects inside of an array.

But in order to be able to provide multiple discriminator functions also for a single property, I could try to add the logic for this case as well.

…to convert into different types in case of a single property and for nested objects in arrays
@mustafaekim
Copy link

OK thanks. I would prefer adding a property (whos key can be set) to the json (discriminator property) indicating the class type.

@janis91
Copy link
Contributor Author

janis91 commented Apr 10, 2018

@mustafaekim could you provide an example, I think I am not understanding what you mean.

@mustafaekim
Copy link

That's how you discover the constructor function:

export abstract class Photo {
    id: number;
    filename: string;
}

export class Landscape extends Photo {
   panorama: boolean;
}

function isLandscape(value: any): Function | false {
    return value.panorama !== undefined ? Landscape : false;
}

export class Album {
    id: number;
    name: string;
    @Type(() => [ isLandscape, isPortrait, isUnderWater ])
    topPhoto: Landscape | Portrait | UnderWater;
}

What I propose is, when transforming from class to json, it may be better to add a discriminator property (like __type) to the json so that when we transform back, we can simply check the value of the discriminator property and find the constructor. Your solution may be optional but by default a adding a discriminator property will work well for 99% of cases

That's how typedjson, Jackson (Java) work

@janis91
Copy link
Contributor Author

janis91 commented Apr 11, 2018

@mustafaekim I tried what I think was your idea. Please check it out.

@mustafaekim
Copy link

Hi, I've read the code, it's like the way I was trying to describe.

can't the code below

topPhoto: Landscape | Portrait | UnderWater;

be

topPhoto: Photo;

?

@janis91
Copy link
Contributor Author

janis91 commented Apr 12, 2018

@mustafaekim Well, actually the topPhoto can only be of type Photo, when the json input doesn't match the other types (because it is used as a fallback).
But in general I would say, that you lose the support of typescript in terms of auto completion and so on, if you just type Photo (it is most likely that you will not only get a Photo, but a Landscape, Portrait or UnderWater photo and you may want to use the different properties they introduce.

@mustafaekim
Copy link

I didnt know that in Typescript if you use the type Photo, you do not get support for tooling. That's interesting. That is not how it works in tooling for other languages. Anyway thank you for clarification.

Is there any schedule when this feature will be merged into the core of the library? (@NoNameProvided )

@cojack cojack changed the base branch from master to develop October 21, 2018 16:07
@cojack cojack added this to the 0.1.11 milestone Oct 21, 2018
@cojack cojack added the type: feature Issues related to new features. label Oct 21, 2018
@cojack cojack merged commit a650d9f into typestack:develop Oct 21, 2018
@cojack
Copy link
Contributor

cojack commented Oct 21, 2018

Hi
@janis91 thanks for your work, I have accept your PR and will be published into next version as 0.1.11

Thanks for your time 😍
Please feel free to contribute for another PR!

@cojack cojack added status: fixed Issues with merged PRs, but not released yet and removed type: feature Issues related to new features. help wanted labels Oct 21, 2018
@lucaswxp
Copy link

lucaswxp commented Mar 6, 2019

Hello guys.

Was this solved? Is it already stable?

@agersea
Copy link

agersea commented Jul 1, 2019

+1 any ETA on this?

@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 30, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: fixed Issues with merged PRs, but not released yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants