How to change to pascal case in Mustache templates?

115 Views Asked by At

I have a mustache template like:

{{#attributes}}
func (event {{fileNameRoot}}) Get{{Name}}() string {
    return event.{{Name}}
}
{{/attributes}}

which outputs:

func (event JustATest) GetuserName() string {
    return event.userName
}

What I want is:

func (event JustATest) GetUserName() string {
    return event.UserName
}

How to achieve this? I am using Go and the mustache library github.com/hoisie/mustache.

The go code looks like:

data := map[string]interface{}{
    "fileNameRoot": "JustATest",
    "attributes":   schema.Attributes,
}

out, err := mustache.RenderFile("templates/test.go.mustache", data)
if err != nil {
    panic(err)
}
1

There are 1 best solutions below

2
ABDULLOKH MUKHAMMADJONOV On

You can use this useful library github.com/iancoleman/strcase to convert strings to different cases. In your case, you can convert schema.Attributes right before sending it as argument:

data := map[string]interface{}{
    "fileNameRoot": "JustATest",
    "attributes":   strcase.ToCamel(schema.Attributes),
}