There's always some repetitive, simple code you have to write, that is very annoy.
e.g. when you must assign structs, and you donn't want use reflect.
When the go generator tool comes up, I write pigo, let the generator write some simple code even a pig can write.
That is why it is called pigo
: pig-go
There are 8 generators:
- checker: use struct tags to generate struct validate function, e.g noempty, compare, valid function, set default..
- convert: convert one struct to another by field name or specified tags
- evalid: enum validate, check if a value is one of the consts
- setter: set one struct to another, support value diff, old/new collects
- setdb: set the DB object which implement chainable Where clause (e.g. gorm)
- jsonfield: conver a map[FieldName] map[jsonname] according the struct's json tag
- genrpc: write some gRpc glue code to connect service layer and api layer
- pfilter: a version of gRpc donnot support optional value, it must use a fieldmask to work with nil
go get github.com/lawrsp/pigo
pigo help
pigo help xxxx
//go:generate pigo setter --type A --target B --tagname setter --name Set --withmap --checkdiff --withold --output gen_setters.go $GOFILE
type A struct {
Foo int `setter:"Bar"`
SameName string
}
type B struct {
Bar int
SameName string
}
run go generate
, it will create a file gen_setters.go (--output) and generate codes:
func (t *A) Update(target *B) (map[string]interface{}, map[string]interface{}) {
if target == nil {
return nil, nil
}
old := map[string]interface{}{}
updated := map[string]interface{}{}
if t.Foo != target.Bar {
old["Bar"] = target.Bar
target.Bar = t.Foo
updated["Bar"] = target.Bar
}
if t.SameName != target.SameName {
old["SameName"] = target.SameName
target.SameName = t.SameName
updated["SameName"] = target.SameName
}
return updated, old
}