middleware_metric.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package got
  2. import (
  3. "fmt"
  4. "strings"
  5. "text/template"
  6. . "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/common"
  7. "git.eugeniocarvalho.dev/eugeniucarvalho/utils"
  8. G "github.com/dave/jennifer/jen"
  9. )
  10. func CreateMetricFiles(project *Project) (err error) {
  11. if !project.HasMetrics {
  12. return
  13. }
  14. if err = createMetricIndex(project); err != nil {
  15. return
  16. }
  17. if err = createMetricHandler(project); err != nil {
  18. return
  19. }
  20. return nil
  21. }
  22. func createMetricIndex(project *Project) (err error) {
  23. var (
  24. Index = G.NewFile("metrics")
  25. metricsTmpl *template.Template
  26. out string
  27. )
  28. metricsTmpl, err = ParseTemplate(`
  29. import (
  30. "github.com/eugeniucarvalho/metric-query-parser/parser"
  31. )
  32. func InitMetrics() {
  33. {{range .metrics}}
  34. // {{.description}}
  35. parser.RegisterHandler("{{.id}}", {{.functionId}} )
  36. {{end}}
  37. }
  38. `)
  39. metrics := []map[string]interface{}{}
  40. for id, metric := range project.Metrics {
  41. metrics = append(metrics, map[string]interface{}{
  42. "id": id,
  43. "functionId": strings.Title(id),
  44. "description": metric.Description,
  45. })
  46. }
  47. context:= map[string]interface{}{
  48. "metrics": metrics,
  49. }
  50. if out, err = TemplateToString(metricsTmpl, context); err != nil {
  51. return
  52. }
  53. Index.Id(out)
  54. err = Write(project.Paths.Include("/go/metrics/index_gen.go"), Index)
  55. return
  56. }
  57. func createMetricHandler(project *Project) (err error) {
  58. var (
  59. metricsTmpl *template.Template
  60. out string
  61. )
  62. metricsTmpl, err = ParseTemplate(`
  63. {{if .metric.Return.Props}}
  64. type {{.functionId}}Result struct {
  65. {{range $id, $prop := .metric.Return.Props}}
  66. {{$prop.ID}} {{$prop.Type}}
  67. {{end}}
  68. }
  69. {{end}}
  70. // {{.metric.Description}}
  71. func {{.functionId}}(props map[string]interface{}) (result interface{}, err error) {
  72. // TO DO....
  73. return {{.return}}, nil
  74. }
  75. `)
  76. for id, metric := range project.Metrics {
  77. path := project.Paths.Include("/go/metrics/%s_gen.go", id)
  78. if utils.FileExists(path) {
  79. continue
  80. }
  81. functionId := strings.Title(id)
  82. returnValue := `""`
  83. if metric.Return.Props != nil {
  84. returnValue = fmt.Sprintf("%sResult{}", functionId)
  85. for propId, prop := range metric.Return.Props {
  86. prop.ID = strings.Title(propId)
  87. }
  88. }
  89. if metric.Return.Range {
  90. returnValue = fmt.Sprintf("[]%s", returnValue)
  91. }
  92. context:= map[string]interface{}{
  93. "id": id,
  94. "functionId": functionId,
  95. "metric": metric,
  96. "return": returnValue,
  97. }
  98. if out, err = TemplateToString(metricsTmpl, context); err != nil {
  99. return
  100. }
  101. Index := G.NewFile("metrics")
  102. Index.Id(out)
  103. err = Write(path, Index)
  104. }
  105. return
  106. }