appvision-gmbh / json2typescript

Map JSON to a TypeScript class with secure type checking!
https://www.npmjs.com/package/json2typescript
MIT License
278 stars 55 forks source link

Call set method for private member #114

Open vincemaz opened 5 years ago

vincemaz commented 5 years ago

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 commented 5 years ago

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.

jmesa-sistel commented 5 years ago

@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 commented 5 years ago

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.

john-sanders-2 commented 4 years ago

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.