-
Notifications
You must be signed in to change notification settings - Fork 18k
Less-Tag or Default Tag #73662
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
Comments
see #63397 (comment) |
@seankhliao you are misunderstanding me My idea is not just about JSON, but yeah It will cover it #63397 is just/mostly about My idea is about writing less tags Here look: // Currently
type User struct {
ID int `json:"id" bson:"_id"`
FirstName string `json:"first_name" bson:"first_name"`
LastName string `json:"last_name" bson:"last_name"`
Username string `json:"username" bson:"username"`
Email string `json:"email" bson:"email"`
PhoneNumber string `json:"phone_number" bson:"phone_number"`
}
// My Intention
type User struct {
ID int `bson:"_id"`
FirstName string
LastName string
Username string
Email string
PhoneNumber string
} It will help to write less code, and you dont have to write one name multiple times (with some casing) It is not just about |
May be I should post in |
Other marshalers like bson are free to do their own thing. |
Intro
Imagne you dont to have write
json:"name"
tag every time, Insted you can just define following:And everything works as you want.
Example: with this struct:
you can get such json:
Isn't It awesome?
This is generally how I see it, If you're short on time, everything important is summarized here.
In the next sections, I’ll go into more detail and share my ideas — but the main point doesn't change.
Strugles
My first strugles with Go
I came to Golang from Java, I did simple pet-project in my internship. At that time I didn't even know I need to write tags. My TeamLead checked apis and said write tags, and I wrote tags with
lowerCamelCase
. TeamLead checked again and said they usesnake_case
and I had to rewrite it. At that time I didn't feel good and I felt like I'm doing something that coulde be probrammed.From that I had ideas
General strugles
Some times we forget about writing tags
Some times we misspell it
Some times It is just lame for us
Attempted to solve
Attempted to solve it on my own.
I found this repo github.com/iancoleman/strcase, It helped me to make my solution.
I did my solution in this repo
github.com/go-shafaq/defcase
It worked but It had same problems
Main problem was/is:
At the End I thought It should be done in languge level, Otherwise there could be some problems
My Ideas About Impl
Example in code
HasDefaultCase(tag)
- checks whether a DefaultCase is defined for this tagNameWithDefaultCase(tag)
- returns name formatted in DefaultCasealso option 2in1
NameWithDefaultCase(tag) (string, bool)
How to define
I thougt defining it with in defaultcase.txt, comments in structs.go and go.mod
I mostly like it with-in go.mod
These are my ideas on how to implement it. It may not be perfect. And I'm not saying This is the only way to do it.
More Realistic Usage
I love Golang
I just want to improve it a little bit
The text was updated successfully, but these errors were encountered: