functions.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. method.Entity,
  17. method.ID,
  18. hookId,
  19. )
  20. if _, fileErr := os.Stat(path); os.IsNotExist(fileErr) {
  21. methodId := fmt.Sprintf(
  22. "%s%s%s",
  23. strings.Title(hookId),
  24. method.Entity,
  25. strings.Title(method.ID),
  26. )
  27. fmt.Println("\n\n\nCREATE HOOK", path, methodId)
  28. file := G.NewFile(project.Package)
  29. file.Func().Id(methodId).Call(
  30. G.Id("ctx").Qual(IRIS_CTX, "Context"),
  31. G.Id("resp").Interface(),
  32. ).Op("*").Qual(API_ERROR, "Error").Block(
  33. G.Return(G.Nil()),
  34. )
  35. if err := Write(path, file); err != nil {
  36. spew.Dump(err)
  37. }
  38. }
  39. }
  40. func parseMethodActions(actions []Action) string {
  41. actionsCallStmt := []string{}
  42. var actionStmt, actionId string
  43. for _, action := range actions {
  44. actionId = strings.Title(action.ID)
  45. actionStmt = fmt.Sprintf("actions.%s", actionId)
  46. if action.Context != nil {
  47. actionStmt = fmt.Sprintf(
  48. "%s(%s)",
  49. actionStmt,
  50. fmt.Sprintf("%#v", action.Context),
  51. )
  52. }
  53. actionsCallStmt = append(
  54. actionsCallStmt,
  55. fmt.Sprintf(`{"%s", %s}`, actionId,actionStmt),
  56. )
  57. }
  58. if len(actionsCallStmt) > 0{
  59. return fmt.Sprintf(`[]Action {
  60. %s,
  61. }`, strings.Join(actionsCallStmt, ",\n"))
  62. }
  63. return ""
  64. }
  65. func getCustom(options map[string]interface{}, path string, fallback ...interface{}) (resp interface{}) {
  66. found := false
  67. if options != nil {
  68. if resp, found = options[path]; found {
  69. return
  70. }
  71. }
  72. for _, value := range fallback {
  73. resp = value
  74. }
  75. return
  76. }