Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Avoid generating empty Go imports statement
Maybe fixes #3134
  • Loading branch information
mikeschinkel committed Jan 14, 2024
commit ac0d9a11847f15e2d99dd767ac6167ad0431cb48
1 change: 1 addition & 0 deletions internal/codegen/golang/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum,
"comment": sdk.DoubleSlashComment,
"escape": sdk.EscapeBacktick,
"imports": i.Imports,
"hasImports": i.HasImports,
"hasPrefix": strings.HasPrefix,

// These methods are Go specific, they do not belong in the codegen package
Expand Down
5 changes: 5 additions & 0 deletions internal/codegen/golang/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ func (i *importer) usesType(typ string) bool {
return false
}

func (i *importer) HasImports(filename string) bool {
imports := i.Imports(filename)
return len(imports[0]) != 0 || len(imports[1]) != 0
}

func (i *importer) Imports(filename string) [][]ImportSpec {
dbFileName := "db.go"
if i.Options.OutputDbFileName != "" {
Expand Down
13 changes: 13 additions & 0 deletions internal/codegen/golang/templates/template.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@

package {{.Package}}

{{ if hasImports .SourceName }}
import (
{{range imports .SourceName}}
{{range .}}{{.}}
{{end}}
{{end}}
)
{{end}}

{{template "dbCode" . }}
{{end}}
Expand All @@ -40,12 +42,14 @@ import (

package {{.Package}}

{{ if hasImports .SourceName }}
import (
{{range imports .SourceName}}
{{range .}}{{.}}
{{end}}
{{end}}
)
{{end}}

{{template "interfaceCode" . }}
{{end}}
Expand All @@ -69,12 +73,14 @@ import (

package {{.Package}}

{{ if hasImports .SourceName }}
import (
{{range imports .SourceName}}
{{range .}}{{.}}
{{end}}
{{end}}
)
{{end}}

{{template "modelsCode" . }}
{{end}}
Expand Down Expand Up @@ -167,12 +173,14 @@ type {{.Name}} struct { {{- range .Fields}}

package {{.Package}}

{{ if hasImports .SourceName }}
import (
{{range imports .SourceName}}
{{range .}}{{.}}
{{end}}
{{end}}
)
{{end}}

{{template "queryCode" . }}
{{end}}
Expand All @@ -196,12 +204,14 @@ import (

package {{.Package}}

{{ if hasImports .SourceName }}
import (
{{range imports .SourceName}}
{{range .}}{{.}}
{{end}}
{{end}}
)
{{end}}

{{template "copyfromCode" . }}
{{end}}
Expand All @@ -225,12 +235,15 @@ import (

package {{.Package}}

{{ if hasImports .SourceName }}
import (
{{range imports .SourceName}}
{{range .}}{{.}}
{{end}}
{{end}}
)
{{end}}

{{template "batchCode" . }}
{{end}}

Expand Down