Skip to content

Commit

Permalink
CLEANUP/MINOR: add inline json comment to embedded structs
Browse files Browse the repository at this point in the history
  • Loading branch information
oktalz authored and mjuraga committed Dec 12, 2023
1 parent b9d3318 commit 4ad4353
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 6 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
79 changes: 79 additions & 0 deletions cmd/struct_tags_checker/args.go
Original file line number Diff line number Diff line change
@@ -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
}
71 changes: 71 additions & 0 deletions cmd/struct_tags_checker/main.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion models/bind.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion models/default_bind.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion models/default_server.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion models/global.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion models/server.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion models/server_template.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4ad4353

Please sign in to comment.