middlewares.go 839 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package got
  2. import (
  3. "fmt"
  4. "strings"
  5. . "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/common"
  6. G "github.com/dave/jennifer/jen"
  7. )
  8. func GenMiddlewares(p *Project) (err error) {
  9. for _, middle := range Middlewares {
  10. if middle.Type == "middleware" {
  11. if err = CreateMiddleware(p, middle); err != nil {
  12. return err
  13. }
  14. }
  15. }
  16. return nil
  17. }
  18. func CreateMiddleware(p *Project, middle *Middleware) error {
  19. var (
  20. err error
  21. file = G.NewFile(p.Package)
  22. ctx = &MiddlewareContext{
  23. Project: p,
  24. Middleware: middle,
  25. Statement: file.Func().Id(fmt.Sprintf("%sMiddleware", strings.Title(middle.Id))).Params(
  26. G.Id("ctx").Qual(IRIS_CTX, "Context"),
  27. ),
  28. }
  29. )
  30. if err = middle.Fn(ctx); err != nil {
  31. return err
  32. }
  33. return Write(fmt.Sprintf("%s/%s/middleware_%s.go", p.OutPath, p.Package, middle.Id), file)
  34. }