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

Add a generator for custom code #131

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

daniel-kuon
Copy link

@daniel-kuon daniel-kuon commented Jun 13, 2019

Adds custom code generator that returns a list of RtNodes. This is helpful when you need to generate classes/interfaces that depend on C# types but have specific characteristics in TypeScript that are not needed on the server side.

These generators can generate pretty much every type of TS. Classes/Interface like the library is already able to do but also objects that are generated from these generated types.

Usage Example: Localization

I have different C# DTO classes that represent form data and are used to automatically generate forms via TypeScript. The labels for the form fields are localizable.

public class Login
{
    public string Username { get; set; }
    public string Password { get; set; }
    public bool RememberMe { get; set; }
}

public class Register
{
    public string Username { get; set; }
    public string Password { get; set; }
    public string PasswordRepeat { get; set; }
}

A CustomCodeGenerator then generates a TS interface that combines localization fields for all properties of the two classes. This interface is not needed at all on the server and it would need to be maintained manually whenever one of the C# form classes is changed.

interface ILocales {
    forms: IFormLocales
}

interface IFormLocales {
    username: string;
    password: string;
    rememberMe: string;
    passwordRepeat: string;
}

I use a localization framework that is initialized with an IFormLocales object and when I need a localized string I pass the path to the property of the object (e.g. translate("forms.userName")). To avoid typing the string every time my custom generator also generates a helper object for this:

export const locales: ILocales = {
    forms: {
        password: "forms.password",
        passwordRepeat: "forms.passwordRepeat",
        rememberMe: "forms.rememberMe",
        username: "forms.username"
    }
};

@ruler501
Copy link
Contributor

Should probably add some tests to make this more maintainable going forward. Otherwise maybe update one of the samples to show a complete example of how this could be used in a more advanced context. It is definitely possible that sufficient tests could show that as well though.
Definitely seems like an interesting concept to add, even if I'm still not quite sure what it does. This also reminds me I need to fix the merge errors in my pr and add some tests.

@pavel-b-novikov
Copy link
Member

I ageree with @ruler501 - tests needed

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

Successfully merging this pull request may close these issues.

None yet

3 participants