add fieldaligment

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-12-26 01:09:11 +03:00
parent f6cfb2de1e
commit 8afbf92aea
4 changed files with 82 additions and 16 deletions

24
fiealaligment.go Normal file
View File

@ -0,0 +1,24 @@
package main
import (
"log"
"os"
"golang.org/x/tools/go/analysis/passes/fieldalignment"
"golang.org/x/tools/go/analysis/singlechecker"
"google.golang.org/protobuf/compiler/protogen"
)
func (g *Generator) fieldAlign(plugin *protogen.Plugin) error {
if !g.fieldaligment {
return nil
}
log.Printf("%v\n", []string{"fieldalignment", "-fix", g.tagPath})
origArgs := os.Args
os.Args = []string{"fieldalignment", "-fix", g.tagPath}
singlechecker.Main(fieldalignment.Analyzer)
os.Args = origArgs
return nil
}

2
go.mod
View File

@ -5,7 +5,7 @@ go 1.16
require (
github.com/fatih/structtag v1.2.0
go.unistack.org/micro-proto/v3 v3.1.0
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
golang.org/x/tools v0.1.8
google.golang.org/protobuf v1.27.1
)

26
go.sum
View File

@ -3,8 +3,34 @@ github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.unistack.org/micro-proto/v3 v3.1.0 h1:q39FwjFiRZn+Ux/tt+d3bJTmDtsQQWa+3SLYVo1vLfA=
go.unistack.org/micro-proto/v3 v3.1.0/go.mod h1:DpRhYCBXlmSJ/AAXTmntvlh7kQkYU6eFvlmYAx4BQS8=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38=
golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 h1:id054HUawV2/6IGm2IV8KZQjqtwAOo2CYlOToYqa0d0=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.8 h1:P1HhGGuLW4aAclzjtmJdf0mJOjVUZUzOTqkAkWL+l6w=
golang.org/x/tools v0.1.8/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

36
main.go
View File

@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"os"
"strings"
"google.golang.org/protobuf/compiler/protogen"
@ -10,23 +11,25 @@ import (
)
var (
flagDebug = flag.Bool("debug", false, "debug output")
flagStandalone = flag.Bool("standalone", false, "generate file to standalone dir")
flagComponents = flag.String("components", "micro|rpc|http|client|server|openapiv3", "specify components to generate")
flagTagPath = flag.String("tag_path", "", "tag rewriting dir")
flagOpenapiFile = flag.String("openapi_file", "apidocs.swagger.json", "openapi file name")
flagHelp = flag.Bool("help", false, "display help message")
flagSet = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
flagDebug = flagSet.Bool("debug", false, "debug output")
flagStandalone = flagSet.Bool("standalone", false, "generate file to standalone dir")
flagFieldaligment = flagSet.Bool("fieldaligment", false, "align struct fields in generated code")
flagComponents = flagSet.String("components", "micro|rpc|http|client|server|openapiv3", "specify components to generate")
flagTagPath = flagSet.String("tag_path", "", "tag rewriting dir")
flagOpenapiFile = flagSet.String("openapi_file", "apidocs.swagger.json", "openapi file name")
flagHelp = flagSet.Bool("help", false, "display help message")
)
func main() {
opts := &protogen.Options{
ParamFunc: flag.CommandLine.Set,
ParamFunc: flagSet.Set,
}
flag.Parse()
flagSet.Parse(os.Args[1:])
if *flagHelp {
flag.PrintDefaults()
flagSet.PrintDefaults()
return
}
@ -39,6 +42,7 @@ type Generator struct {
components string
standalone bool
debug bool
fieldaligment bool
tagPath string
openapiFile string
}
@ -49,12 +53,14 @@ func (g *Generator) Generate(plugin *protogen.Plugin) error {
g.standalone = *flagStandalone
g.debug = *flagDebug
g.components = *flagComponents
g.fieldaligment = *flagFieldaligment
g.tagPath = *flagTagPath
g.openapiFile = *flagOpenapiFile
plugin.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
var genClient bool
var genServer bool
var genNone bool
if strings.Contains(g.components, "server") {
genServer = true
@ -62,8 +68,11 @@ func (g *Generator) Generate(plugin *protogen.Plugin) error {
if strings.Contains(g.components, "client") {
genClient = true
}
if strings.Contains(g.components, "none") {
genNone = true
}
if strings.Contains(g.components, "rpc") || strings.Contains(g.components, "http") {
if !genServer && !genClient {
if !genServer && !genClient && !genNone {
genServer = true
genClient = true
}
@ -86,6 +95,8 @@ func (g *Generator) Generate(plugin *protogen.Plugin) error {
err = g.chiGenerate(component, plugin)
case "openapiv3":
err = g.openapiv3Generate(component, plugin)
case "none":
break
default:
err = fmt.Errorf("unknown component: %s", component)
}
@ -102,5 +113,10 @@ func (g *Generator) Generate(plugin *protogen.Plugin) error {
return err
}
if err = g.fieldAlign(plugin); err != nil {
plugin.Error(err)
return err
}
return nil
}