export importer members and add basepath for Stat

This commit is contained in:
Patrick O'brien 2017-02-05 21:41:30 +10:00
parent e2c53aa312
commit 3747754236
4 changed files with 27 additions and 22 deletions

View file

@ -213,6 +213,11 @@ func (s *State) initTemplates() error {
// processReplacements loads any replacement templates
func (s *State) processReplacements() error {
basePath, err := getBasePath(s.Config.BaseDir)
if err != nil {
return err
}
for _, replace := range s.Config.Replacements {
splits := strings.Split(replace, ":")
if len(splits) != 2 {
@ -222,7 +227,7 @@ func (s *State) processReplacements() error {
var toReplaceFname string
toReplace, replaceWith := splits[0], splits[1]
inf, err := os.Stat(toReplace)
inf, err := os.Stat(filepath.Join(basePath, toReplace))
if err != nil {
return errors.Errorf("cannot stat %q", toReplace)
}

View file

@ -144,22 +144,22 @@ func removeDuplicates(dedup []string) []string {
type mapImports map[string]imports
type importer struct {
standard imports
testStandard imports
Standard imports
TestStandard imports
singleton mapImports
testSingleton mapImports
Singleton mapImports
TestSingleton mapImports
testMain mapImports
TestMain mapImports
basedOnType mapImports
BasedOnType mapImports
}
// newImporter returns an importer struct with default import values
func newImporter() importer {
var imp importer
imp.standard = imports{
imp.Standard = imports{
standard: importList{
`"bytes"`,
`"database/sql"`,
@ -178,7 +178,7 @@ func newImporter() importer {
},
}
imp.singleton = mapImports{
imp.Singleton = mapImports{
"boil_queries": {
thirdParty: importList{
`"github.com/vattle/sqlboiler/boil"`,
@ -194,7 +194,7 @@ func newImporter() importer {
},
}
imp.testStandard = imports{
imp.TestStandard = imports{
standard: importList{
`"bytes"`,
`"reflect"`,
@ -207,7 +207,7 @@ func newImporter() importer {
},
}
imp.testSingleton = mapImports{
imp.TestSingleton = mapImports{
"boil_main_test": {
standard: importList{
`"database/sql"`,
@ -246,7 +246,7 @@ func newImporter() importer {
},
}
imp.testMain = mapImports{
imp.TestMain = mapImports{
"postgres": {
standard: importList{
`"bytes"`,
@ -290,7 +290,7 @@ func newImporter() importer {
// basedOnType 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.
imp.basedOnType = mapImports{
imp.BasedOnType = mapImports{
"null.Float32": {
thirdParty: importList{`"gopkg.in/nullbio/null.v6"`},
},

View file

@ -267,7 +267,7 @@ func TestCombineTypeImports(t *testing.T) {
imps := newImporter()
res1 := combineTypeImports(imports1, imps.basedOnType, cols)
res1 := combineTypeImports(imports1, imps.BasedOnType, cols)
if !reflect.DeepEqual(res1, importsExpected) {
t.Errorf("Expected res1 to match importsExpected, got:\n\n%#v\n", res1)
@ -285,7 +285,7 @@ func TestCombineTypeImports(t *testing.T) {
},
}
res2 := combineTypeImports(imports2, imps.basedOnType, cols)
res2 := combineTypeImports(imports2, imps.BasedOnType, cols)
if !reflect.DeepEqual(res2, importsExpected) {
t.Errorf("Expected res2 to match importsExpected, got:\n\n%#v\n", res1)

View file

@ -38,7 +38,7 @@ func generateOutput(state *State, data *templateData) error {
state: state,
data: data,
templates: state.Templates,
importSet: state.Importer.standard,
importSet: state.Importer.Standard,
combineImportsOnType: true,
fileSuffix: ".go",
})
@ -50,7 +50,7 @@ func generateTestOutput(state *State, data *templateData) error {
state: state,
data: data,
templates: state.TestTemplates,
importSet: state.Importer.testStandard,
importSet: state.Importer.TestStandard,
combineImportsOnType: false,
fileSuffix: "_test.go",
})
@ -63,7 +63,7 @@ func generateSingletonOutput(state *State, data *templateData) error {
state: state,
data: data,
templates: state.SingletonTemplates,
importNamedSet: state.Importer.singleton,
importNamedSet: state.Importer.Singleton,
fileSuffix: ".go",
})
}
@ -75,7 +75,7 @@ func generateSingletonTestOutput(state *State, data *templateData) error {
state: state,
data: data,
templates: state.SingletonTestTemplates,
importNamedSet: state.Importer.testSingleton,
importNamedSet: state.Importer.TestSingleton,
fileSuffix: ".go",
})
}
@ -106,7 +106,7 @@ func executeTemplates(e executeTemplateData) error {
imps.standard = e.importSet.standard
imps.thirdParty = e.importSet.thirdParty
if e.combineImportsOnType {
imps = combineTypeImports(imps, e.state.Importer.basedOnType, e.data.Table.Columns)
imps = combineTypeImports(imps, e.state.Importer.BasedOnType, e.data.Table.Columns)
}
writeFileDisclaimer(out)
@ -170,8 +170,8 @@ func generateTestMainOutput(state *State, data *templateData) error {
out.Reset()
var imps imports
imps.standard = state.Importer.testMain[state.Config.DriverName].standard
imps.thirdParty = state.Importer.testMain[state.Config.DriverName].thirdParty
imps.standard = state.Importer.TestMain[state.Config.DriverName].standard
imps.thirdParty = state.Importer.TestMain[state.Config.DriverName].thirdParty
writeFileDisclaimer(out)
writePackageName(out, state.Config.PkgName)