2016-06-12 20:19:23 +02:00
|
|
|
package main
|
2016-06-12 03:25:00 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
2016-08-09 09:59:30 +02:00
|
|
|
"github.com/vattle/sqlboiler/bdb"
|
2016-06-12 03:25:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// imports defines the optional standard imports and
|
|
|
|
// thirdParty imports (from github for example)
|
|
|
|
type imports struct {
|
|
|
|
standard importList
|
|
|
|
thirdParty importList
|
|
|
|
}
|
|
|
|
|
|
|
|
// importList is a list of import names
|
|
|
|
type importList []string
|
|
|
|
|
|
|
|
func (i importList) Len() int {
|
|
|
|
return len(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i importList) Swap(k, j int) {
|
|
|
|
i[k], i[j] = i[j], i[k]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i importList) Less(k, j int) bool {
|
|
|
|
res := strings.Compare(strings.TrimLeft(i[k], "_ "), strings.TrimLeft(i[j], "_ "))
|
|
|
|
if res <= 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func combineImports(a, b imports) imports {
|
|
|
|
var c imports
|
|
|
|
|
|
|
|
c.standard = removeDuplicates(combineStringSlices(a.standard, b.standard))
|
|
|
|
c.thirdParty = removeDuplicates(combineStringSlices(a.thirdParty, b.thirdParty))
|
|
|
|
|
|
|
|
sort.Sort(c.standard)
|
|
|
|
sort.Sort(c.thirdParty)
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2016-06-23 08:09:56 +02:00
|
|
|
func combineTypeImports(a imports, b map[string]imports, columns []bdb.Column) imports {
|
2016-06-12 03:25:00 +02:00
|
|
|
tmpImp := imports{
|
|
|
|
standard: make(importList, len(a.standard)),
|
|
|
|
thirdParty: make(importList, len(a.thirdParty)),
|
|
|
|
}
|
|
|
|
|
|
|
|
copy(tmpImp.standard, a.standard)
|
|
|
|
copy(tmpImp.thirdParty, a.thirdParty)
|
|
|
|
|
|
|
|
for _, col := range columns {
|
|
|
|
for key, imp := range b {
|
|
|
|
if col.Type == key {
|
|
|
|
tmpImp.standard = append(tmpImp.standard, imp.standard...)
|
|
|
|
tmpImp.thirdParty = append(tmpImp.thirdParty, imp.thirdParty...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpImp.standard = removeDuplicates(tmpImp.standard)
|
|
|
|
tmpImp.thirdParty = removeDuplicates(tmpImp.thirdParty)
|
|
|
|
|
|
|
|
sort.Sort(tmpImp.standard)
|
|
|
|
sort.Sort(tmpImp.thirdParty)
|
|
|
|
|
|
|
|
return tmpImp
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildImportString(imps imports) []byte {
|
|
|
|
stdlen, thirdlen := len(imps.standard), len(imps.thirdParty)
|
|
|
|
if stdlen+thirdlen < 1 {
|
|
|
|
return []byte{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if stdlen+thirdlen == 1 {
|
|
|
|
var imp string
|
|
|
|
if stdlen == 1 {
|
|
|
|
imp = imps.standard[0]
|
|
|
|
} else {
|
|
|
|
imp = imps.thirdParty[0]
|
|
|
|
}
|
|
|
|
return []byte(fmt.Sprintf("import %s", imp))
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
buf.WriteString("import (")
|
|
|
|
for _, std := range imps.standard {
|
|
|
|
fmt.Fprintf(buf, "\n\t%s", std)
|
|
|
|
}
|
|
|
|
if stdlen != 0 && thirdlen != 0 {
|
|
|
|
buf.WriteString("\n")
|
|
|
|
}
|
|
|
|
for _, third := range imps.thirdParty {
|
|
|
|
fmt.Fprintf(buf, "\n\t%s", third)
|
|
|
|
}
|
|
|
|
buf.WriteString("\n)\n")
|
|
|
|
|
|
|
|
return buf.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
func combineStringSlices(a, b []string) []string {
|
|
|
|
c := make([]string, len(a)+len(b))
|
|
|
|
if len(a) > 0 {
|
|
|
|
copy(c, a)
|
|
|
|
}
|
|
|
|
if len(b) > 0 {
|
|
|
|
copy(c[len(a):], b)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeDuplicates(dedup []string) []string {
|
|
|
|
if len(dedup) <= 1 {
|
|
|
|
return dedup
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(dedup)-1; i++ {
|
|
|
|
for j := i + 1; j < len(dedup); j++ {
|
|
|
|
if dedup[i] != dedup[j] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if j != len(dedup)-1 {
|
|
|
|
dedup[j] = dedup[len(dedup)-1]
|
|
|
|
j--
|
|
|
|
}
|
|
|
|
dedup = dedup[:len(dedup)-1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dedup
|
|
|
|
}
|
|
|
|
|
|
|
|
var defaultTemplateImports = imports{
|
|
|
|
standard: importList{
|
2016-09-16 09:22:12 +02:00
|
|
|
`"bytes"`,
|
2016-08-13 20:42:28 +02:00
|
|
|
`"database/sql"`,
|
2016-09-16 09:22:12 +02:00
|
|
|
`"fmt"`,
|
2016-09-03 21:47:27 +02:00
|
|
|
`"reflect"`,
|
2016-09-16 09:22:12 +02:00
|
|
|
`"strings"`,
|
2016-09-03 21:47:27 +02:00
|
|
|
`"sync"`,
|
2016-08-28 16:12:37 +02:00
|
|
|
`"time"`,
|
2016-06-12 03:25:00 +02:00
|
|
|
},
|
|
|
|
thirdParty: importList{
|
2016-08-13 20:36:03 +02:00
|
|
|
`"github.com/pkg/errors"`,
|
2016-08-09 09:59:30 +02:00
|
|
|
`"github.com/vattle/sqlboiler/boil"`,
|
2016-09-15 05:59:55 +02:00
|
|
|
`"github.com/vattle/sqlboiler/queries"`,
|
2016-09-15 05:45:09 +02:00
|
|
|
`"github.com/vattle/sqlboiler/queries/qm"`,
|
2016-08-09 09:59:30 +02:00
|
|
|
`"github.com/vattle/sqlboiler/strmangle"`,
|
2016-06-12 03:25:00 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var defaultSingletonTemplateImports = map[string]imports{
|
2016-08-18 08:51:32 +02:00
|
|
|
"boil_queries": {
|
2016-06-12 03:25:00 +02:00
|
|
|
thirdParty: importList{
|
2016-08-09 09:59:30 +02:00
|
|
|
`"github.com/vattle/sqlboiler/boil"`,
|
2016-09-15 05:59:55 +02:00
|
|
|
`"github.com/vattle/sqlboiler/queries"`,
|
2016-09-15 05:45:09 +02:00
|
|
|
`"github.com/vattle/sqlboiler/queries/qm"`,
|
2016-06-12 03:25:00 +02:00
|
|
|
},
|
|
|
|
},
|
2016-08-14 01:10:17 +02:00
|
|
|
"boil_types": {
|
|
|
|
thirdParty: importList{
|
|
|
|
`"github.com/pkg/errors"`,
|
2016-09-03 21:47:27 +02:00
|
|
|
`"github.com/vattle/sqlboiler/strmangle"`,
|
2016-08-14 01:10:17 +02:00
|
|
|
},
|
|
|
|
},
|
2016-06-12 03:25:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var defaultTestTemplateImports = imports{
|
|
|
|
standard: importList{
|
2016-09-16 09:22:12 +02:00
|
|
|
`"bytes"`,
|
2016-06-12 03:25:00 +02:00
|
|
|
`"reflect"`,
|
2016-09-16 09:22:12 +02:00
|
|
|
`"testing"`,
|
2016-06-12 03:25:00 +02:00
|
|
|
},
|
|
|
|
thirdParty: importList{
|
2016-08-09 09:59:30 +02:00
|
|
|
`"github.com/vattle/sqlboiler/boil"`,
|
2016-09-15 05:45:09 +02:00
|
|
|
`"github.com/vattle/sqlboiler/randomize"`,
|
2016-08-09 09:59:30 +02:00
|
|
|
`"github.com/vattle/sqlboiler/strmangle"`,
|
2016-06-12 03:25:00 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var defaultSingletonTestTemplateImports = map[string]imports{
|
2016-09-11 21:07:39 +02:00
|
|
|
"boil_main_test": {
|
2016-06-27 06:53:23 +02:00
|
|
|
standard: importList{
|
|
|
|
`"database/sql"`,
|
2016-09-11 21:07:39 +02:00
|
|
|
`"flag"`,
|
|
|
|
`"fmt"`,
|
|
|
|
`"math/rand"`,
|
2016-06-27 06:53:23 +02:00
|
|
|
`"os"`,
|
|
|
|
`"path/filepath"`,
|
2016-09-11 21:07:39 +02:00
|
|
|
`"testing"`,
|
|
|
|
`"time"`,
|
2016-06-27 06:53:23 +02:00
|
|
|
},
|
|
|
|
thirdParty: importList{
|
2016-09-11 21:07:39 +02:00
|
|
|
`"github.com/kat-co/vala"`,
|
|
|
|
`"github.com/pkg/errors"`,
|
2016-06-27 06:53:23 +02:00
|
|
|
`"github.com/spf13/viper"`,
|
2016-09-11 21:07:39 +02:00
|
|
|
`"github.com/vattle/sqlboiler/boil"`,
|
2016-06-27 06:53:23 +02:00
|
|
|
},
|
|
|
|
},
|
2016-08-18 08:51:32 +02:00
|
|
|
"boil_queries_test": {
|
2016-06-12 03:25:00 +02:00
|
|
|
standard: importList{
|
2016-09-13 07:43:29 +02:00
|
|
|
`"bytes"`,
|
2016-06-12 03:25:00 +02:00
|
|
|
`"fmt"`,
|
2016-09-13 07:43:29 +02:00
|
|
|
`"io"`,
|
|
|
|
`"io/ioutil"`,
|
2016-06-12 03:25:00 +02:00
|
|
|
`"math/rand"`,
|
2016-09-13 07:43:29 +02:00
|
|
|
`"regexp"`,
|
2016-06-12 03:25:00 +02:00
|
|
|
},
|
2016-07-15 07:32:36 +02:00
|
|
|
thirdParty: importList{
|
2016-08-09 09:59:30 +02:00
|
|
|
`"github.com/vattle/sqlboiler/boil"`,
|
2016-07-15 07:32:36 +02:00
|
|
|
},
|
2016-06-12 03:25:00 +02:00
|
|
|
},
|
2016-08-18 08:51:32 +02:00
|
|
|
"boil_suites_test": {
|
2016-08-15 11:36:38 +02:00
|
|
|
standard: importList{
|
|
|
|
`"testing"`,
|
|
|
|
},
|
|
|
|
},
|
2016-06-12 03:25:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var defaultTestMainImports = map[string]imports{
|
2016-08-08 09:46:06 +02:00
|
|
|
"postgres": {
|
2016-06-12 03:25:00 +02:00
|
|
|
standard: importList{
|
|
|
|
`"bytes"`,
|
|
|
|
`"database/sql"`,
|
2016-09-14 06:46:58 +02:00
|
|
|
`"fmt"`,
|
|
|
|
`"io"`,
|
|
|
|
`"io/ioutil"`,
|
|
|
|
`"os"`,
|
|
|
|
`"os/exec"`,
|
|
|
|
`"strings"`,
|
2016-06-12 03:25:00 +02:00
|
|
|
},
|
|
|
|
thirdParty: importList{
|
2016-08-13 20:36:03 +02:00
|
|
|
`"github.com/pkg/errors"`,
|
|
|
|
`"github.com/spf13/viper"`,
|
2016-08-09 09:59:30 +02:00
|
|
|
`"github.com/vattle/sqlboiler/bdb/drivers"`,
|
2016-09-15 05:45:09 +02:00
|
|
|
`"github.com/vattle/sqlboiler/randomize"`,
|
2016-06-12 03:25:00 +02:00
|
|
|
`_ "github.com/lib/pq"`,
|
|
|
|
},
|
|
|
|
},
|
2016-09-11 18:17:08 +02:00
|
|
|
"mysql": {
|
|
|
|
standard: importList{
|
|
|
|
`"bytes"`,
|
|
|
|
`"database/sql"`,
|
2016-09-12 08:49:47 +02:00
|
|
|
`"fmt"`,
|
|
|
|
`"io"`,
|
|
|
|
`"io/ioutil"`,
|
|
|
|
`"os"`,
|
|
|
|
`"os/exec"`,
|
|
|
|
`"strings"`,
|
2016-09-11 18:17:08 +02:00
|
|
|
},
|
|
|
|
thirdParty: importList{
|
|
|
|
`"github.com/pkg/errors"`,
|
|
|
|
`"github.com/spf13/viper"`,
|
|
|
|
`"github.com/vattle/sqlboiler/bdb/drivers"`,
|
2016-09-15 05:45:09 +02:00
|
|
|
`"github.com/vattle/sqlboiler/randomize"`,
|
2016-09-12 08:49:47 +02:00
|
|
|
`_ "github.com/go-sql-driver/mysql"`,
|
2016-09-11 18:17:08 +02:00
|
|
|
},
|
|
|
|
},
|
2016-06-12 03:25:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// importsBasedOnType imports are only included in the template output if the
|
|
|
|
// database requires one of the following special types. Check
|
|
|
|
// TranslateColumnType to see the type assignments.
|
|
|
|
var importsBasedOnType = map[string]imports{
|
2016-08-08 09:46:06 +02:00
|
|
|
"null.Float32": {
|
2016-11-12 03:02:44 +01:00
|
|
|
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
|
2016-06-12 03:25:00 +02:00
|
|
|
},
|
2016-08-08 09:46:06 +02:00
|
|
|
"null.Float64": {
|
2016-11-12 03:02:44 +01:00
|
|
|
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
|
2016-06-12 03:25:00 +02:00
|
|
|
},
|
2016-08-08 09:46:06 +02:00
|
|
|
"null.Int": {
|
2016-11-12 03:02:44 +01:00
|
|
|
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
|
2016-06-12 03:25:00 +02:00
|
|
|
},
|
2016-08-08 09:46:06 +02:00
|
|
|
"null.Int8": {
|
2016-11-12 03:02:44 +01:00
|
|
|
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
|
2016-06-12 03:25:00 +02:00
|
|
|
},
|
2016-08-08 09:46:06 +02:00
|
|
|
"null.Int16": {
|
2016-11-12 03:02:44 +01:00
|
|
|
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
|
2016-06-12 03:25:00 +02:00
|
|
|
},
|
2016-08-08 09:46:06 +02:00
|
|
|
"null.Int32": {
|
2016-11-12 03:02:44 +01:00
|
|
|
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
|
2016-06-12 03:25:00 +02:00
|
|
|
},
|
2016-08-08 09:46:06 +02:00
|
|
|
"null.Int64": {
|
2016-11-12 03:02:44 +01:00
|
|
|
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
|
2016-06-12 03:25:00 +02:00
|
|
|
},
|
2016-08-08 09:46:06 +02:00
|
|
|
"null.Uint": {
|
2016-11-12 03:02:44 +01:00
|
|
|
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
|
2016-06-12 03:25:00 +02:00
|
|
|
},
|
2016-08-08 09:46:06 +02:00
|
|
|
"null.Uint8": {
|
2016-11-12 03:02:44 +01:00
|
|
|
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
|
2016-06-12 03:25:00 +02:00
|
|
|
},
|
2016-08-08 09:46:06 +02:00
|
|
|
"null.Uint16": {
|
2016-11-12 03:02:44 +01:00
|
|
|
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
|
2016-06-12 03:25:00 +02:00
|
|
|
},
|
2016-08-08 09:46:06 +02:00
|
|
|
"null.Uint32": {
|
2016-11-12 03:02:44 +01:00
|
|
|
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
|
2016-06-12 03:25:00 +02:00
|
|
|
},
|
2016-08-08 09:46:06 +02:00
|
|
|
"null.Uint64": {
|
2016-11-12 03:02:44 +01:00
|
|
|
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
|
2016-06-12 03:25:00 +02:00
|
|
|
},
|
2016-08-08 09:46:06 +02:00
|
|
|
"null.String": {
|
2016-11-12 03:02:44 +01:00
|
|
|
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
|
2016-06-12 03:25:00 +02:00
|
|
|
},
|
2016-08-08 09:46:06 +02:00
|
|
|
"null.Bool": {
|
2016-11-12 03:02:44 +01:00
|
|
|
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
|
2016-06-12 03:25:00 +02:00
|
|
|
},
|
2016-08-08 09:46:06 +02:00
|
|
|
"null.Time": {
|
2016-11-12 03:02:44 +01:00
|
|
|
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
|
2016-09-07 15:50:54 +02:00
|
|
|
},
|
|
|
|
"null.JSON": {
|
2016-11-12 03:02:44 +01:00
|
|
|
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
|
2016-09-07 15:50:54 +02:00
|
|
|
},
|
|
|
|
"null.Bytes": {
|
2016-11-12 03:02:44 +01:00
|
|
|
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
|
2016-06-12 03:25:00 +02:00
|
|
|
},
|
2016-08-08 09:46:06 +02:00
|
|
|
"time.Time": {
|
2016-06-12 03:25:00 +02:00
|
|
|
standard: importList{`"time"`},
|
|
|
|
},
|
2016-09-08 11:07:33 +02:00
|
|
|
"types.JSON": {
|
2016-09-15 05:45:09 +02:00
|
|
|
thirdParty: importList{`"github.com/vattle/sqlboiler/types"`},
|
2016-09-07 15:50:54 +02:00
|
|
|
},
|
2016-09-11 19:40:59 +02:00
|
|
|
"types.BytesArray": {
|
2016-09-15 05:45:09 +02:00
|
|
|
thirdParty: importList{`"github.com/vattle/sqlboiler/types"`},
|
2016-09-11 19:40:59 +02:00
|
|
|
},
|
|
|
|
"types.Int64Array": {
|
2016-09-15 05:45:09 +02:00
|
|
|
thirdParty: importList{`"github.com/vattle/sqlboiler/types"`},
|
2016-09-11 19:40:59 +02:00
|
|
|
},
|
|
|
|
"types.Float64Array": {
|
2016-09-15 05:45:09 +02:00
|
|
|
thirdParty: importList{`"github.com/vattle/sqlboiler/types"`},
|
2016-09-11 19:40:59 +02:00
|
|
|
},
|
|
|
|
"types.BoolArray": {
|
2016-09-15 05:45:09 +02:00
|
|
|
thirdParty: importList{`"github.com/vattle/sqlboiler/types"`},
|
2016-09-11 19:40:59 +02:00
|
|
|
},
|
|
|
|
"types.Hstore": {
|
2016-09-15 05:45:09 +02:00
|
|
|
thirdParty: importList{`"github.com/vattle/sqlboiler/types"`},
|
2016-09-11 19:40:59 +02:00
|
|
|
},
|
2016-06-12 03:25:00 +02:00
|
|
|
}
|