Adding Prefixes to Generated SubModel Names #108
killik-dan-stubbs
started this conversation in
Ideas
Replies: 1 comment
-
@killik-dan-stubbs Hi 👋 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Adding Prefixes to Generated SubModel Names
Over the past year or so, I've really enjoyed using Json to Dart Model for generating freezed classes from json responses that I get from my company's backend. It saves so much time and effort; I'm very grateful for the work that has gone into it.
Issue: Confusing Model Names
I have one issue with class generation though which keeps with extension from perfection. The problem is that there is no way, as far as I can tell, to assign a standard prefix to a generated model.
The reason why this is important is that you could generate two separate models for different api calls which both have a 'contact' map field. After generation, both of these models would be called
Contact
. If looking using devtools and trying to find one contact over another, it can be hard to differentiate which model is the correct to use.If we had the ability to add a Prefix to all generated models, we could have models like
RouteOneContact
andRouteTwoContact
which would be much easier to read.Below is the response of an imaginary api call which returns a users Contact information.
Currently, the generated model would look like this with the Base Class name set to
UserDetails
:Proposed Solution 1: Prefix Input
Perhaps an extra question could be added to the flow of the json to dart model generation which would allow the user to add a prefix to the generated model. This would be a simple string input that would be added to the model name.
Example
This would then generate the following models:
The fact that the base class name would not be effected by the prefix input is not ideal. Perhaps the base class name could be used as a prefix if the user does not input one.
Proposed Solution 2: Base Class name as Prefix of Sub Models
Another solution could be to ask the user if the base class name should be used as a prefix for all sub models. This would be a simple boolean input which could either be asked or added within a config file.
As a side note, the file names for the models may not need to be changed with the prefix but it may be a good idea to add them anyway for consistency and to avoid potential linting errors.
Example
This would then generate the following models:
Finishing Up
What do you all think? Would this be a useful feature to add to the json to dart model generation process? If I get a chance, I may make a fork of the project and try to implement this feature myself.
Beta Was this translation helpful? Give feedback.
All reactions