Implement type-to-string-to-type mappings

This commit is contained in:
2024-11-26 20:18:10 -05:00
parent c9d3b534de
commit 447e6d842e
3 changed files with 2203 additions and 2181 deletions

View File

@ -19,13 +19,6 @@ 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{}{
@ -41,15 +34,30 @@ type FunctionParamInfo struct {
}
const tmplFuncParams = `
var FunctionParams = map[string][]reflect.Type{
// List of functions in eiaapi package
// with slice of arg types as strings
var FunctionParams = map[string][]string{
{{- range .}}
"{{ .FunctionName }}": {
{{- range .Params }}
reflect.TypeOf((*{{ . }})(nil)).Elem(),
{{ . | printf "\"%s\"" }},
{{- end }}
},
{{- end}}
}
func GetFuncParamType(funcName string, idx int) string {
funcParams, exists := FunctionParams[funcName]
if !exists {
return ""
}
if idx < len(funcParams) {
return funcParams[idx]
}
return ""
}
`
func main() {
@ -137,7 +145,7 @@ func exprToString(expr ast.Expr) string {
case *ast.ArrayType:
return "[]" + exprToString(t.Elt) // Array type
case *ast.StarExpr:
return exprToString(t.X) // Pointer type
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: