-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
- Loading branch information
There are no files selected for viewing
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 | ||
} |
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 | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.