json2typescript icon indicating copy to clipboard operation
json2typescript copied to clipboard

Call set method for private member

Open vincemaz opened this issue 6 years ago • 4 comments

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.

vincemaz avatar Oct 23 '19 21:10 vincemaz

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 avatar Oct 24 '19 13:10 andreas-aeschlimann

@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.

jmesa-sistel avatar Oct 24 '19 15:10 jmesa-sistel

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.

vincemaz avatar Oct 24 '19 17:10 vincemaz

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.

john-sanders-2 avatar Jul 03 '20 01:07 john-sanders-2