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

Call set method for private member #114

Open
vincemaz opened this issue Oct 23, 2019 · 4 comments
Open

Call set method for private member #114

vincemaz opened this issue Oct 23, 2019 · 4 comments
Assignees
Labels
enhancement New feature or request

Comments

@vincemaz
Copy link

Hi,

first thing first, you did a great job! Thanks!

I have some enhancement to propose.
When you use a private member it is mainly because you want to process some stuff in the set (or get) method.
By mapping directly the value to the private member you miss all this stuff.

So it will be great to call the set method if it exists, otherwise you set directly the class member.

My 2 cents.

@andreas-aeschlimann
Copy link
Member

You are right. The problem is that you can only use json2typescript with properties right now, not with getters and setters. If you do that, how can json2typescript know the actual name of the setter and getter? Of course, most people use the conventional _property and get property() naming. But if you use another naming scheme, we have no way to connect those.

On the other hand, is it really necessary to process json that comes from a server with a setter? Shouldn't be the data coming from a server already "sanitized"?

I just had this discussion with my colleague @tobiasschweizer and I am not sure yet how to proceed.

@andreas-aeschlimann andreas-aeschlimann self-assigned this Oct 24, 2019
@andreas-aeschlimann andreas-aeschlimann added the enhancement New feature or request label Oct 24, 2019
@jmesa-sistel
Copy link

@andreas-aeschlimann Sometimes you want to update other component or variable when you set the value, for example make visible a div if the property has a special value.
In my opinion it is very useful to use a setter.

@vincemaz
Copy link
Author

vincemaz commented Oct 24, 2019

You are right. The problem is that you can only use json2typescript with properties right now, not with getters and setters. If you do that, how can json2typescript know the actual name of the setter and getter? Of course, most people use the conventional _property and get property() naming. But if you use another naming scheme, we have no way to connect those.

On the other hand, is it really necessary to process json that comes from a server with a setter? Shouldn't be the data coming from a server already "sanitized"?

I just had this discussion with my colleague @tobiasschweizer and I am not sure yet how to proceed.

You could search for set _property() then set property() (with public visibility by example) and throw an error if you could not find any of them.
You will force the user to follow your naming scheme but it's already the case with setting ùndefined` value to properties.

Regarding your second point: your client application consume some APIs (and you probably don't owned these APIs) and receive data that may not match with your model or the way you want to provide them to your users.

@andreas-aeschlimann andreas-aeschlimann added enhancement New feature or request and removed enhancement New feature or request labels Apr 18, 2020
@john-sanders-2
Copy link

Thumbs up for this idea. In some of my data classes, I have calls to other private functions after data is set. This worked great using Object.assign, because it calls your setter methods. Now that my data is just being set into the variable, I've lost that functionality.

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

No branches or pull requests

4 participants