Skip to content

FeatureRequest: struct tag case only for json #1321

Closed
@c9s

Description

@c9s

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 ?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions