Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/lawrsp/pigo

some go generate tools
https://github.com/lawrsp/pigo

builder generate go parser

Last synced: about 2 months ago
JSON representation

some go generate tools

Awesome Lists containing this project

README

        

# pigo
some go generate tools
[![Go Reference](https://pkg.go.dev/badge/github.com/lawrsp/pigo.svg)](https://pkg.go.dev/github.com/lawrsp/pigo)

# what is

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**

# for now
There are 8 generators:

1. checker: use struct tags to generate struct validate function, e.g noempty, compare, valid function, set default..
2. convert: convert one struct to another by field name or specified tags
3. evalid: enum validate, check if a value is one of the consts
4. setter: set one struct to another, support value diff, old/new collects
5. setdb: set the DB object which implement chainable Where clause (e.g. gorm)
6. jsonfield: conver a map[FieldName] map[jsonname] according the struct's json tag
7. genrpc: write some gRpc glue code to connect service layer and api layer
8. pfilter: a version of gRpc donnot support optional value, it must use a fieldmask to work with nil

# instal

go get github.com/lawrsp/pigo

# run

pigo help

pigo help xxxx

# example

```golang

//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:

```golang
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
}

```