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"
|
|
|
|
|
|
|
|
"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
|
|
|
|
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 {
|
2022-09-06 15:49:29 +00:00
|
|
|
Num string
|
2021-04-03 11:12:50 +00:00
|
|
|
node ast.Node
|
|
|
|
Tags map[string][]string
|
|
|
|
NamedParams, ParamNames, Results, DefRes string
|
2021-03-23 12:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type strinfo struct {
|
2022-09-06 15:49:29 +00:00
|
|
|
Num string
|
2021-03-23 12:42:56 +00:00
|
|
|
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{
|
2022-09-06 15:49:29 +00:00
|
|
|
Num: ifname,
|
2021-03-23 12:42:56 +00:00
|
|
|
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{
|
2022-09-06 15:49:29 +00:00
|
|
|
Num: mname,
|
2021-03-23 12:42:56 +00:00
|
|
|
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
|
|
|
|
}
|
2023-01-16 13:54:26 +00:00
|
|
|
if tf[0] != "perm" && tf[0] != "rpc_method" && tf[0] != "notify" { // todo: allow more tag types
|
2021-03-23 12:42:56 +00:00
|
|
|
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}}
|
2022-09-06 15:49:29 +00:00
|
|
|
type {{.Num}}Struct struct {
|
2021-03-23 12:42:56 +00:00
|
|
|
{{range .Include}}
|
|
|
|
{{.}}Struct
|
|
|
|
{{end}}
|
2023-01-16 13:54:26 +00:00
|
|
|
Internal {{.Num}}Methods
|
|
|
|
}
|
|
|
|
|
|
|
|
type {{.Num}}Methods struct {
|
2021-03-23 12:42:56 +00:00
|
|
|
{{range .Methods}}
|
2023-02-17 23:06:58 +00:00
|
|
|
{{.Num}} func({{.NamedParams}}) ({{.Results}}) `+"`"+`{{$first := true}}{{range .Tags}}{{if $first}}{{$first = false}}{{else}} {{end}}{{index . 0}}:"{{index . 1}}"{{end}}`+"`"+`
|
|
|
|
|
2021-03-23 12:42:56 +00:00
|
|
|
{{end}}
|
|
|
|
}
|
2021-04-03 11:12:50 +00:00
|
|
|
|
2022-09-06 15:49:29 +00:00
|
|
|
type {{.Num}}Stub struct {
|
2021-04-03 11:12:50 +00:00
|
|
|
{{range .Include}}
|
|
|
|
{{.}}Stub
|
|
|
|
{{end}}
|
|
|
|
}
|
2021-03-23 12:42:56 +00:00
|
|
|
{{end}}
|
|
|
|
|
|
|
|
{{range .Infos}}
|
2022-09-06 15:49:29 +00:00
|
|
|
{{$name := .Num}}
|
2021-03-23 12:42:56 +00:00
|
|
|
{{range .Methods}}
|
2022-09-06 15:49:29 +00:00
|
|
|
func (s *{{$name}}Struct) {{.Num}}({{.NamedParams}}) ({{.Results}}) {
|
|
|
|
if s.Internal.{{.Num}} == nil {
|
2021-07-23 23:16:42 +00:00
|
|
|
return {{.DefRes}}ErrNotSupported
|
|
|
|
}
|
2022-09-06 15:49:29 +00:00
|
|
|
return s.Internal.{{.Num}}({{.ParamNames}})
|
2021-03-23 12:42:56 +00:00
|
|
|
}
|
2021-04-03 11:12:50 +00:00
|
|
|
|
2022-09-06 15:49:29 +00:00
|
|
|
func (s *{{$name}}Stub) {{.Num}}({{.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}}
|
|
|
|
|
2022-09-06 15:49:29 +00:00
|
|
|
{{range .Infos}}var _ {{.Num}} = new({{.Num}}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)
|
|
|
|
}
|