Skip to content
This repository has been archived by the owner on Apr 19, 2018. It is now read-only.
/ sqlgen Public archive

added optional import for other package structure #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 4 additions & 2 deletions gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"os"
"strings"

"github.com/drone/sqlgen/parse"
"github.com/drone/sqlgen/schema"
Expand All @@ -20,6 +21,7 @@ var (
genSchema = flag.Bool("schema", true, "generate sql schema and queries")
genFuncs = flag.Bool("funcs", true, "generate sql helper functions")
extraFuncs = flag.Bool("extras", true, "generate extra sql helper functions")
importPath = ""
)

func main() {
Expand All @@ -36,7 +38,7 @@ func main() {
// if the code is generated in a different folder
// that the struct we need to import the struct
if tree.Pkg != *pkgName && *pkgName != "main" {
// TODO
importPath = strings.Join([]string{*pkgName, tree.Pkg}, "/")
}

// load the Tree into a schema Object
Expand All @@ -47,7 +49,7 @@ func main() {

if *genFuncs {
writePackage(&buf, *pkgName)
writeImports(&buf, tree, "database/sql")
writeImports(&buf, tree, "database/sql", importPath)
writeRowFunc(&buf, tree)
writeRowsFunc(&buf, tree)
writeSliceFunc(&buf, tree)
Expand Down
4 changes: 3 additions & 1 deletion gen_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ func writeImports(w io.Writer, tree *parse.Node, pkgs ...string) {
// encoder package that was specified.
fmt.Fprintln(w, "\nimport (")
for pkg, _ := range pmap {
fmt.Fprintf(w, "\t%q\n", pkg)
if pkg != "" {
fmt.Fprintf(w, "\t%q\n", pkg)
}
}
fmt.Fprintln(w, ")")
}
Expand Down