Too many field tags, how to split definitions ?

112 views
Skip to first unread message

skat...@root.gg

unread,
Jan 24, 2017, 8:30:58 AM1/24/17
to golang-nuts
Hello,
I'm looking for a way to split fields tags in multiple files.

My application can work with different backends (
mongodb / sql / storm) that could persist the same objects. Add json for the Rest API and the fields tags definitions start to be really ugly.

type MyStruct struct {
 ID string `json:"id" bson:"id" gorm:"column:id;primary_key" storm:"unique"`
 
Creation int64 `json:"uploadDate" bson:"uploadDate" gorm:"column:uploadDate" storm:"index"`
 TTL int `json:"ttl" bson:"ttl" gorm:"column:ttl"`
  ...
}

Backend code is splitted into modules and I'd like that each module define tags it needs. 

Does anyone sees a solution ?

Jordan Krage

unread,
Jan 24, 2017, 8:42:57 AM1/24/17
to golang-nuts, skat...@root.gg
As of Go 1.8, this should be easy.  You can now convert between two structs which differ only in tags (1.8 release notes), so you can have a separate struct definition for each type (json, bson, gorm, strom, etc.).

skat...@root.gg

unread,
Jan 24, 2017, 3:00:24 PM1/24/17
to golang-nuts, skat...@root.gg
Perfect.
Reply all
Reply to author
Forward
0 new messages