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

FeatureRequest: struct tag case only for json #1321

Closed
c9s opened this issue Oct 25, 2023 · 3 comments
Closed

FeatureRequest: struct tag case only for json #1321

c9s opened this issue Oct 25, 2023 · 3 comments

Comments

@c9s
Copy link
Contributor

c9s commented Oct 25, 2023

I am looking for the camel case option, however, I am using snake case for the table, column names and camel case for the json tag

I have seen PR #194 and Issue #191
but it seems the flag turned to be a global switch for all tag formats.

The reason that we need a single case switch specifically for json tag is, using camel case in the database field names and table names could cause some case-sensitivity issues. For JSON, since it's used for the HTTP server, we'd like to have the response size as short as possible, and JavaScript uses camel case as its convention case for variable names, property names... etc. see Google JavaScript Style Guide https://google.github.io/styleguide/jsguide.html#naming-parameter-names

for example, right now it looks like this:

type User struct {
	ID                      uint64      `db:"id" boil:"id" json:"id" toml:"id" yaml:"id"`
	Email                   string      `db:"email" boil:"email" json:"email" toml:"email" yaml:"email"`
	Name                    string      `db:"name" boil:"name" json:"name" toml:"name" yaml:"name"`
	FirstName               string      `db:"first_name" boil:"first_name" json:"first_name" toml:"first_name" yaml:"first_name"`
	LastName                string      `db:"last_name" boil:"last_name" json:"last_name" toml:"last_name" yaml:"last_name"`
}

and it would be great if we can generate camel case for the json tag:

type User struct {
	ID                      uint64      `db:"id" boil:"id" json:"id" toml:"id" yaml:"id"`
	Email                   string      `db:"email" boil:"email" json:"email" toml:"email" yaml:"email"`
	Name                    string      `db:"name" boil:"name" json:"name" toml:"name" yaml:"name"`
	FirstName               string      `db:"first_name" boil:"firstName" json:"first_name" toml:"first_name" yaml:"first_name"`
	LastName                string      `db:"last_name" boil:"lastName" json:"last_name" toml:"last_name" yaml:"last_name"`
}

since in JavaScript convention (see here https://google.github.io/styleguide/jsguide.html#naming-parameter-names), we use camel case for property names, it should look like this:

resp.firstName
resp.lastName

so, do you think if we could add an option that specifies the case for specific format, like: --struct-tag-case json:camel or --struct-tag-case yaml:camel ?

@stephenafamo
Copy link
Collaborator

I won't be able to implement this myself, but I would be willing to review a PR for this

@c9s
Copy link
Contributor Author

c9s commented Dec 21, 2023

@stephenafamo I will work on this

@c9s
Copy link
Contributor Author

c9s commented Dec 21, 2023

@stephenafamo I've implemented this feature in PR #1336 please take a look, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants