Skip to content

Commit

Permalink
Add a replacement for buildImportString
Browse files Browse the repository at this point in the history
- Rename IndexPlaceholders to UseIndexPlaceholders
  • Loading branch information
aarondl committed Nov 12, 2017
1 parent 97d235c commit ba06e88
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 35 deletions.
6 changes: 3 additions & 3 deletions drivers/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ type Properties struct {
LQ rune
RQ rune

UseLastInsertID bool
IndexPlaceholders bool
UseTopClause bool
UseLastInsertID bool
UseIndexPlaceholders bool
UseTopClause bool
}

// Constructor breaks down the functionality required to implement a driver
Expand Down
68 changes: 36 additions & 32 deletions importers/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,39 @@ type Set struct {
ThirdParty List `toml:"third_party"`
}

// Format the set into Go syntax (compatible with go imports)
func (s Set) Format() []byte {
stdlen, thirdlen := len(s.Standard), len(s.ThirdParty)
if stdlen+thirdlen < 1 {
return []byte{}
}

if stdlen+thirdlen == 1 {
var imp string
if stdlen == 1 {
imp = s.Standard[0]
} else {
imp = s.ThirdParty[0]
}
return []byte(fmt.Sprintf("import %s", imp))
}

buf := &bytes.Buffer{}
buf.WriteString("import (")
for _, std := range s.Standard {
fmt.Fprintf(buf, "\n\t%s", std)
}
if stdlen != 0 && thirdlen != 0 {
buf.WriteString("\n")
}
for _, third := range s.ThirdParty {
fmt.Fprintf(buf, "\n\t%s", third)
}
buf.WriteString("\n)\n")

return buf.Bytes()
}

// SetFromInterface creates a set from a theoretical map[string]interface{}.
// This is to load from a loosely defined configuration file.
func SetFromInterface(intf interface{}) (Set, error) {
Expand Down Expand Up @@ -105,14 +138,17 @@ func MapFromInterface(intf interface{}) (Map, error) {
// List of imports
type List []string

// Len implements sort.Interface.Len
func (l List) Len() int {
return len(l)
}

// Swap implements sort.Interface.Swap
func (l List) Swap(i, j int) {
l[i], l[j] = l[j], l[i]
}

// Less implements sort.Interface.Less
func (l List) Less(i, j int) bool {
res := strings.Compare(strings.TrimLeft(l[i], "_ "), strings.TrimLeft(l[j], "_ "))
if res <= 0 {
Expand Down Expand Up @@ -394,38 +430,6 @@ func combineTypeImports(a Set, b map[string]Set, columnTypes []string) Set {
return tmpImp
}

func buildImportString(imps Set) []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 {
Expand Down
25 changes: 25 additions & 0 deletions importers/imports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package importers
import (
"reflect"
"sort"
"strings"
"testing"

"github.com/pkg/errors"
Expand Down Expand Up @@ -286,3 +287,27 @@ func TestCombineStringSlices(t *testing.T) {
t.Errorf("Slice mismatch: %#v + %#v != #%v", a, b, slice)
}
}

var testImportStringExpect = `import (
"fmt"
"github.com/pkg/errors"
)`

func TestSetFormat(t *testing.T) {
t.Parallel()

s := Set{
Standard: List{
`"fmt"`,
},
ThirdParty: List{
`"github.com/pkg/errors"`,
},
}

got := strings.TrimSpace(string(s.Format()))
if got != testImportStringExpect {
t.Error("want:\n", testImportStringExpect, "\ngot:\n", got)
}
}

0 comments on commit ba06e88

Please sign in to comment.