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

Capital letters in struct field names #60

Closed
riwarek opened this issue Jul 21, 2021 · 2 comments
Closed

Capital letters in struct field names #60

riwarek opened this issue Jul 21, 2021 · 2 comments

Comments

@riwarek
Copy link

riwarek commented Jul 21, 2021

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).

@cbroglie
Copy link
Owner

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.

@riwarek
Copy link
Author

riwarek commented Aug 22, 2021

I use something like this:

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.

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