constants.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package got
  2. import (
  3. "fmt"
  4. . "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/common"
  5. G "github.com/dave/jennifer/jen"
  6. )
  7. func CreateVariables(p *Project) error {
  8. var (
  9. varList = G.Statement{}
  10. constList = G.Statement{}
  11. filterFunctionList = G.Statement{}
  12. dbid, collectionid string
  13. )
  14. // file := G.NewFile(p.Package)
  15. file := G.NewFile("models")
  16. for _, entity := range p.Schemas {
  17. if entity.Type == "object" {
  18. dbid = fmt.Sprintf("%sDataBase", entity.ID)
  19. collectionid = fmt.Sprintf("%sCollection", entity.ID)
  20. constList = append(constList, G.Id(dbid).Op("=").Lit(entity.DB))
  21. constList = append(constList, G.Id(collectionid).Op("=").Lit(entity.Collection))
  22. filterFunctionList = append(filterFunctionList, G.Id(`
  23. func `).Id(
  24. fmt.Sprintf("Filter%sOptions", entity.ID),
  25. ).Params(
  26. G.Id("ctx").Qual(IRIS_CTX, "Context"),
  27. ).Id(` *`).Qual(API_URL, "Filter").Id(`{
  28. var session `).Qual(MONGO, "SessionContext").Id(`
  29. if ctx != nil {
  30. session = api.GetSessionContext(ctx)
  31. }
  32. return &api.Filter{
  33. DB:`).Id(dbid).Id(`,
  34. Collection:`).Id(collectionid).Id(`,
  35. SessionContext: session,
  36. }
  37. }
  38. `).Line())
  39. }
  40. }
  41. file.Const().Defs(constList...).Line()
  42. for id, v1 := range p.Variables {
  43. varList = append(varList, G.Id(id).Op("=").Lit(v1))
  44. }
  45. file.Var().Defs(varList...).Line()
  46. file.Add(filterFunctionList...)
  47. return Write(fmt.Sprintf("%s/%s/models/variables_gen.go", p.OutPath, p.Package), file)
  48. }
  49. // ctx, _ = `).Qual("context", "WithTimeout").Call(
  50. // G.Qual("context", "Background()"),
  51. // G.Lit(30).Op("*").Qual("time", "Second"),
  52. // ).Id(`