-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify code generation logic for a faster and better UX (#12493)
The changes here simplify the string of targets executed as part of `make gen`. The simplifications include: * parallelised code generation whenever possible * re-implementation of documentation generation for a significant simplification and improved readability. * unified mocks generation to avoid multiple slow calls to `go run` per package. Note, the changes introduced here are purely mechanical and do not alter the Lotus node functionality. Fixes #8392
- Loading branch information
Showing
31 changed files
with
1,503 additions
and
1,468 deletions.
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
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
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 |
---|---|---|
@@ -1,121 +1,24 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"reflect" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/filecoin-project/lotus/api/docgen" | ||
) | ||
|
||
func main() { | ||
comments, groupComments := docgen.ParseApiASTInfo(os.Args[1], os.Args[2], os.Args[3], os.Args[4]) | ||
|
||
groups := make(map[string]*docgen.MethodGroup) | ||
|
||
_, t, permStruct := docgen.GetAPIType(os.Args[2], os.Args[3]) | ||
|
||
for i := 0; i < t.NumMethod(); i++ { | ||
m := t.Method(i) | ||
|
||
groupName := docgen.MethodGroupFromName(m.Name) | ||
|
||
g, ok := groups[groupName] | ||
if !ok { | ||
g = new(docgen.MethodGroup) | ||
g.Header = groupComments[groupName] | ||
g.GroupName = groupName | ||
groups[groupName] = g | ||
} | ||
|
||
var args []interface{} | ||
ft := m.Func.Type() | ||
for j := 2; j < ft.NumIn(); j++ { | ||
inp := ft.In(j) | ||
args = append(args, docgen.ExampleValue(m.Name, inp, nil)) | ||
} | ||
|
||
v, err := json.MarshalIndent(args, "", " ") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
outv := docgen.ExampleValue(m.Name, ft.Out(0), nil) | ||
|
||
ov, err := json.MarshalIndent(outv, "", " ") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
g.Methods = append(g.Methods, &docgen.Method{ | ||
Name: m.Name, | ||
Comment: comments[m.Name], | ||
InputExample: string(v), | ||
ResponseExample: string(ov), | ||
}) | ||
} | ||
|
||
var groupslice []*docgen.MethodGroup | ||
for _, g := range groups { | ||
groupslice = append(groupslice, g) | ||
} | ||
|
||
sort.Slice(groupslice, func(i, j int) bool { | ||
return groupslice[i].GroupName < groupslice[j].GroupName | ||
}) | ||
|
||
fmt.Printf("# Groups\n") | ||
|
||
for _, g := range groupslice { | ||
fmt.Printf("* [%s](#%s)\n", g.GroupName, g.GroupName) | ||
for _, method := range g.Methods { | ||
fmt.Printf(" * [%s](#%s)\n", method.Name, method.Name) | ||
} | ||
} | ||
|
||
for _, g := range groupslice { | ||
g := g | ||
fmt.Printf("## %s\n", g.GroupName) | ||
fmt.Printf("%s\n\n", g.Header) | ||
|
||
sort.Slice(g.Methods, func(i, j int) bool { | ||
return g.Methods[i].Name < g.Methods[j].Name | ||
}) | ||
|
||
for _, m := range g.Methods { | ||
fmt.Printf("### %s\n", m.Name) | ||
fmt.Printf("%s\n\n", m.Comment) | ||
|
||
var meth reflect.StructField | ||
var ok bool | ||
for _, ps := range permStruct { | ||
meth, ok = ps.FieldByName(m.Name) | ||
if ok { | ||
break | ||
} | ||
} | ||
if !ok { | ||
panic("no perms for method: " + m.Name) | ||
} | ||
|
||
perms := meth.Tag.Get("perm") | ||
|
||
fmt.Printf("Perms: %s\n\n", perms) | ||
|
||
if strings.Count(m.InputExample, "\n") > 0 { | ||
fmt.Printf("Inputs:\n```json\n%s\n```\n\n", m.InputExample) | ||
} else { | ||
fmt.Printf("Inputs: `%s`\n\n", m.InputExample) | ||
} | ||
|
||
if strings.Count(m.ResponseExample, "\n") > 0 { | ||
fmt.Printf("Response:\n```json\n%s\n```\n\n", m.ResponseExample) | ||
} else { | ||
fmt.Printf("Response: `%s`\n\n", m.ResponseExample) | ||
} | ||
} | ||
var ( | ||
apiFile = os.Args[1] | ||
iface = os.Args[2] | ||
pkg = os.Args[3] | ||
dir = os.Args[4] | ||
) | ||
if ainfo, err := docgen.ParseApiASTInfo(apiFile, iface, pkg, dir); err != nil { | ||
_, _ = fmt.Fprintf(os.Stderr, "Failed to parse API AST info: %v\n", err) | ||
os.Exit(1) | ||
} else if err := docgen.Generate(os.Stdout, iface, pkg, ainfo); err != nil { | ||
_, _ = fmt.Fprintf(os.Stderr, "Failed to generate docs: %v\n", err) | ||
os.Exit(1) | ||
} | ||
} |
Oops, something went wrong.