You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Lets assume we have such struct:
type Products struct {
Gtin string json:"gtin,omitempty"
Name Lang json:"name,omitempty"
}
From somwhere i recieve such template:
{{gtin}} blah {{name}}
And now, because I have no impact on this template and my structure fields MUST be public (golang to make it public use capital letters in names) I have inconsistency gtin<->Gtin.
As a result only "blah" will be shown.
Is it possible to add some "ignore capitalisation" switch?
Otherwise I have to make some initial parsing which seems to be usless effort (and has impact on performance).
The text was updated successfully, but these errors were encountered:
The capitalization behavior is part of the Go language specification so there is no magic switch to add, but you can certainly use encoding/json to convert to JSON and then back to a map: https://goplay.space/#Yz-AVJrRifR.
var (
reg = regexp.MustCompile(`{{[^\w]?\w+}}`) //any word closed into {{}} with possible leading sign
)
func CapitalizeTemplate(template string) string {
return reg.ReplaceAllStringFunc(template, func(t string) string {
return strings.Title(t)
})
}
This is preatty short solution and shouldn't take too long to write it , anyway it would be just handful for user to have some built-in method to do this. Just a proposition.
Lets assume we have such struct:
type Products struct {
Gtin string
json:"gtin,omitempty"
Name Lang
json:"name,omitempty"
}
From somwhere i recieve such template:
{{gtin}} blah {{name}}
And now, because I have no impact on this template and my structure fields MUST be public (golang to make it public use capital letters in names) I have inconsistency gtin<->Gtin.
As a result only "blah" will be shown.
Is it possible to add some "ignore capitalisation" switch?
Otherwise I have to make some initial parsing which seems to be usless effort (and has impact on performance).
The text was updated successfully, but these errors were encountered: