diff --git a/Makefile b/Makefile index da9dfa06..d8137b5c 100644 --- a/Makefile +++ b/Makefile @@ -28,6 +28,7 @@ models: spec swagger-check rm -rf models/*_compare.go rm -rf models/*_compare_test.go go run cmd/struct_equal_generator/*.go -l ${PROJECT_PATH}/specification/copyright.txt ${PROJECT_PATH}/models + go run cmd/struct_tags_checker/*.go -l ${PROJECT_PATH}/specification/copyright.txt ${PROJECT_PATH}/models .PHONY: swagger-check swagger-check: diff --git a/cmd/struct_tags_checker/args.go b/cmd/struct_tags_checker/args.go new file mode 100644 index 00000000..987ab542 --- /dev/null +++ b/cmd/struct_tags_checker/args.go @@ -0,0 +1,79 @@ +package main + +import ( + _ "embed" + "fmt" + "io/fs" + "log" + "os" + "path" + "path/filepath" + "strings" +) + +type Args struct { + Directory string + Selector string + Files []string +} + +func (a *Args) Parse() error { //nolint:gocognit,unparam + selector, err := os.Getwd() + if err != nil { + log.Panic(err) + } + for i := 1; i < len(os.Args); i++ { + selector = os.Args[i] + } + + isDirectory := false + file, err := os.Open(selector) + if err == nil { + var fileInfo fs.FileInfo + fileInfo, err = file.Stat() + if err == nil { + isDirectory = fileInfo.IsDir() + } + } + + if selector == "*" || selector == "." || isDirectory { + err = filepath.Walk(selector, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if info.IsDir() { + return nil + } + if strings.HasSuffix(path, "_compare.go") { + return nil + } + if strings.HasSuffix(path, "_test.go") { + return nil + } + if strings.HasSuffix(path, "_easyjson.go") { + return nil + } + if strings.HasSuffix(path, "_generated.go") { + return nil + } + fmt.Println(path) //nolint:forbidigo + if strings.HasSuffix(path, ".go") { + a.Files = append(a.Files, path) + } + return nil + }) + if err != nil { + log.Panic(err) + } + } else { + a.Files = append(a.Files, selector) + } + a.Selector = selector + if isDirectory { + a.Directory = selector + } else { + a.Directory = path.Dir(selector) + } + + return nil +} diff --git a/cmd/struct_tags_checker/main.go b/cmd/struct_tags_checker/main.go new file mode 100644 index 00000000..0bc22c8d --- /dev/null +++ b/cmd/struct_tags_checker/main.go @@ -0,0 +1,71 @@ +package main + +import ( + _ "embed" + "go/token" + "log" + "os" + + "github.com/sirkon/dst" + "github.com/sirkon/dst/decorator" +) + +func main() { + // tool to add `json:",inline"` for embedded structs + + args := Args{} + err := args.Parse() + if err != nil { + log.Panic(err) + } + for _, fileName := range args.Files { + err = generate(fileName) + if err != nil { + log.Panic(err) + } + } +} + +func generate(fileName string) error { //nolint:gocognit,unparam + f, err := decorator.ParseFile(token.NewFileSet(), fileName, nil, 0) + if err != nil { + log.Fatal(err) + } + + for _, decl := range f.Decls { + if genDecl, ok := decl.(*dst.GenDecl); ok && genDecl.Tok == token.TYPE { + for _, spec := range genDecl.Specs { + if typeSpec, ok := spec.(*dst.TypeSpec); ok { + if structType, ok := typeSpec.Type.(*dst.StructType); ok { + for _, field := range structType.Fields.List { + if len(field.Names) == 0 { + // This is an embedded struct. + if field.Tag == nil { + log.Printf("Embedded struct found with no tag: %s: %+v\n", fileName, field.Type) + field.Tag = &dst.BasicLit{ + Kind: token.STRING, + Value: "`json:\",inline\"`", + } + continue + } + } + } + } + } + } + } + } + + outputFile, err := os.Create(fileName) + if err != nil { + log.Fatal(err) + } + + err = decorator.Fprint(outputFile, f) + if err != nil { + log.Fatal(err) + } + defer outputFile.Close() + + return nil +} diff --git a/go.mod b/go.mod index ba202b6c..1b2d135f 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 github.com/mitchellh/mapstructure v1.5.0 github.com/pkg/errors v0.9.1 + github.com/sirkon/dst v0.26.4 github.com/stretchr/testify v1.8.4 golang.org/x/text v0.14.0 golang.org/x/tools v0.16.0 diff --git a/go.sum b/go.sum index 50c39236..47f121ae 100644 --- a/go.sum +++ b/go.sum @@ -91,6 +91,10 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= +github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ= +github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= +github.com/sirkon/dst v0.26.4 h1:ETxfjyp5JKE8OCpdybyyhzTyQqq/MwbIIcs7kxcUAcA= +github.com/sirkon/dst v0.26.4/go.mod h1:e6HRc56jU5F2XT6GB8Cyci1Jb5cjX6gLqrm5+T/P7Zo= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= @@ -125,6 +129,8 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug 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/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= +golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 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= diff --git a/models/bind.go b/models/bind.go index 88d90fe8..7754ab61 100644 --- a/models/bind.go +++ b/models/bind.go @@ -35,7 +35,7 @@ import ( // // swagger:model bind type Bind struct { - BindParams + BindParams `json:",inline"` // address // Example: 127.0.0.1 diff --git a/models/default_bind.go b/models/default_bind.go index ffb9bb48..51a256ef 100644 --- a/models/default_bind.go +++ b/models/default_bind.go @@ -34,7 +34,7 @@ import ( // // swagger:model default_bind type DefaultBind struct { - BindParams + BindParams `json:",inline"` } // UnmarshalJSON unmarshals this object from a JSON structure diff --git a/models/default_server.go b/models/default_server.go index c7c4bc7e..d7041164 100644 --- a/models/default_server.go +++ b/models/default_server.go @@ -32,7 +32,7 @@ import ( // // swagger:model default_server type DefaultServer struct { - ServerParams + ServerParams `json:",inline"` } // UnmarshalJSON unmarshals this object from a JSON structure diff --git a/models/global.go b/models/global.go index 3d9dd381..8ea646c3 100644 --- a/models/global.go +++ b/models/global.go @@ -2573,7 +2573,7 @@ func (m *PresetEnv) UnmarshalBinary(b []byte) error { // // swagger:model RuntimeAPI type RuntimeAPI struct { - BindParams + BindParams `json:",inline"` // address // Required: true diff --git a/models/server.go b/models/server.go index a247d8d8..9bafd5d2 100644 --- a/models/server.go +++ b/models/server.go @@ -36,7 +36,7 @@ import ( // // swagger:model server type Server struct { - ServerParams + ServerParams `json:",inline"` // address // Required: true diff --git a/models/server_template.go b/models/server_template.go index dbd3d62b..9302be58 100644 --- a/models/server_template.go +++ b/models/server_template.go @@ -36,7 +36,7 @@ import ( // // swagger:model server_template type ServerTemplate struct { - ServerParams + ServerParams `json:",inline"` // fqdn // Required: true