functions.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. }
  59. func getCustom(options map[string]interface{}, path string) (resp interface{}) {
  60. if options != nil {
  61. resp = options[path]
  62. }
  63. return
  64. }