apigen/lib/loader.go

346 lines
8.5 KiB
Go
Raw Normal View History

2021-09-06 12:54:09 +00:00
package lib
2021-01-30 09:53:34 +00:00
import (
2021-03-22 02:08:04 +00:00
"fmt"
2021-01-30 09:53:34 +00:00
"github.com/fatih/structtag"
"go/ast"
"go/parser"
2021-03-22 02:08:04 +00:00
"go/token"
2021-01-30 09:53:34 +00:00
"log"
2021-03-22 02:08:04 +00:00
"net/http"
2021-04-02 14:40:28 +00:00
"sort"
2021-01-30 09:53:34 +00:00
"strings"
)
var api API
2021-03-22 02:08:04 +00:00
func llog(s string, p ...interface{}) {
fmt.Printf(s+"\n", p...)
}
2021-01-30 09:53:34 +00:00
func addStruct(a *ast.GenDecl) {
md := manageComments(a.Doc)
if md["API"] == "" {
return
}
tp := APIType{
Name: "",
Desc: "",
Fields: make(map[string]*APIField),
Col: md["COL"],
}
tp.Name = a.Specs[0].(*ast.TypeSpec).Name.Name
2021-03-22 02:08:04 +00:00
llog("Adding type: %s => %#v", tp.Name, md)
2021-01-30 09:53:34 +00:00
for _, v := range a.Specs[0].(*ast.TypeSpec).Type.(*ast.StructType).Fields.List {
2021-03-22 02:08:04 +00:00
2021-01-30 09:53:34 +00:00
tp.Fields[v.Names[0].Name] = &APIField{}
tp.Fields[v.Names[0].Name].Tags = make(map[string]APIFieldTag)
switch x := v.Type.(type) {
2021-03-22 02:08:04 +00:00
2021-01-30 09:53:34 +00:00
case *ast.Ident:
tp.Fields[v.Names[0].Name].Type = x.Name
case *ast.ArrayType:
switch z := x.Elt.(type) {
case *ast.Ident:
tp.Fields[v.Names[0].Name].Type = z.Name
tp.Fields[v.Names[0].Name].Array = true
case *ast.InterfaceType:
tp.Fields[v.Names[0].Name].Type = "interface{}"
tp.Fields[v.Names[0].Name].Array = true
case *ast.SelectorExpr:
2021-04-02 14:40:28 +00:00
api.UsedImportsTypes[z.X.(*ast.Ident).Name] = api.Imports[z.X.(*ast.Ident).Name]
2021-01-30 09:53:34 +00:00
tp.Fields[v.Names[0].Name].Type = z.X.(*ast.Ident).Name + "." + z.Sel.Name
tp.Fields[v.Names[0].Name].Array = true
}
case *ast.StarExpr:
switch y := x.X.(type) {
case *ast.Ident:
tp.Fields[v.Names[0].Name].Type = y.Name
case *ast.SelectorExpr:
switch z := y.X.(type) {
case *ast.Ident:
2021-04-02 14:40:28 +00:00
api.UsedImportsTypes[z.Name] = api.Imports[z.Name]
2021-01-30 09:53:34 +00:00
tp.Fields[v.Names[0].Name].Type = z.Name + "." + y.Sel.Name
}
}
2021-03-22 02:08:04 +00:00
2021-01-30 09:53:34 +00:00
case *ast.InterfaceType:
tp.Fields[v.Names[0].Name].Type = "interface{}"
case *ast.SelectorExpr:
switch z := x.X.(type) {
case *ast.Ident:
2021-04-02 14:40:28 +00:00
api.UsedImportsTypes[z.Name] = api.Imports[z.Name]
2021-01-30 09:53:34 +00:00
tp.Fields[v.Names[0].Name].Type = z.Name + "." + x.Sel.Name
}
case *ast.MapType:
switch z := x.Value.(type) {
case *ast.Ident:
tp.Fields[v.Names[0].Name].Type = ""
tp.Fields[v.Names[0].Name].Mapkey = x.Key.(*ast.Ident).Name
tp.Fields[v.Names[0].Name].Mapval = z.Name
tp.Fields[v.Names[0].Name].Map = true
case *ast.InterfaceType:
tp.Fields[v.Names[0].Name].Type = "interface{}"
tp.Fields[v.Names[0].Name].Array = true
}
default:
log.Printf("%#v", x)
}
if v.Tag != nil {
tgstr := strings.ReplaceAll(v.Tag.Value, "`", "")
tg, err := structtag.Parse(tgstr)
if err != nil {
panic(err)
}
for _, tgv := range tg.Keys() {
atg, err := tg.Get(tgv)
if err != nil {
panic(err)
}
tp.Fields[v.Names[0].Name].Tags[tgv] = APIFieldTag{
Key: tgv,
Name: atg.Name,
Opts: atg.Options,
}
}
log.Printf("#%v", tg)
}
}
api.Types[tp.Name] = &tp
}
func addFunction(a *ast.FuncDecl) {
md := manageComments(a.Doc)
if md["API"] == "" {
return
}
2021-03-22 02:08:04 +00:00
llog("Adding Fuction: %s => %#v", a.Name, md)
2021-01-30 09:53:34 +00:00
reqType := &APIParamType{}
resType := &APIParamType{}
if len(a.Type.Params.List) > 1 {
switch x := a.Type.Params.List[1].Type.(type) {
case *ast.StarExpr:
reqType.Ispointer = true
switch y := x.X.(type) {
case *ast.Ident:
reqType.Typename = y.Name
case *ast.SelectorExpr:
2021-04-02 14:40:28 +00:00
api.UsedImportsFunctions[y.X.(*ast.Ident).Name] = api.Imports[y.X.(*ast.Ident).Name]
2021-01-30 09:53:34 +00:00
reqType.Typename = y.X.(*ast.Ident).Name + "." + y.Sel.Name
}
case *ast.ArrayType:
reqType.IsArray = true
switch y := x.Elt.(type) {
case *ast.Ident:
reqType.Typename = y.Name
case *ast.SelectorExpr:
2021-04-02 14:40:28 +00:00
api.UsedImportsFunctions[y.X.(*ast.Ident).Name] = api.Imports[y.X.(*ast.Ident).Name]
2021-01-30 09:53:34 +00:00
reqType.Typename = y.X.(*ast.Ident).Name + "." + y.Sel.Name
case *ast.StarExpr:
reqType.Ispointer = true
switch z := y.X.(type) {
case *ast.Ident:
reqType.Typename = z.Name
case *ast.SelectorExpr:
2021-04-02 14:40:28 +00:00
api.UsedImportsFunctions[z.X.(*ast.Ident).Name] = api.Imports[z.X.(*ast.Ident).Name]
2021-01-30 09:53:34 +00:00
reqType.Typename = z.X.(*ast.Ident).Name + "." + z.Sel.Name
}
}
case *ast.Ident:
reqType.Typename = x.Name
2021-03-22 02:08:04 +00:00
case *ast.SelectorExpr:
2021-04-02 14:40:28 +00:00
api.UsedImportsFunctions[x.X.(*ast.Ident).Name] = api.Imports[x.X.(*ast.Ident).Name]
2021-03-22 02:08:04 +00:00
reqType.Typename = x.X.(*ast.Ident).Name + "." + x.Sel.Name
2021-01-30 09:53:34 +00:00
}
}
if a.Type.Results != nil && len(a.Type.Results.List) > 0 {
switch x := a.Type.Results.List[0].Type.(type) {
case *ast.StarExpr:
resType.Ispointer = true
switch y := x.X.(type) {
case *ast.Ident:
resType.Typename = y.Name
case *ast.SelectorExpr:
2021-04-02 14:40:28 +00:00
api.UsedImportsFunctions[y.X.(*ast.Ident).Name] = api.Imports[y.X.(*ast.Ident).Name]
2021-01-30 09:53:34 +00:00
resType.Typename = y.X.(*ast.Ident).Name + "." + y.Sel.Name
}
case *ast.ArrayType:
resType.IsArray = true
switch y := x.Elt.(type) {
case *ast.Ident:
resType.Typename = y.Name
case *ast.SelectorExpr:
2021-04-02 14:40:28 +00:00
api.UsedImportsFunctions[y.X.(*ast.Ident).Name] = api.Imports[y.X.(*ast.Ident).Name]
2021-01-30 09:53:34 +00:00
resType.Typename = y.X.(*ast.Ident).Name + "." + y.Sel.Name
case *ast.StarExpr:
resType.Ispointer = true
switch z := y.X.(type) {
case *ast.Ident:
resType.Typename = z.Name
case *ast.SelectorExpr:
2021-04-02 14:40:28 +00:00
api.UsedImportsFunctions[z.X.(*ast.Ident).Name] = api.Imports[z.X.(*ast.Ident).Name]
2021-01-30 09:53:34 +00:00
resType.Typename = z.X.(*ast.Ident).Name + "." + z.Sel.Name
}
}
2021-03-22 02:08:04 +00:00
case *ast.Ident:
resType.Typename = x.Name
case *ast.SelectorExpr:
2021-04-02 14:40:28 +00:00
api.UsedImportsFunctions[x.X.(*ast.Ident).Name] = api.Imports[x.X.(*ast.Ident).Name]
2021-03-22 02:08:04 +00:00
resType.Typename = x.X.(*ast.Ident).Name + "." + x.Sel.Name
2021-01-30 09:53:34 +00:00
}
if md["RAW"] == "true" {
reqType.Typename = md["REQ"]
resType.Typename = md["RES"]
}
verb := md["VERB"]
if verb == "" {
verb = http.MethodPost
}
fn := APIMethod{
2021-04-02 14:40:28 +00:00
Name: a.Name.Name,
2021-01-30 09:53:34 +00:00
Desc: a.Name.Name,
Verb: verb,
Path: md["PATH"],
Perm: md["PERM"],
ReqType: reqType,
ResType: resType,
}
2021-02-20 07:58:09 +00:00
if fn.Path == "" {
2021-05-01 22:09:48 +00:00
fn.Path = "/" + strings.Replace(strings.ToLower(a.Name.Name), "_", "/", -1)
2021-02-20 07:58:09 +00:00
}
2021-01-30 09:53:34 +00:00
api.Methods[a.Name.Name] = &fn
}
}
func load(src string) error {
api.Types = (make(map[string]*APIType))
api.Methods = (make(map[string]*APIMethod))
2021-03-22 02:08:04 +00:00
api.Imports = make(map[string]string)
2021-04-02 14:40:28 +00:00
api.UsedImportsTypes = make(map[string]string)
api.UsedImportsFunctions = make(map[string]string)
api.Paths = make(map[string]*APIPath)
api.SortedPaths = make([]*APIPath, 0)
2021-01-30 09:53:34 +00:00
fset := token.NewFileSet() // positions are relative to fset
f, err := parser.ParseDir(fset, src, nil, parser.ParseComments)
if err != nil {
return err
}
for _, v := range f {
2021-03-22 02:08:04 +00:00
llog("Loading Package: %s", v.Name)
2021-01-30 09:53:34 +00:00
// Print the AST.
ast.Inspect(v, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.GenDecl:
if x.Tok == token.TYPE {
addStruct(x)
} else {
return true
}
2021-03-22 02:08:04 +00:00
case *ast.ImportSpec:
var impkey = ""
var impval = ""
impval = strings.Replace(x.Path.Value, "\"", "", -1)
impval = strings.Replace(impval, "'", "", -1)
if x.Name != nil && x.Name.Name != "" {
impkey = x.Name.Name
} else {
parts := strings.Split(impval, "/")
impkey = parts[len(parts)-1]
}
api.Imports[impkey] = impval
2021-01-30 09:53:34 +00:00
case *ast.File:
2021-03-22 02:08:04 +00:00
manageCommentsGroups(x.Comments)
2021-01-30 09:53:34 +00:00
case *ast.FuncDecl:
addFunction(x)
2021-03-22 02:08:04 +00:00
llog("Adding fn: %s", x.Name)
2021-01-30 09:53:34 +00:00
case *ast.ValueSpec:
if x.Names[0].Name == "BASEPATH" {
api.BasePath = strings.Replace(x.Values[0].(*ast.BasicLit).Value, "\"", "", -1)
}
if x.Names[0].Name == "NAMESPACE" {
api.Namespace = strings.Replace(x.Values[0].(*ast.BasicLit).Value, "\"", "", -1)
}
log.Printf("%#v", x)
case *ast.Package:
packageName = x.Name
default:
//log.Printf("%#v", x)
return true
}
return true
})
}
for k, v := range api.Methods {
2021-04-02 14:40:28 +00:00
path, ok := api.Paths[v.Path]
if !ok {
path = &APIPath{
Path: v.Path,
MapVerbs: make(map[string]*APIVerb),
SortedVerbs: make([]*APIVerb, 0),
}
api.Paths[v.Path] = path
}
path.MapVerbs[v.Verb] = &APIVerb{
Verb: v.Verb,
Method: v,
}
2021-01-30 09:53:34 +00:00
pathmap, ok := httpMapper[v.Path]
if !ok {
httpMapper[v.Path] = make(map[string]string)
pathmap = httpMapper[v.Path]
}
pathmap[v.Verb] = k
}
2021-04-02 14:40:28 +00:00
pathNames := make([]string, 0)
for k, v := range api.Paths {
verbs := make([]string, 0)
for k, _ := range v.MapVerbs {
verbs = append(verbs, k)
}
sort.Strings(verbs)
for _, sv := range verbs {
v.SortedVerbs = append(v.SortedVerbs, v.MapVerbs[sv])
}
pathNames = append(pathNames, k)
}
sort.Strings(pathNames)
for _, p := range pathNames {
api.SortedPaths = append(api.SortedPaths, api.Paths[p])
}
api.Namespace = packageName
2021-01-30 09:53:34 +00:00
return nil
}