constants.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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(p.Paths.Build("/%s/models/variables_gen.go", p.Package), file)
  50. }