2021-03-23 12:42:56 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
"go/parser"
|
|
|
|
"go/token"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
"unicode"
|
|
|
|
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type methodMeta struct {
|
|
|
|
node ast.Node
|
|
|
|
ftype *ast.FuncType
|
|
|
|
}
|
|
|
|
|
|
|
|
type Visitor struct {
|
|
|
|
Methods map[string]map[string]*methodMeta
|
|
|
|
Include map[string][]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Visitor) Visit(node ast.Node) ast.Visitor {
|
|
|
|
st, ok := node.(*ast.TypeSpec)
|
|
|
|
if !ok {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
iface, ok := st.Type.(*ast.InterfaceType)
|
|
|
|
if !ok {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
if v.Methods[st.Name.Name] == nil {
|
|
|
|
v.Methods[st.Name.Name] = map[string]*methodMeta{}
|
|
|
|
}
|
|
|
|
for _, m := range iface.Methods.List {
|
|
|
|
switch ft := m.Type.(type) {
|
|
|
|
case *ast.Ident:
|
|
|
|
v.Include[st.Name.Name] = append(v.Include[st.Name.Name], ft.Name)
|
|
|
|
case *ast.FuncType:
|
|
|
|
v.Methods[st.Name.Name][m.Names[0].Name] = &methodMeta{
|
|
|
|
node: m,
|
|
|
|
ftype: ft,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return v
|
|
|
|
}
|
2021-04-03 11:12:50 +00:00
|
|
|
|
2021-03-23 12:42:56 +00:00
|
|
|
func main() {
|
2021-03-23 16:01:56 +00:00
|
|
|
// latest (v1)
|
2021-03-25 14:11:37 +00:00
|
|
|
if err := generate("./api", "api", "api", "./api/proxy_gen.go"); err != nil {
|
2021-03-23 16:01:56 +00:00
|
|
|
fmt.Println("error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// v0
|
2021-03-25 14:11:37 +00:00
|
|
|
if err := generate("./api/v0api", "v0api", "v0api", "./api/v0api/proxy_gen.go"); err != nil {
|
2021-03-23 12:42:56 +00:00
|
|
|
fmt.Println("error: ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:09:50 +00:00
|
|
|
func typeName(e ast.Expr, pkg string) (string, error) {
|
2021-03-23 12:42:56 +00:00
|
|
|
switch t := e.(type) {
|
|
|
|
case *ast.SelectorExpr:
|
|
|
|
return t.X.(*ast.Ident).Name + "." + t.Sel.Name, nil
|
|
|
|
case *ast.Ident:
|
|
|
|
pstr := t.Name
|
2021-03-25 14:09:50 +00:00
|
|
|
if !unicode.IsLower(rune(pstr[0])) && pkg != "api" {
|
2021-03-23 12:42:56 +00:00
|
|
|
pstr = "api." + pstr // todo src pkg name
|
|
|
|
}
|
|
|
|
return pstr, nil
|
|
|
|
case *ast.ArrayType:
|
2021-03-25 14:09:50 +00:00
|
|
|
subt, err := typeName(t.Elt, pkg)
|
2021-03-23 12:42:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return "[]" + subt, nil
|
|
|
|
case *ast.StarExpr:
|
2021-03-25 14:09:50 +00:00
|
|
|
subt, err := typeName(t.X, pkg)
|
2021-03-23 12:42:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return "*" + subt, nil
|
|
|
|
case *ast.MapType:
|
2021-03-25 14:09:50 +00:00
|
|
|
k, err := typeName(t.Key, pkg)
|
2021-03-23 12:42:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-03-25 14:09:50 +00:00
|
|
|
v, err := typeName(t.Value, pkg)
|
2021-03-23 12:42:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return "map[" + k + "]" + v, nil
|
|
|
|
case *ast.StructType:
|
|
|
|
if len(t.Fields.List) != 0 {
|
|
|
|
return "", xerrors.Errorf("can't struct")
|
|
|
|
}
|
|
|
|
return "struct{}", nil
|
|
|
|
case *ast.InterfaceType:
|
|
|
|
if len(t.Methods.List) != 0 {
|
|
|
|
return "", xerrors.Errorf("can't interface")
|
|
|
|
}
|
|
|
|
return "interface{}", nil
|
|
|
|
case *ast.ChanType:
|
2021-03-25 14:09:50 +00:00
|
|
|
subt, err := typeName(t.Value, pkg)
|
2021-03-23 12:42:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if t.Dir == ast.SEND {
|
|
|
|
subt = "->chan " + subt
|
|
|
|
} else {
|
|
|
|
subt = "<-chan " + subt
|
|
|
|
}
|
|
|
|
return subt, nil
|
|
|
|
default:
|
|
|
|
return "", xerrors.Errorf("unknown type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 16:01:56 +00:00
|
|
|
func generate(path, pkg, outpkg, outfile string) error {
|
2021-03-23 12:42:56 +00:00
|
|
|
fset := token.NewFileSet()
|
2021-03-23 16:01:56 +00:00
|
|
|
apiDir, err := filepath.Abs(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
outfile, err = filepath.Abs(outfile)
|
2021-03-23 12:42:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pkgs, err := parser.ParseDir(fset, apiDir, nil, parser.AllErrors|parser.ParseComments)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-23 16:01:56 +00:00
|
|
|
ap := pkgs[pkg]
|
2021-03-23 12:42:56 +00:00
|
|
|
|
|
|
|
v := &Visitor{make(map[string]map[string]*methodMeta), map[string][]string{}}
|
|
|
|
ast.Walk(v, ap)
|
|
|
|
|
|
|
|
type methodInfo struct {
|
2021-04-03 11:12:50 +00:00
|
|
|
Name string
|
|
|
|
node ast.Node
|
|
|
|
Tags map[string][]string
|
|
|
|
NamedParams, ParamNames, Results, DefRes string
|
2021-03-23 12:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type strinfo struct {
|
|
|
|
Name string
|
|
|
|
Methods map[string]*methodInfo
|
|
|
|
Include []string
|
|
|
|
}
|
|
|
|
|
|
|
|
type meta struct {
|
|
|
|
Infos map[string]*strinfo
|
|
|
|
Imports map[string]string
|
2021-03-23 16:01:56 +00:00
|
|
|
OutPkg string
|
2021-03-23 12:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m := &meta{
|
2021-03-23 16:01:56 +00:00
|
|
|
OutPkg: outpkg,
|
2021-03-23 12:42:56 +00:00
|
|
|
Infos: map[string]*strinfo{},
|
|
|
|
Imports: map[string]string{},
|
|
|
|
}
|
|
|
|
|
|
|
|
for fn, f := range ap.Files {
|
|
|
|
if strings.HasSuffix(fn, "gen.go") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
//fmt.Println("F:", fn)
|
|
|
|
cmap := ast.NewCommentMap(fset, f, f.Comments)
|
|
|
|
|
|
|
|
for _, im := range f.Imports {
|
|
|
|
m.Imports[im.Path.Value] = im.Path.Value
|
2021-03-23 16:01:56 +00:00
|
|
|
if im.Name != nil {
|
|
|
|
m.Imports[im.Path.Value] = im.Name.Name + " " + m.Imports[im.Path.Value]
|
|
|
|
}
|
2021-03-23 12:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for ifname, methods := range v.Methods {
|
|
|
|
if _, ok := m.Infos[ifname]; !ok {
|
|
|
|
m.Infos[ifname] = &strinfo{
|
|
|
|
Name: ifname,
|
|
|
|
Methods: map[string]*methodInfo{},
|
|
|
|
Include: v.Include[ifname],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
info := m.Infos[ifname]
|
|
|
|
for mname, node := range methods {
|
|
|
|
filteredComments := cmap.Filter(node.node).Comments()
|
|
|
|
|
|
|
|
if _, ok := info.Methods[mname]; !ok {
|
|
|
|
var params, pnames []string
|
|
|
|
for _, param := range node.ftype.Params.List {
|
2021-03-25 14:09:50 +00:00
|
|
|
pstr, err := typeName(param.Type, outpkg)
|
2021-03-23 12:42:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c := len(param.Names)
|
|
|
|
if c == 0 {
|
|
|
|
c = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < c; i++ {
|
|
|
|
pname := fmt.Sprintf("p%d", len(params))
|
|
|
|
pnames = append(pnames, pname)
|
|
|
|
params = append(params, pname+" "+pstr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-03 11:12:50 +00:00
|
|
|
results := []string{}
|
2021-03-23 12:42:56 +00:00
|
|
|
for _, result := range node.ftype.Results.List {
|
2021-03-25 14:09:50 +00:00
|
|
|
rs, err := typeName(result.Type, outpkg)
|
2021-03-23 12:42:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
results = append(results, rs)
|
|
|
|
}
|
|
|
|
|
2021-04-03 11:12:50 +00:00
|
|
|
defRes := ""
|
|
|
|
if len(results) > 1 {
|
|
|
|
defRes = results[0]
|
|
|
|
switch {
|
|
|
|
case defRes[0] == '*' || defRes[0] == '<', defRes == "interface{}":
|
|
|
|
defRes = "nil"
|
|
|
|
case defRes == "bool":
|
|
|
|
defRes = "false"
|
|
|
|
case defRes == "string":
|
|
|
|
defRes = `""`
|
|
|
|
case defRes == "int", defRes == "int64", defRes == "uint64", defRes == "uint":
|
|
|
|
defRes = "0"
|
|
|
|
default:
|
|
|
|
defRes = "*new(" + defRes + ")"
|
|
|
|
}
|
|
|
|
defRes += ", "
|
|
|
|
}
|
|
|
|
|
2021-03-23 12:42:56 +00:00
|
|
|
info.Methods[mname] = &methodInfo{
|
|
|
|
Name: mname,
|
|
|
|
node: node.node,
|
|
|
|
Tags: map[string][]string{},
|
|
|
|
NamedParams: strings.Join(params, ", "),
|
|
|
|
ParamNames: strings.Join(pnames, ", "),
|
|
|
|
Results: strings.Join(results, ", "),
|
2021-04-03 11:12:50 +00:00
|
|
|
DefRes: defRes,
|
2021-03-23 12:42:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to parse tag info
|
|
|
|
if len(filteredComments) > 0 {
|
|
|
|
tagstr := filteredComments[len(filteredComments)-1].List[0].Text
|
|
|
|
tagstr = strings.TrimPrefix(tagstr, "//")
|
|
|
|
tl := strings.Split(strings.TrimSpace(tagstr), " ")
|
|
|
|
for _, ts := range tl {
|
|
|
|
tf := strings.Split(ts, ":")
|
|
|
|
if len(tf) != 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if tf[0] != "perm" { // todo: allow more tag types
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
info.Methods[mname].Tags[tf[0]] = tf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*jb, err := json.MarshalIndent(Infos, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println(string(jb))*/
|
|
|
|
|
2021-03-23 16:01:56 +00:00
|
|
|
w, err := os.OpenFile(outfile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-23 12:42:56 +00:00
|
|
|
|
|
|
|
err = doTemplate(w, m, `// Code generated by github.com/filecoin-project/lotus/gen/api. DO NOT EDIT.
|
|
|
|
|
2021-03-23 16:01:56 +00:00
|
|
|
package {{.OutPkg}}
|
2021-03-23 12:42:56 +00:00
|
|
|
|
|
|
|
import (
|
2021-03-23 16:01:56 +00:00
|
|
|
{{range .Imports}} {{.}}
|
2021-03-23 12:42:56 +00:00
|
|
|
{{end}}
|
|
|
|
)
|
|
|
|
`)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = doTemplate(w, m, `
|
2021-07-23 23:16:42 +00:00
|
|
|
|
|
|
|
var ErrNotSupported = xerrors.New("method not supported")
|
|
|
|
|
2021-03-23 12:42:56 +00:00
|
|
|
{{range .Infos}}
|
|
|
|
type {{.Name}}Struct struct {
|
|
|
|
{{range .Include}}
|
|
|
|
{{.}}Struct
|
|
|
|
{{end}}
|
|
|
|
Internal struct {
|
|
|
|
{{range .Methods}}
|
|
|
|
{{.Name}} func({{.NamedParams}}) ({{.Results}}) `+"`"+`{{range .Tags}}{{index . 0}}:"{{index . 1}}"{{end}}`+"`"+`
|
|
|
|
{{end}}
|
|
|
|
}
|
|
|
|
}
|
2021-04-03 11:12:50 +00:00
|
|
|
|
|
|
|
type {{.Name}}Stub struct {
|
|
|
|
{{range .Include}}
|
|
|
|
{{.}}Stub
|
|
|
|
{{end}}
|
|
|
|
}
|
2021-03-23 12:42:56 +00:00
|
|
|
{{end}}
|
|
|
|
|
|
|
|
{{range .Infos}}
|
|
|
|
{{$name := .Name}}
|
|
|
|
{{range .Methods}}
|
|
|
|
func (s *{{$name}}Struct) {{.Name}}({{.NamedParams}}) ({{.Results}}) {
|
2021-07-23 23:16:42 +00:00
|
|
|
if s.Internal.{{.Name}} == nil {
|
|
|
|
return {{.DefRes}}ErrNotSupported
|
|
|
|
}
|
2021-03-23 12:42:56 +00:00
|
|
|
return s.Internal.{{.Name}}({{.ParamNames}})
|
|
|
|
}
|
2021-04-03 11:12:50 +00:00
|
|
|
|
|
|
|
func (s *{{$name}}Stub) {{.Name}}({{.NamedParams}}) ({{.Results}}) {
|
2021-07-23 23:16:42 +00:00
|
|
|
return {{.DefRes}}ErrNotSupported
|
2021-04-03 11:12:50 +00:00
|
|
|
}
|
2021-03-23 12:42:56 +00:00
|
|
|
{{end}}
|
|
|
|
{{end}}
|
|
|
|
|
2021-03-23 16:01:56 +00:00
|
|
|
{{range .Infos}}var _ {{.Name}} = new({{.Name}}Struct)
|
2021-03-23 12:42:56 +00:00
|
|
|
{{end}}
|
|
|
|
|
|
|
|
`)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func doTemplate(w io.Writer, info interface{}, templ string) error {
|
|
|
|
t := template.Must(template.New("").
|
|
|
|
Funcs(template.FuncMap{}).Parse(templ))
|
|
|
|
|
|
|
|
return t.Execute(w, info)
|
|
|
|
}
|