v_one.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. EmitDBTags bool `json:"emit_db_tags" yaml:"emit_db_tags"`
  23. EmitPreparedQueries bool `json:"emit_prepared_queries" yaml:"emit_prepared_queries"`
  24. EmitExactTableNames bool `json:"emit_exact_table_names,omitempty" yaml:"emit_exact_table_names"`
  25. EmitEmptySlices bool `json:"emit_empty_slices,omitempty" yaml:"emit_empty_slices"`
  26. EmitExportedQueries bool `json:"emit_exported_queries,omitempty" yaml:"emit_exported_queries"`
  27. EmitResultStructPointers bool `json:"emit_result_struct_pointers" yaml:"emit_result_struct_pointers"`
  28. EmitParamsStructPointers bool `json:"emit_params_struct_pointers" yaml:"emit_params_struct_pointers"`
  29. EmitMethodsWithDBArgument bool `json:"emit_methods_with_db_argument" yaml:"emit_methods_with_db_argument"`
  30. JSONTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
  31. SQLPackage string `json:"sql_package" yaml:"sql_package"`
  32. Overrides []Override `json:"overrides" yaml:"overrides"`
  33. OutputDBFileName string `json:"output_db_file_name,omitempty" yaml:"output_db_file_name"`
  34. OutputModelsFileName string `json:"output_models_file_name,omitempty" yaml:"output_models_file_name"`
  35. OutputQuerierFileName string `json:"output_querier_file_name,omitempty" yaml:"output_querier_file_name"`
  36. OutputFilesSuffix string `json:"output_files_suffix,omitempty" yaml:"output_files_suffix"`
  37. StrictFunctionChecks bool `json:"strict_function_checks" yaml:"strict_function_checks"`
  38. }
  39. func v1ParseConfig(rd io.Reader) (Config, error) {
  40. dec := yaml.NewDecoder(rd)
  41. dec.KnownFields(true)
  42. var settings V1GenerateSettings
  43. var config Config
  44. if err := dec.Decode(&settings); err != nil {
  45. return config, err
  46. }
  47. if settings.Version == "" {
  48. return config, ErrMissingVersion
  49. }
  50. if settings.Version != "1" {
  51. return config, ErrUnknownVersion
  52. }
  53. if len(settings.Packages) == 0 {
  54. return config, ErrNoPackages
  55. }
  56. if err := settings.ValidateGlobalOverrides(); err != nil {
  57. return config, err
  58. }
  59. for i := range settings.Overrides {
  60. if err := settings.Overrides[i].Parse(); err != nil {
  61. return config, err
  62. }
  63. }
  64. for j := range settings.Packages {
  65. if settings.Packages[j].Path == "" {
  66. return config, ErrNoPackagePath
  67. }
  68. for i := range settings.Packages[j].Overrides {
  69. if err := settings.Packages[j].Overrides[i].Parse(); err != nil {
  70. return config, err
  71. }
  72. }
  73. if settings.Packages[j].Name == "" {
  74. settings.Packages[j].Name = filepath.Base(settings.Packages[j].Path)
  75. }
  76. if settings.Packages[j].Engine == "" {
  77. settings.Packages[j].Engine = EnginePostgreSQL
  78. }
  79. }
  80. return settings.Translate(), nil
  81. }
  82. func (c *V1GenerateSettings) ValidateGlobalOverrides() error {
  83. engines := map[Engine]struct{}{}
  84. for _, pkg := range c.Packages {
  85. if _, ok := engines[pkg.Engine]; !ok {
  86. engines[pkg.Engine] = struct{}{}
  87. }
  88. }
  89. usesMultipleEngines := len(engines) > 1
  90. for _, oride := range c.Overrides {
  91. if usesMultipleEngines && oride.Engine == "" {
  92. return fmt.Errorf(`the "engine" field is required for global type overrides because your configuration uses multiple database engines`)
  93. }
  94. }
  95. return nil
  96. }
  97. func (c *V1GenerateSettings) Translate() Config {
  98. conf := Config{
  99. Version: c.Version,
  100. }
  101. for _, pkg := range c.Packages {
  102. conf.SQL = append(conf.SQL, SQL{
  103. Engine: pkg.Engine,
  104. Schema: pkg.Schema,
  105. Queries: pkg.Queries,
  106. Gen: SQLGen{
  107. Go: &SQLGo{
  108. EmitInterface: pkg.EmitInterface,
  109. EmitJSONTags: pkg.EmitJSONTags,
  110. EmitDBTags: pkg.EmitDBTags,
  111. EmitPreparedQueries: pkg.EmitPreparedQueries,
  112. EmitExactTableNames: pkg.EmitExactTableNames,
  113. EmitEmptySlices: pkg.EmitEmptySlices,
  114. EmitExportedQueries: pkg.EmitExportedQueries,
  115. EmitResultStructPointers: pkg.EmitResultStructPointers,
  116. EmitParamsStructPointers: pkg.EmitParamsStructPointers,
  117. EmitMethodsWithDBArgument: pkg.EmitMethodsWithDBArgument,
  118. Package: pkg.Name,
  119. Out: pkg.Path,
  120. SQLPackage: pkg.SQLPackage,
  121. Overrides: pkg.Overrides,
  122. JSONTagsCaseStyle: pkg.JSONTagsCaseStyle,
  123. OutputDBFileName: pkg.OutputDBFileName,
  124. OutputModelsFileName: pkg.OutputModelsFileName,
  125. OutputQuerierFileName: pkg.OutputQuerierFileName,
  126. OutputFilesSuffix: pkg.OutputFilesSuffix,
  127. },
  128. },
  129. StrictFunctionChecks: pkg.StrictFunctionChecks,
  130. })
  131. }
  132. if len(c.Overrides) > 0 || len(c.Rename) > 0 {
  133. conf.Gen.Go = &GenGo{
  134. Overrides: c.Overrides,
  135. Rename: c.Rename,
  136. }
  137. }
  138. return conf
  139. }