middleware_implement.go 941 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package got
  2. import (
  3. "text/template"
  4. . "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/common"
  5. G "github.com/dave/jennifer/jen"
  6. )
  7. var (
  8. implementStmtsTmpl *template.Template
  9. implementStmtsErr error
  10. )
  11. func init() {
  12. implementStmtsTmpl, implementStmtsErr = ParseTemplate(`
  13. {{if .preconditions}}
  14. if _, err = executeAction(
  15. ctx,
  16. {{.preconditions}},
  17. ); err != nil {
  18. return
  19. }
  20. {{end}}
  21. return
  22. `)
  23. if implementStmtsErr != nil {
  24. panic(implementStmtsErr)
  25. }
  26. }
  27. var (
  28. GenImplement = &Middleware{
  29. Id: "implement",
  30. Type: "method",
  31. Fn: func(ctx *MiddlewareContext) error {
  32. var (
  33. method = ctx.Method
  34. context = map[string]interface{}{
  35. "preconditions": parseMethodActions(method.Preconditions),
  36. }
  37. )
  38. // Nome do metodo que verifica se a entidade tem dependencias
  39. out, _ := TemplateToString(implementStmtsTmpl, context)
  40. ctx.Statement.Block(G.Id(out)).Line()
  41. return nil
  42. },
  43. }
  44. )