Typewriter: generate variable name with appended postfix

232 Views Asked by At

Currently I am using Typewriter for automatic generation of TypeScript class from my C# classes:

[TsDTO]
public class MyDto {
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
}

And I want to simply generate a Typescript interface appending the postfix "xyz" to the properties names:

export Interface IMyDto {
    prop1xyz: string;
    prop2xyz: string;
}

Is is possible to define my template for adding "xyz" without creating a custom method?

This template does not work:

$Classes(c => HasAttribute(c.Attributes, "TsDTO"))[
    export interface I$Name {
        // properties
        $Properties()[$namexyz: $Type;    <-- "xyz" breaks the code!
        ]
    }]
1

There are 1 best solutions below

3
adam0101 On

Create an extension method in a code block at the top of your template:

${
    string NameSuffixed(Property p)
    {
        return p.name + "xyz";
    }
}

Then use it in your Property template:

$Classes(c => HasAttribute(c.Attributes, "TsDTO"))[
    export interface I$Name {
        // properties
        $Properties()[$NameSuffixed: $Type;
        ]
    }]