Use Gozz to Manage Your Golang Struct Field Tags Formats Automatically
Just-maple
Posted on October 27, 2023
In Go developing, we would design structs and fill their struct tag in many case.
For example, json:something
for json marshal and unmarshal:
package x
type T struct{
MyField string `json:"my_field"`
}
As we known, naming is a big problem in programing. When we fill these tag, we would face these questions as follows:
- Which case should use,
camelCase
orsnake_case
? - If
camelCase
used. how to format words likeUserID
?userId
oruserID
? - Struct fields could be added and their name could be modified also. How to quickly updated these field tag as format your desired, and may be there would be many tag to update.
- If project would be cooperated with peoples. How to ensure every members would follow these convention, and never need to remind these rules again and again.
Even we could design lint tool to validate these tag is whether desired in our pipeline, but I believe that : The best way to manage these struct fields tags is never manage it in manually, but in instrumentally.
You could try Gozz
to do this more simply:
Here is a normal case, we want to add json
and bson
tag for our struct.
package tag01
// +zz:tag:json,bson:{{ snake .FieldName }}
type User struct {
Id string
Name string
Address string
CreatedAt time.Time
UpdatedAt time.Time
}
add the annotation on struct : +zz:tag:json,bson:{{ snake .FieldName}}
and then execute gozz run -p "tag" ./
You would find out these stuff is solved and nobody would be troubled anymore.
package tag01
// +zz:tag:json,bson:{{ snake .FieldName }}
type User struct {
Id string `bson:"id" json:"id"`
Name string `bson:"name" json:"name"`
Address string `bson:"address" json:"address"`
CreatedAt time.Time `bson:"created_at" json:"created_at"`
UpdatedAt time.Time `bson:"updated_at" json:"updated_at"`
}
Just put these script in project’s makefile and execute them in projects’s build pipeline or commit hooks. Time Is Saved.
The upper example show what is the basic usage of gozz-tag
, this example would show how it strong could be:
- It placed the annotation both on block declaration ,struct declaration, and also struct field.
- And it use different cases and pipeline method in
go-template
// +zz:tag:json,bson:{{ snake .FieldName }}
type (
User struct {
Id string
Name string
Address string
CreatedAt time.Time
UpdatedAt time.Time
}
// +zz:tag:json,bson:{{ camel .FieldName }}
Book struct {
Id string
Title string
CreatedAt time.Time
UpdatedAt time.Time
}
Order struct {
Id string
UserId string
BookId string
// +zz:tag:json,bson:{{ upper .FieldName | upper }}
CreatedAt time.Time
// +zz:tag:+json:,omitempty
UpdatedAt time.Time
}
)
Execute gozz
again and see what is happened:
// +zz:tag:json,bson:{{ snake .FieldName }}
type (
User struct {
Id string `bson:"id" json:"id"`
Name string `bson:"name" json:"name"`
Address string `bson:"address" json:"address"`
CreatedAt time.Time `bson:"created_at" json:"created_at"`
UpdatedAt time.Time `bson:"updated_at" json:"updated_at"`
}
// +zz:tag:json,bson:{{ camel .FieldName }}
Book struct {
Id string `bson:"id" json:"id"`
Title string `bson:"title" json:"title"`
CreatedAt time.Time `bson:"createdAt" json:"createdAt"`
UpdatedAt time.Time `bson:"updatedAt" json:"updatedAt"`
}
Order struct {
Id string `bson:"id" json:"id"`
UserId string `bson:"user_id" json:"user_id"`
BookId string `bson:"book_id" json:"book_id"`
// +zz:tag:json,bson:{{ snake .FieldName | upper }}
CreatedAt time.Time `bson:"CREATED_AT" json:"CREATED_AT"`
// +zz:tag:+json:,omitempty
UpdatedAt time.Time `bson:"updated_at" json:"updated_at,omitempty"`
}
)
Every different needs was solved.
Some I believe that, with the help of Gozz
, we do not need to manage every struct fields in coding , just put them to Gozz
and pipeline.
Actually, this function is just one of the builtin plugins of Gozz
, it provides many other more awesome plugins to help us improve developing with the help of annotations, such as autowired , api routing , orm structure generation .
Just checkout to github and documentation , and it’s welcome to give us a star .
Posted on October 27, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.