Expand generated code, add routes list
This commit is contained in:
		@@ -19,6 +19,13 @@ const (
 | 
			
		||||
 | 
			
		||||
const tmplParseFuncs = `package ` + packageName + `
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"io"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"reflect"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Generated map of Parse functions to support
 | 
			
		||||
// dynamic reflection upon parser functions
 | 
			
		||||
var ParseFunctionsMap = map[string]interface{}{
 | 
			
		||||
@@ -28,6 +35,23 @@ var ParseFunctionsMap = map[string]interface{}{
 | 
			
		||||
}
 | 
			
		||||
`
 | 
			
		||||
 | 
			
		||||
type FunctionParamInfo struct {
 | 
			
		||||
	FunctionName string
 | 
			
		||||
	Params       []string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const tmplFuncParams = `
 | 
			
		||||
var FunctionParams = map[string][]reflect.Type{
 | 
			
		||||
{{- range .}}
 | 
			
		||||
	"{{ .FunctionName }}": {
 | 
			
		||||
	  {{- range .Params }}
 | 
			
		||||
	  reflect.TypeOf((*{{ . }})(nil)).Elem(),
 | 
			
		||||
	  {{- end }}
 | 
			
		||||
	},
 | 
			
		||||
{{- end}}
 | 
			
		||||
}
 | 
			
		||||
`
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	// Parse the package
 | 
			
		||||
	fset := token.NewFileSet()
 | 
			
		||||
@@ -37,15 +61,44 @@ func main() {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Collect function names that start with "Parse"
 | 
			
		||||
	var functions []string
 | 
			
		||||
	parseFunctions := make([]string, 0)
 | 
			
		||||
	functionParams := make([]FunctionParamInfo, 0)
 | 
			
		||||
 | 
			
		||||
	for _, pkg := range node {
 | 
			
		||||
		for _, file := range pkg.Files {
 | 
			
		||||
			for _, decl := range file.Decls {
 | 
			
		||||
				if funcDecl, ok := decl.(*ast.FuncDecl); ok {
 | 
			
		||||
					// Load up Parse functions
 | 
			
		||||
					if strings.HasPrefix(funcDecl.Name.Name, "Parse") {
 | 
			
		||||
						// Add function name to the list
 | 
			
		||||
						functions = append(functions, funcDecl.Name.Name)
 | 
			
		||||
						parseFunctions = append(parseFunctions, funcDecl.Name.Name)
 | 
			
		||||
					}
 | 
			
		||||
 | 
			
		||||
					if strings.HasPrefix(funcDecl.Name.Name, "Status") {
 | 
			
		||||
						continue
 | 
			
		||||
					}
 | 
			
		||||
 | 
			
		||||
					// Load up params for all functions
 | 
			
		||||
					if funcDecl.Name.IsExported() {
 | 
			
		||||
						paramTypes := make([]string, 0)
 | 
			
		||||
						if funcDecl.Type.Params != nil {
 | 
			
		||||
							for _, param := range funcDecl.Type.Params.List {
 | 
			
		||||
								// Convert the type expression to a string representation
 | 
			
		||||
								typeExpr := param.Type
 | 
			
		||||
								typeStr := exprToString(typeExpr)
 | 
			
		||||
								if typeStr == "" {
 | 
			
		||||
									continue
 | 
			
		||||
								}
 | 
			
		||||
								// Append the type once for each name in the parameter
 | 
			
		||||
								for range param.Names {
 | 
			
		||||
									paramTypes = append(paramTypes, typeStr)
 | 
			
		||||
								}
 | 
			
		||||
							}
 | 
			
		||||
						}
 | 
			
		||||
						functionParams = append(functionParams, FunctionParamInfo{
 | 
			
		||||
							FunctionName: funcDecl.Name.String(),
 | 
			
		||||
							Params:       paramTypes,
 | 
			
		||||
						})
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
@@ -60,12 +113,38 @@ func main() {
 | 
			
		||||
	}
 | 
			
		||||
	defer mapFile.Close()
 | 
			
		||||
 | 
			
		||||
	// Execute the template and write to the file
 | 
			
		||||
	t := template.Must(template.New("mapTemplate").Parse(tmplParseFuncs))
 | 
			
		||||
	if err := t.Execute(mapFile, functions); err != nil {
 | 
			
		||||
	// Execute the template for parse funcs and write to the file
 | 
			
		||||
	parseFuncsTpl := template.Must(template.New("mapTemplate").Parse(tmplParseFuncs))
 | 
			
		||||
	if err := parseFuncsTpl.Execute(mapFile, parseFunctions); err != nil {
 | 
			
		||||
		fmt.Println("Error generating Go code:", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Execute the template for func params and write to the file
 | 
			
		||||
	funcParamsTpl := template.Must(template.New("paramsTemplate").Parse(tmplFuncParams))
 | 
			
		||||
	if err := funcParamsTpl.Execute(mapFile, functionParams); err != nil {
 | 
			
		||||
		fmt.Println("Error generating FunctionParams:", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fmt.Printf("%s has been generated successfully!\n", mapperFile)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func exprToString(expr ast.Expr) string {
 | 
			
		||||
	switch t := expr.(type) {
 | 
			
		||||
	case *ast.Ident:
 | 
			
		||||
		return t.Name // Simple type like "int" or "string"
 | 
			
		||||
	case *ast.ArrayType:
 | 
			
		||||
		return "[]" + exprToString(t.Elt) // Array type
 | 
			
		||||
	case *ast.StarExpr:
 | 
			
		||||
		return exprToString(t.X) // Pointer type
 | 
			
		||||
	case *ast.SelectorExpr:
 | 
			
		||||
		return fmt.Sprintf("%s.%s", exprToString(t.X), t.Sel.Name) // Qualified type like "pkg.Type"
 | 
			
		||||
	case *ast.FuncType:
 | 
			
		||||
		return "func" // Handle function types minimally
 | 
			
		||||
	case *ast.Ellipsis:
 | 
			
		||||
		return ""
 | 
			
		||||
	default:
 | 
			
		||||
		return fmt.Sprintf("%T", expr) // Fallback to the type's Go type
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user