/
generate.go
43 lines (35 loc) · 1013 Bytes
/
generate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package genapi
import (
"go/types"
"text/template"
generator "github.com/olvrng/ggen"
"github.com/olvrng/rbot/be/tools/genapi/defs"
)
var currentPrinter generator.Printer
var tpl = template.Must(template.New("tpl").Funcs(funcs).Parse(tplText))
var funcs = map[string]interface{}{
"type": renderType,
"new": renderNew,
}
type Opts struct {
BasePath string
}
func generateServices(printer generator.Printer, opts Opts, services []*defs.Service) error {
currentPrinter = printer
printer.Import("context", "context")
printer.Import("fmt", "fmt")
printer.Import("http", "net/http")
printer.Import("httprpc", "github.com/olvrng/rbot/be/pkg/httprpc")
vars := map[string]interface{}{
"Services": services,
"Opts": opts,
}
return tpl.Execute(printer, vars)
}
func renderType(typ types.Type) string {
return currentPrinter.TypeString(typ)
}
func renderNew(typ types.Type) string {
named := typ.(*types.Pointer).Elem().(*types.Named)
return "&" + currentPrinter.TypeString(named) + "{}"
}