functions.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package got
  2. import (
  3. "fmt"
  4. "os"
  5. "strings"
  6. . "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/common"
  7. G "github.com/dave/jennifer/jen"
  8. "github.com/davecgh/go-spew/spew"
  9. )
  10. func generateHookCall(project *Project, method *Method, hookId string) {
  11. if !method.Hook(hookId) {
  12. return
  13. }
  14. path := fmt.Sprintf(
  15. "../project/include/go/hook_%s_%s_%s_gen.go",
  16. // project.OutPath,
  17. // project.Package,
  18. method.Entity,
  19. method.ID,
  20. hookId,
  21. )
  22. // return
  23. if _, fileErr := os.Stat(path); os.IsNotExist(fileErr) {
  24. methodId := fmt.Sprintf(
  25. "%s%s%s",
  26. strings.Title(hookId),
  27. method.Entity,
  28. strings.Title(method.ID),
  29. )
  30. fmt.Println("\n\n\nCREATE HOOK", path, methodId)
  31. file := G.NewFile(project.Package)
  32. file.Func().Id(methodId).Call(
  33. G.Id("ctx").Qual(IRIS_CTX, "Context"),
  34. G.Id("resp").Interface(),
  35. ).Op("*").Qual(API_ERROR, "Error").Block(
  36. G.Return(G.Nil()),
  37. )
  38. if err := Write(path, file); err != nil {
  39. spew.Dump(err)
  40. }
  41. }
  42. }
  43. func parseMethodActions(actions []Action) string {
  44. actionsCallStmt := []string{}
  45. var actionStmt string
  46. for _, action := range actions {
  47. actionStmt = fmt.Sprintf("actions.%s", strings.Title(action.ID))
  48. if action.Context != nil {
  49. actionStmt = fmt.Sprintf(
  50. "%s(%s)",
  51. actionStmt,
  52. fmt.Sprintf("%#v", action.Context),
  53. )
  54. }
  55. actionsCallStmt = append(actionsCallStmt, actionStmt)
  56. }
  57. return strings.Join(actionsCallStmt, ",\n")
  58. }