functions.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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, actionId string
  46. for _, action := range actions {
  47. actionId = strings.Title(action.ID)
  48. actionStmt = fmt.Sprintf("actions.%s", actionId)
  49. if action.Context != nil {
  50. actionStmt = fmt.Sprintf(
  51. "%s(%s)",
  52. actionStmt,
  53. fmt.Sprintf("%#v", action.Context),
  54. )
  55. }
  56. actionsCallStmt = append(
  57. actionsCallStmt,
  58. fmt.Sprintf(`{"%s", %s}`, actionId,actionStmt),
  59. )
  60. }
  61. if len(actionsCallStmt) > 0{
  62. return fmt.Sprintf(`[]Action {
  63. %s,
  64. }`, strings.Join(actionsCallStmt, ",\n"))
  65. }
  66. return ""
  67. }
  68. func getCustom(options map[string]interface{}, path string, fallback ...interface{}) (resp interface{}) {
  69. found := false
  70. if options != nil {
  71. if resp, found = options[path]; found {
  72. return
  73. }
  74. }
  75. for _, value := range fallback {
  76. resp = value
  77. }
  78. return
  79. }