-
-
Notifications
You must be signed in to change notification settings - Fork 413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: codegen to fetch from env without reflect #193
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Exprgen | ||
|
||
## Install | ||
``` | ||
go install github.com/antonmedv/expr/exprgen | ||
``` | ||
## Usage | ||
Fetch methods generates for all struct/map/array/string named types(exception is map types with unnamed not basic key type like `map[struct{...}]int`). | ||
|
||
To generate just call exprgen with pkg paths as arguments: | ||
``` | ||
exprgen pkg1 pkg2 ... | ||
``` | ||
|
||
After call, file `*pkg_name*_exprgen.go` will be created in each packages from arguments. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,351 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"go/ast" | ||
"go/format" | ||
"go/importer" | ||
"go/parser" | ||
"go/token" | ||
"go/types" | ||
"io/fs" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"sort" | ||
"strings" | ||
) | ||
|
||
const exprgenSuffix = "_exprgen.go" | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
filenames := flag.Args() | ||
if len(filenames) == 0 { | ||
flag.Usage() | ||
os.Exit(1) | ||
} | ||
|
||
for _, filename := range filenames { | ||
if err := generate(filename); err != nil { | ||
fmt.Fprintf(os.Stderr, "generate '%s' error: %s", filename, err.Error()) | ||
os.Exit(2) | ||
} | ||
} | ||
} | ||
|
||
func generate(filename string) error { | ||
fi, err := os.Stat(filename) | ||
if err != nil { | ||
return fmt.Errorf("stat err: %w", err) | ||
} | ||
|
||
if !fi.IsDir() { | ||
return fmt.Errorf("filename must be dir") | ||
} | ||
tfs := token.NewFileSet() | ||
packages, err := parser.ParseDir(tfs, filename, func(info fs.FileInfo) bool { | ||
return !strings.HasSuffix(info.Name(), exprgenSuffix) | ||
}, parser.ParseComments) | ||
if err != nil { | ||
return fmt.Errorf("parse dir error: %w", err) | ||
} | ||
|
||
typesChecker := types.Config{ | ||
Importer: importer.ForCompiler(tfs, "source", nil), | ||
} | ||
|
||
for name, pkg := range packages { | ||
if strings.HasSuffix(name, "_test") { | ||
continue | ||
} | ||
|
||
files := make([]*ast.File, 0, len(pkg.Files)) | ||
for _, f := range pkg.Files { | ||
files = append(files, f) | ||
} | ||
|
||
packageTypes, err := typesChecker.Check(name, tfs, files, nil) | ||
if err != nil { | ||
return fmt.Errorf("types check error: %w", err) | ||
} | ||
|
||
b, err := fileData(name, packageTypes) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = ioutil.WriteFile(filepath.Join(filename, name+exprgenSuffix), b, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func fileData(pkgName string, pkg *types.Package) ([]byte, error) { | ||
var data string | ||
echo := func(s string, xs ...interface{}) { | ||
data += fmt.Sprintf(s, xs...) + "\n" | ||
} | ||
echoRaw := func(s string) { | ||
data += fmt.Sprint(s) + "\n" | ||
} | ||
|
||
echo(`// Code generated by exprgen. DO NOT EDIT.`) | ||
echo(``) | ||
echo(`package ` + pkgName) | ||
echo(``) | ||
echo(`--imports`) | ||
echo(``) | ||
|
||
echoRaw(`func toInt(a interface{}) int { | ||
switch x := a.(type) { | ||
case float32: | ||
return int(x) | ||
case float64: | ||
return int(x) | ||
|
||
case int: | ||
return x | ||
case int8: | ||
return int(x) | ||
case int16: | ||
return int(x) | ||
case int32: | ||
return int(x) | ||
case int64: | ||
return int(x) | ||
|
||
case uint: | ||
return int(x) | ||
case uint8: | ||
return int(x) | ||
case uint16: | ||
return int(x) | ||
case uint32: | ||
return int(x) | ||
case uint64: | ||
return int(x) | ||
|
||
default: | ||
panic(fmt.Sprintf("invalid operation: int(%T)", x)) | ||
} | ||
}`) | ||
echo(``) | ||
|
||
imports := make(map[string]string) | ||
|
||
scope := pkg.Scope() | ||
for _, objectName := range scope.Names() { | ||
obj := scope.Lookup(objectName) | ||
namedType, ok := obj.Type().(*types.Named) | ||
if !ok { | ||
continue | ||
} | ||
|
||
recvName := "v" | ||
for i := 0; i < namedType.NumMethods(); i++ { | ||
method := namedType.Method(i) | ||
signature := method.Type().(*types.Signature) | ||
recv := signature.Recv() | ||
if recv != nil && recv.Name() != "" { | ||
recvName = recv.Name() | ||
break | ||
} | ||
} | ||
|
||
switch t := namedType.Underlying().(type) { | ||
case *types.Basic: | ||
if t.Kind() != types.String { | ||
break | ||
} | ||
|
||
echo("func (%s %s) Fetch(i interface{}) interface{} {", recvName, objectName) | ||
echo("return %s[toInt(i)]", recvName) | ||
echo("}") | ||
case *types.Slice, *types.Array: | ||
echo("func (%s %s) Fetch(i interface{}) interface{} {", recvName, objectName) | ||
echo("return %s[toInt(i)]", recvName) | ||
echo("}") | ||
case *types.Map: | ||
echo("func (%s %s) Fetch(i interface{}) interface{} {", recvName, objectName) | ||
key := t.Key() | ||
|
||
numericCases := []string{ | ||
"int", | ||
"int8", | ||
"int16", | ||
"int32", | ||
"int64", | ||
"uint", | ||
"uint8", | ||
"uint16", | ||
"uint32", | ||
"uint64", | ||
"uintptr", | ||
"float32", | ||
"float64", | ||
} | ||
|
||
switch k := key.(type) { | ||
case *types.Named: | ||
objKey := k.Obj() | ||
keyName := objKey.Name() | ||
if objKey.Pkg().Path() != pkg.Path() { | ||
path := objKey.Pkg().Path() | ||
name := objKey.Pkg().Name() | ||
for imports[name] != "" && path != imports[name] { | ||
name = name + "1" | ||
} | ||
imports[name] = path | ||
keyName = name + "." + keyName | ||
} | ||
|
||
echo(`switch _x_i := i.(type) {`) | ||
echo("case %s:", keyName) | ||
echo("return %s[_x_i]", recvName) | ||
if basicKey, ok := k.Underlying().(*types.Basic); ok { | ||
if basicKey.Info()&types.IsNumeric != 0 { | ||
for _, c := range numericCases { | ||
echo("case %s:", c) | ||
echo("return %s[%s(_x_i)]", recvName, keyName) | ||
} | ||
} | ||
if basicKey.Info()&types.IsString != 0 { | ||
echo(`case string:`) | ||
echo("return %s[%s(_x_i)]", recvName, keyName) | ||
echo("default:") | ||
imports["fmt"] = "fmt" | ||
echo("return %s[%s(fmt.Sprint(i))]", recvName, keyName) | ||
} | ||
} | ||
echo(`}`) | ||
case *types.Basic: | ||
keyName := k.String() | ||
echo(`switch _x_i := i.(type) {`) | ||
echo("case %s:", keyName) | ||
echo("return %s[_x_i]", recvName) | ||
if k.Info()&types.IsNumeric != 0 { | ||
for _, c := range numericCases { | ||
if c == keyName { | ||
continue | ||
} | ||
echo("case %s:", c) | ||
echo("return %s[%s(_x_i)]", recvName, keyName) | ||
} | ||
} | ||
|
||
if k.Info()&types.IsString != 0 { | ||
echo("default:") | ||
imports["fmt"] = "fmt" | ||
echo("return %s[%s(fmt.Sprint(i))]", recvName, keyName) | ||
} | ||
|
||
echo(`}`) | ||
} | ||
echo("return nil") | ||
echo(`}`) | ||
case *types.Struct: | ||
echo("func (%s %s) Fetch(i interface{}) interface{} {", recvName, objectName) | ||
|
||
fields := make(map[string]string) | ||
collectStruct(recvName, t, func(c string, r string) { | ||
if _, ok := fields[c]; ok { | ||
fields[c] = "-" | ||
} | ||
fields[c] = r | ||
}) | ||
|
||
keys := make([]string, 0, len(fields)) | ||
for c, r := range fields { | ||
if r == "-" { | ||
continue | ||
} | ||
keys = append(keys, c) | ||
} | ||
sort.Strings(keys) | ||
imports["fmt"] = "fmt" | ||
|
||
echo(`var string_i string`) | ||
echo(`if s, ok := i.(string); ok {`) | ||
echo(`string_i = s`) | ||
echo(`} else {`) | ||
echo(`string_i = fmt.Sprint(i)`) | ||
echo(`}`) | ||
|
||
echo(`switch string_i {`) | ||
for _, key := range keys { | ||
echo("case \"%s\":", key) | ||
echo("return %s", fields[key]) | ||
} | ||
echo(`}`) | ||
echo(`return nil`) | ||
echo(`}`) | ||
} | ||
} | ||
|
||
importsString := "import (\n" | ||
for k, v := range imports { | ||
importsString += k + "\"" + v + "\"\n" | ||
} | ||
importsString += ")" | ||
data = strings.Replace(data, "--imports", importsString, 1) | ||
|
||
return format.Source([]byte(data)) | ||
} | ||
|
||
func collectStruct(recv string, t *types.Struct, collect func(string, string), skippedNames ...string) { | ||
fieldNames := make([]string, 0, t.NumFields()) | ||
for i := 0; i < t.NumFields(); i++ { | ||
fieldNames = append(fieldNames, t.Field(i).Name()) | ||
} | ||
|
||
for i := 0; i < t.NumFields(); i++ { | ||
v := t.Field(i) | ||
if !v.Exported() || contains(skippedNames, v.Name()) { | ||
continue | ||
} | ||
|
||
collect(v.Name(), recv+"."+v.Name()) | ||
|
||
if v.Embedded() { | ||
tt := v.Type() | ||
for dereference(tt) != underlying(tt) { | ||
tt = dereference(tt) | ||
tt = underlying(tt) | ||
} | ||
|
||
switch vt := tt.(type) { | ||
case *types.Struct: | ||
collectStruct(recv+"."+v.Name(), vt, collect, fieldNames...) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func dereference(t types.Type) types.Type { | ||
if p, ok := t.(*types.Pointer); ok { | ||
return dereference(p.Elem()) | ||
} | ||
return t | ||
} | ||
|
||
func underlying(t types.Type) types.Type { | ||
if t != t.Underlying() { | ||
return underlying(t.Underlying()) | ||
} | ||
return t | ||
} | ||
|
||
func contains(arr []string, s string) bool { | ||
for _, e := range arr { | ||
if e == s { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about exporting it from vm.ToInt?