2023-01-05 20:51:45 +00:00
|
|
|
package generator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/dave/jennifer/jen"
|
2023-09-25 02:28:46 +00:00
|
|
|
"go.elara.ws/go-lemmy/cmd/gen/extractor"
|
2023-01-05 20:51:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type RoutesGenerator struct {
|
|
|
|
w io.Writer
|
|
|
|
PkgName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRoutes(w io.Writer, pkgName string) *RoutesGenerator {
|
|
|
|
return &RoutesGenerator{w, pkgName}
|
|
|
|
}
|
|
|
|
|
2023-09-25 02:28:46 +00:00
|
|
|
func (r *RoutesGenerator) Generate(routes []extractor.Route) error {
|
2023-01-05 20:51:45 +00:00
|
|
|
f := jen.NewFile(r.PkgName)
|
2023-04-21 02:27:38 +00:00
|
|
|
f.HeaderComment("Code generated by go.elara.ws/go-lemmy/cmd/gen (routes generator). DO NOT EDIT.")
|
2023-01-07 22:40:18 +00:00
|
|
|
|
2023-01-05 20:51:45 +00:00
|
|
|
for _, r := range routes {
|
|
|
|
|
2023-09-25 02:28:46 +00:00
|
|
|
f.Comment(r.Summary)
|
2023-01-05 20:51:45 +00:00
|
|
|
f.Func().Params(
|
|
|
|
jen.Id("c").Id("*Client"),
|
2023-09-25 02:28:46 +00:00
|
|
|
).Id(transformName(r.Name)).Params(
|
2023-01-05 20:51:45 +00:00
|
|
|
jen.Id("ctx").Qual("context", "Context"),
|
2023-09-25 02:28:46 +00:00
|
|
|
jen.Id("data").Qual("go.elara.ws/go-lemmy/types", r.ParamsName),
|
|
|
|
).ParamsFunc(func(g *jen.Group) {
|
|
|
|
if r.ReturnName != "" {
|
|
|
|
g.Op("*").Qual("go.elara.ws/go-lemmy/types", r.ReturnName)
|
|
|
|
}
|
|
|
|
g.Error()
|
|
|
|
}).BlockFunc(func(g *jen.Group) {
|
|
|
|
returnName := r.ReturnName
|
|
|
|
if returnName == "" {
|
|
|
|
returnName = "EmptyResponse"
|
|
|
|
}
|
|
|
|
|
|
|
|
g.Id("resData").Op(":=").Op("&").Qual("go.elara.ws/go-lemmy/types", returnName).Block()
|
2023-01-05 20:51:45 +00:00
|
|
|
|
|
|
|
var funcName string
|
|
|
|
switch r.Method {
|
|
|
|
case "GET":
|
|
|
|
funcName = "getReq"
|
|
|
|
default:
|
|
|
|
funcName = "req"
|
|
|
|
}
|
|
|
|
|
|
|
|
g.List(jen.Id("res"), jen.Err()).Op(":=").Id("c").Dot(funcName).Params(
|
|
|
|
jen.Id("ctx"), jen.Lit(r.Method), jen.Lit(r.Path), jen.Id("data"), jen.Op("&").Id("resData"),
|
|
|
|
)
|
2023-09-25 02:28:46 +00:00
|
|
|
g.If(jen.Err().Op("!=").Nil()).BlockFunc(func(g *jen.Group) {
|
|
|
|
if returnName == "EmptyResponse" {
|
|
|
|
g.Return(jen.Err())
|
|
|
|
} else {
|
|
|
|
g.Return(jen.Nil(), jen.Err())
|
|
|
|
}
|
|
|
|
})
|
2023-01-05 20:51:45 +00:00
|
|
|
|
|
|
|
g.Err().Op("=").Id("resError").Params(jen.Id("res"), jen.Id("resData").Dot("LemmyResponse"))
|
2023-09-25 02:28:46 +00:00
|
|
|
g.If(jen.Err().Op("!=").Nil()).BlockFunc(func(g *jen.Group) {
|
|
|
|
if returnName == "EmptyResponse" {
|
|
|
|
g.Return(jen.Err())
|
|
|
|
} else {
|
|
|
|
g.Return(jen.Nil(), jen.Err())
|
|
|
|
}
|
|
|
|
})
|
2023-01-05 20:51:45 +00:00
|
|
|
|
2023-09-25 02:28:46 +00:00
|
|
|
if returnName == "EmptyResponse" {
|
|
|
|
g.Return(jen.Nil())
|
|
|
|
} else {
|
|
|
|
g.Return(jen.Id("resData"), jen.Nil())
|
|
|
|
}
|
2023-01-05 20:51:45 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return f.Render(r.w)
|
|
|
|
}
|
2023-01-31 03:38:57 +00:00
|
|
|
|
|
|
|
func transformName(s string) string {
|
2023-09-25 02:28:46 +00:00
|
|
|
s = strings.ToUpper(s[:1]) + s[1:]
|
2023-01-31 03:38:57 +00:00
|
|
|
s = strings.TrimPrefix(s, "Get")
|
|
|
|
s = strings.TrimPrefix(s, "List")
|
|
|
|
return s
|
|
|
|
}
|