Implement type-to-string-to-type mappings
This commit is contained in:
parent
c9d3b534de
commit
447e6d842e
File diff suppressed because it is too large
Load Diff
@ -19,13 +19,6 @@ const (
|
|||||||
|
|
||||||
const tmplParseFuncs = `package ` + packageName + `
|
const tmplParseFuncs = `package ` + packageName + `
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"reflect"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Generated map of Parse functions to support
|
// Generated map of Parse functions to support
|
||||||
// dynamic reflection upon parser functions
|
// dynamic reflection upon parser functions
|
||||||
var ParseFunctionsMap = map[string]interface{}{
|
var ParseFunctionsMap = map[string]interface{}{
|
||||||
@ -41,15 +34,30 @@ type FunctionParamInfo struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const tmplFuncParams = `
|
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 .}}
|
{{- range .}}
|
||||||
"{{ .FunctionName }}": {
|
"{{ .FunctionName }}": {
|
||||||
{{- range .Params }}
|
{{- range .Params }}
|
||||||
reflect.TypeOf((*{{ . }})(nil)).Elem(),
|
{{ . | printf "\"%s\"" }},
|
||||||
{{- end }}
|
{{- end }}
|
||||||
},
|
},
|
||||||
{{- 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() {
|
func main() {
|
||||||
@ -137,7 +145,7 @@ func exprToString(expr ast.Expr) string {
|
|||||||
case *ast.ArrayType:
|
case *ast.ArrayType:
|
||||||
return "[]" + exprToString(t.Elt) // Array type
|
return "[]" + exprToString(t.Elt) // Array type
|
||||||
case *ast.StarExpr:
|
case *ast.StarExpr:
|
||||||
return exprToString(t.X) // Pointer type
|
return "*" + exprToString(t.X) // Pointer type
|
||||||
case *ast.SelectorExpr:
|
case *ast.SelectorExpr:
|
||||||
return fmt.Sprintf("%s.%s", exprToString(t.X), t.Sel.Name) // Qualified type like "pkg.Type"
|
return fmt.Sprintf("%s.%s", exprToString(t.X), t.Sel.Name) // Qualified type like "pkg.Type"
|
||||||
case *ast.FuncType:
|
case *ast.FuncType:
|
||||||
|
@ -18,6 +18,7 @@ import (
|
|||||||
// parameters by type
|
// parameters by type
|
||||||
type MethodSubs struct {
|
type MethodSubs struct {
|
||||||
TypedParams map[reflect.Type]reflect.Value // Replace field of specific type with value, must be ptr to type
|
TypedParams map[reflect.Type]reflect.Value // Replace field of specific type with value, must be ptr to type
|
||||||
|
StrTypedParams map[string]reflect.Value // Parameter types by string name from eiaapi mappings
|
||||||
RequestEditorFns []eiaapi.RequestEditorFn // Optional request editor functions
|
RequestEditorFns []eiaapi.RequestEditorFn // Optional request editor functions
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,9 +26,13 @@ type MethodSubs struct {
|
|||||||
// To be more specific, set NameParams, which will overwrite
|
// To be more specific, set NameParams, which will overwrite
|
||||||
// NameContainsParams
|
// NameContainsParams
|
||||||
var defaultMethodSubs = MethodSubs{
|
var defaultMethodSubs = MethodSubs{
|
||||||
TypedParams: map[reflect.Type]reflect.Value{
|
StrTypedParams: map[string]reflect.Value{
|
||||||
reflect.TypeOf((*eiaapi.Route1)(nil)): reflect.ValueOf(
|
"Route1": reflect.ValueOf(eiaapi.Route1(
|
||||||
strconv.Itoa(time.Now().AddDate(-1, 0, 0).Year())),
|
strconv.Itoa(time.Now().AddDate(-1, 0, 0).Year()),
|
||||||
|
)),
|
||||||
|
"Route2": reflect.ValueOf(eiaapi.Route1(
|
||||||
|
strconv.Itoa(time.Now().AddDate(-1, 0, 0).Year()),
|
||||||
|
)),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,8 +47,7 @@ func (client *Client) GetFacet(ctx context.Context, route string, facet string,
|
|||||||
return nil, fmt.Errorf("method %s not found", methodName)
|
return nil, fmt.Errorf("method %s not found", methodName)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Populate facet param anywhere in func call
|
subs.StrTypedParams["FacetId"] = reflect.ValueOf(eiaapi.FacetId(facet))
|
||||||
subs.TypedParams[reflect.TypeOf((*eiaapi.FacetId)(nil))] = reflect.ValueOf(facet)
|
|
||||||
|
|
||||||
args := prepMethodArgs(method, methodName, subs)
|
args := prepMethodArgs(method, methodName, subs)
|
||||||
|
|
||||||
@ -174,7 +178,9 @@ func (client *Client) GetFacets(ctx context.Context, route string, subs *MethodS
|
|||||||
func DefaultMethodSubs(ctx context.Context) *MethodSubs {
|
func DefaultMethodSubs(ctx context.Context) *MethodSubs {
|
||||||
subs := defaultMethodSubs
|
subs := defaultMethodSubs
|
||||||
|
|
||||||
subs.TypedParams[reflect.TypeOf((*context.Context)(nil)).Elem()] = reflect.ValueOf(ctx)
|
subs.TypedParams = map[reflect.Type]reflect.Value{
|
||||||
|
reflect.TypeOf((*context.Context)(nil)).Elem(): reflect.ValueOf(ctx),
|
||||||
|
}
|
||||||
|
|
||||||
return &subs
|
return &subs
|
||||||
}
|
}
|
||||||
@ -226,18 +232,14 @@ func prepMethodArgs(method reflect.Value, name string, subs *MethodSubs) []refle
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve arg from generated arg mapping due to use of
|
// Perform type lookups by string match
|
||||||
// type aliases (e.g. type Route1 = string) rather than use of
|
if paramType := eiaapi.GetFuncParamType(name, i); paramType != "" {
|
||||||
// discrete types (e.g. type Route1 string) in generated code
|
for name, val := range subs.StrTypedParams {
|
||||||
funcArgs, ok := eiaapi.FunctionParams[name]
|
if paramType == name {
|
||||||
if ok {
|
args = append(args, val)
|
||||||
if i > len(funcArgs) {
|
goto next
|
||||||
// WARN: This indicates a generator issues
|
}
|
||||||
} else {
|
|
||||||
argType = funcArgs[i]
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// WARN: This indicates a generator issues
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run through type substitutions
|
// Run through type substitutions
|
||||||
|
Loading…
Reference in New Issue
Block a user