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

Always provide default values for properties #34

Closed
schneiderpat opened this issue Nov 20, 2018 · 13 comments
Closed

Always provide default values for properties #34

schneiderpat opened this issue Nov 20, 2018 · 13 comments

Comments

@schneiderpat
Copy link

Hey :)
Is there an option to always generate default values in the generated typescript class without annotating every property with [TSDefaultValue]?

Greets Patrick

@jburzynski
Copy link
Owner

Hi! Can you give an example? Do you mean providing a "global" default value for all properties?

@schneiderpat
Copy link
Author

Currently we have to to this:

[TSDefaultValue("")]
public string FirstName { get; set; }

[TSDefaultValue("")]
public string LastName { get; set; }

We need to generate the classes with default values because we have another generator to create formgroups in an angular in a special way.
A global Setting to generate default values for specific types would be awesome.

@jburzynski
Copy link
Owner

This seems like a useful feature for some cases. I'll aim to add it in the next release (the next release will also have the fix to #33).

I'll keep you updated with any progress in this topic. Cheers!

@schneiderpat
Copy link
Author

Thanks a lot :)

Greets!

@jburzynski
Copy link
Owner

I updated the NuGet packages in the dev branch (both regular and .NET CLI tool; nuget and nuget-dotnetcli folders). They now contain the functionality to generate empty values for the specified TypeScript types. You would use it like this in tgconfig.json (there's also a corresponding option in GenerationOptions):

{
  "generateEmptyValues": [ "string", "number", ... ]
}

Can you please check if it works properly in your case? I'd like to make sure it works before releasing. Thanks

@schneiderpat
Copy link
Author

schneiderpat commented Dec 3, 2018

How can I install the nuget-dotnetcli tool to test it?

I tested the NuGet package. If we have nullable type. Is it possible to set the default value to null?
But so far awesome job!!

@schneiderpat
Copy link
Author

An option with custom default mappings would solve the problem and would be very flexiable.

// tgconfig.json
{
    "emptyValueMappings: {
        "Date | null": "null",
        "number": "-1"
    }
}

And in your EmptyValue.cs you check for that option and iterate through them first. By checking them first you add the possibility to override your defaults.

@jburzynski
Copy link
Owner

You can download the TypeGen.DotNetCli package from NuGet and then specify custom source when installing the tool:

dotnet tool install TypeGen.DotNetCli -g --add-source "PACKAGE_FOLDER_PATH"

I've dropped support for per-project tools, because they are going to be abandoned by Microsoft anyway (I gave a more detailed 'rationale' for dropping per-project tools in this post), so right now TypeGen.DotNetCli is only a global tool.

About empty values, I'm thinking about removing the option that exists right now (generateEmptyValues) and replacing it with the option you suggested. I think there is no need for an additional option, if there's already an option to specify values for types. Do you think this would be ok?

@schneiderpat
Copy link
Author

You are right, an additional option is not necessary.
Removing generateEmptyValues is ok, when we can specify default values ourselves.

Thanks again for your work!

@jburzynski
Copy link
Owner

You're welcome! Thanks!

I'll let you know when it's implemented.

@jburzynski
Copy link
Owner

There is a new version on the dev branch (nuget package). The option is named defaultValuesForTypes and you would use it like this (the same as in your post):

"defaultValuesForTypes": {
    "number": "-1",
    "Date | null": "null"
}

It would be cool if you could check again if it works! Thanks! :)

@schneiderpat
Copy link
Author

I tested and it works awesome!
I´m waiting for the official release ;)

Thanks again for your great work!

@jburzynski
Copy link
Owner

No problem! Thanks for the info!

I've released version 2.0.1 (major version change is because of the changed syntax in the TypeGen nuget package - it's now the same as in the .NET CLI tool) and it contains the new functionality (usage is exactly the same). Hope it works! :)

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

No branches or pull requests

2 participants