v_one.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package config
  2. import (
  3. "fmt"
  4. "io"
  5. "path/filepath"
  6. yaml "gopkg.in/yaml.v3"
  7. )
  8. type V1GenerateSettings struct {
  9. Version string `json:"version" yaml:"version"`
  10. Packages []v1PackageSettings `json:"packages" yaml:"packages"`
  11. Overrides []Override `json:"overrides,omitempty" yaml:"overrides,omitempty"`
  12. Rename map[string]string `json:"rename,omitempty" yaml:"rename,omitempty"`
  13. }
  14. type v1PackageSettings struct {
  15. Name string `json:"name" yaml:"name"`
  16. Engine Engine `json:"engine,omitempty" yaml:"engine"`
  17. Path string `json:"path" yaml:"path"`
  18. Schema Paths `json:"schema" yaml:"schema"`
  19. Queries Paths `json:"queries" yaml:"queries"`
  20. EmitInterface bool `json:"emit_interface" yaml:"emit_interface"`
  21. EmitJSONTags bool `json:"emit_json_tags" yaml:"emit_json_tags"`
  22. EmitPreparedQueries bool `json:"emit_prepared_queries" yaml:"emit_prepared_queries"`
  23. EmitExactTableNames bool `json:"emit_exact_table_names,omitempty" yaml:"emit_exact_table_names"`
  24. EmitEmptySlices bool `json:"emit_empty_slices,omitempty" yaml:"emit_empty_slices"`
  25. Overrides []Override `json:"overrides" yaml:"overrides"`
  26. }
  27. func v1ParseConfig(rd io.Reader) (Config, error) {
  28. dec := yaml.NewDecoder(rd)
  29. dec.KnownFields(true)
  30. var settings V1GenerateSettings
  31. var config Config
  32. if err := dec.Decode(&settings); err != nil {
  33. return config, err
  34. }
  35. if settings.Version == "" {
  36. return config, ErrMissingVersion
  37. }
  38. if settings.Version != "1" {
  39. return config, ErrUnknownVersion
  40. }
  41. if len(settings.Packages) == 0 {
  42. return config, ErrNoPackages
  43. }
  44. if err := settings.ValidateGlobalOverrides(); err != nil {
  45. return config, err
  46. }
  47. for i := range settings.Overrides {
  48. if err := settings.Overrides[i].Parse(); err != nil {
  49. return config, err
  50. }
  51. }
  52. for j := range settings.Packages {
  53. if settings.Packages[j].Path == "" {
  54. return config, ErrNoPackagePath
  55. }
  56. for i := range settings.Packages[j].Overrides {
  57. if err := settings.Packages[j].Overrides[i].Parse(); err != nil {
  58. return config, err
  59. }
  60. }
  61. if settings.Packages[j].Name == "" {
  62. settings.Packages[j].Name = filepath.Base(settings.Packages[j].Path)
  63. }
  64. if settings.Packages[j].Engine == "" {
  65. settings.Packages[j].Engine = EnginePostgreSQL
  66. }
  67. }
  68. return settings.Translate(), nil
  69. }
  70. func (c *V1GenerateSettings) ValidateGlobalOverrides() error {
  71. engines := map[Engine]struct{}{}
  72. for _, pkg := range c.Packages {
  73. if _, ok := engines[pkg.Engine]; !ok {
  74. engines[pkg.Engine] = struct{}{}
  75. }
  76. }
  77. usesMultipleEngines := len(engines) > 1
  78. for _, oride := range c.Overrides {
  79. if usesMultipleEngines && oride.Engine == "" {
  80. return fmt.Errorf(`the "engine" field is required for global type overrides because your configuration uses multiple database engines`)
  81. }
  82. }
  83. return nil
  84. }
  85. func (c *V1GenerateSettings) Translate() Config {
  86. conf := Config{
  87. Version: c.Version,
  88. }
  89. for _, pkg := range c.Packages {
  90. conf.SQL = append(conf.SQL, SQL{
  91. Engine: pkg.Engine,
  92. Schema: pkg.Schema,
  93. Queries: pkg.Queries,
  94. Gen: SQLGen{
  95. Go: &SQLGo{
  96. EmitInterface: pkg.EmitInterface,
  97. EmitJSONTags: pkg.EmitJSONTags,
  98. EmitPreparedQueries: pkg.EmitPreparedQueries,
  99. EmitExactTableNames: pkg.EmitExactTableNames,
  100. EmitEmptySlices: pkg.EmitEmptySlices,
  101. Package: pkg.Name,
  102. Out: pkg.Path,
  103. Overrides: pkg.Overrides,
  104. },
  105. },
  106. })
  107. }
  108. if len(c.Overrides) > 0 || len(c.Rename) > 0 {
  109. conf.Gen.Go = &GenGo{
  110. Overrides: c.Overrides,
  111. Rename: c.Rename,
  112. }
  113. }
  114. return conf
  115. }