api stub codegen

This commit is contained in:
Łukasz Magiera 2021-04-03 13:12:50 +02:00
parent 1b32d7f52f
commit 09c374cdde
4 changed files with 2148 additions and 5 deletions

File diff suppressed because it is too large Load Diff

View File

@ -6,6 +6,7 @@ import (
type Common = api.Common
type CommonStruct = api.CommonStruct
type CommonStub = api.CommonStub
type Gateway = api.Gateway

File diff suppressed because it is too large Load Diff

View File

@ -52,6 +52,7 @@ func (v *Visitor) Visit(node ast.Node) ast.Visitor {
return v
}
func main() {
// latest (v1)
if err := generate("./api", "api", "api", "./api/proxy_gen.go"); err != nil {
@ -143,10 +144,10 @@ func generate(path, pkg, outpkg, outfile string) error {
ast.Walk(v, ap)
type methodInfo struct {
Name string
node ast.Node
Tags map[string][]string
NamedParams, ParamNames, Results string
Name string
node ast.Node
Tags map[string][]string
NamedParams, ParamNames, Results, DefRes string
}
type strinfo struct {
@ -214,7 +215,7 @@ func generate(path, pkg, outpkg, outfile string) error {
}
}
var results []string
results := []string{}
for _, result := range node.ftype.Results.List {
rs, err := typeName(result.Type, outpkg)
if err != nil {
@ -223,6 +224,24 @@ func generate(path, pkg, outpkg, outfile string) error {
results = append(results, rs)
}
defRes := ""
if len(results) > 1 {
defRes = results[0]
switch {
case defRes[0] == '*' || defRes[0] == '<', defRes == "interface{}":
defRes = "nil"
case defRes == "bool":
defRes = "false"
case defRes == "string":
defRes = `""`
case defRes == "int", defRes == "int64", defRes == "uint64", defRes == "uint":
defRes = "0"
default:
defRes = "*new(" + defRes + ")"
}
defRes += ", "
}
info.Methods[mname] = &methodInfo{
Name: mname,
node: node.node,
@ -230,6 +249,7 @@ func generate(path, pkg, outpkg, outfile string) error {
NamedParams: strings.Join(params, ", "),
ParamNames: strings.Join(pnames, ", "),
Results: strings.Join(results, ", "),
DefRes: defRes,
}
}
@ -289,6 +309,12 @@ type {{.Name}}Struct struct {
{{end}}
}
}
type {{.Name}}Stub struct {
{{range .Include}}
{{.}}Stub
{{end}}
}
{{end}}
{{range .Infos}}
@ -297,6 +323,10 @@ type {{.Name}}Struct struct {
func (s *{{$name}}Struct) {{.Name}}({{.NamedParams}}) ({{.Results}}) {
return s.Internal.{{.Name}}({{.ParamNames}})
}
func (s *{{$name}}Stub) {{.Name}}({{.NamedParams}}) ({{.Results}}) {
return {{.DefRes}}xerrors.New("method not supported")
}
{{end}}
{{end}}