constants.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 (
  29. options = &api.Filter{
  30. DB:`).Id(dbid).Id(`,
  31. Collection:`).Id(collectionid).Id(`,
  32. }
  33. )
  34. if ctx != nil {
  35. options.Context = ctx
  36. options.SessionContext = api.GetSessionContext(ctx)
  37. }
  38. return options
  39. }
  40. `).Line())
  41. }
  42. }
  43. file.Const().Defs(constList...).Line()
  44. for id, v1 := range p.Variables {
  45. varList = append(varList, G.Id(id).Op("=").Lit(v1))
  46. }
  47. file.Var().Defs(varList...).Line()
  48. file.Add(filterFunctionList...)
  49. return Write(fmt.Sprintf("%s/%s/models/variables_gen.go", p.OutPath, p.Package), file)
  50. }
  51. // ctx, _ = `).Qual("context", "WithTimeout").Call(
  52. // G.Qual("context", "Background()"),
  53. // G.Lit(30).Op("*").Qual("time", "Second"),
  54. // ).Id(`