Add Dependencies info to README.md.

This commit is contained in:
go-jet 2019-07-04 17:54:15 +02:00
parent f34c5e7fe8
commit 55e8c3bbb1
10 changed files with 79 additions and 43 deletions

View file

@ -473,7 +473,13 @@ because integer columns and expressions can be only compered to other integer co
Without Jet these bugs will have to be either caught by some test or by manual testing.
## Dependencies
TODO:
At the moment Jet dependence only of:
- `github.com/google/uuid` _(Used for debug purposes)_
- `github.com/lib/pq` _(Used by JetGen to read information about database schema)_
To run the tests, additional dependencies are required:
- `github.com/pkg/profile`
- `gotest.tools/assert`
## Contributing

View file

@ -2,8 +2,8 @@ package jet
import (
"bytes"
"github.com/go-jet/jet/internal/utils"
"github.com/google/uuid"
"github.com/lib/pq"
"strconv"
"strings"
"time"
@ -250,7 +250,7 @@ func ArgToString(value interface{}) string {
case uuid.UUID:
return stringQuote(bindVal.String())
case time.Time:
return stringQuote(string(pq.FormatTimestamp(bindVal)))
return stringQuote(string(utils.FormatTimestamp(bindVal)))
default:
return "[Unknown type]"
}

View file

@ -7,7 +7,7 @@ import (
"errors"
"fmt"
"github.com/go-jet/jet/execution/internal"
"github.com/go-jet/jet/internal/util"
"github.com/go-jet/jet/internal/utils"
"reflect"
"strconv"
"strings"
@ -550,10 +550,10 @@ func newScanContext(rows *sql.Rows) (*scanContext, error) {
for i, alias := range aliases {
names := strings.SplitN(alias, ".", 2)
goName := util.ToGoIdentifier(names[0])
goName := utils.ToGoIdentifier(names[0])
if len(names) > 1 {
goName += "." + util.ToGoIdentifier(names[1])
goName += "." + utils.ToGoIdentifier(names[1])
}
goNamesMap[strings.ToLower(goName)] = i

View file

@ -3,7 +3,7 @@ package postgres_metadata
import (
"database/sql"
"fmt"
"github.com/go-jet/jet/internal/util"
"github.com/go-jet/jet/internal/utils"
"strings"
)
@ -44,7 +44,7 @@ func (c ColumnInfo) SqlBuilderColumnType() string {
func (c ColumnInfo) GoBaseType() string {
switch c.DataType {
case "USER-DEFINED":
return util.ToGoIdentifier(c.EnumName)
return utils.ToGoIdentifier(c.EnumName)
case "boolean":
return "bool"
case "smallint":

View file

@ -2,7 +2,7 @@ package postgres_metadata
import (
"database/sql"
"github.com/go-jet/jet/internal/util"
"github.com/go-jet/jet/internal/utils"
)
type TableInfo struct {
@ -58,7 +58,7 @@ func (t TableInfo) GetImports() []string {
}
func (t TableInfo) GoStructName() string {
return util.ToGoIdentifier(t.name) + "Table"
return utils.ToGoIdentifier(t.name) + "Table"
}
func GetTableInfo(db *sql.DB, dbName, schemaName, tableName string) (tableInfo TableInfo, err error) {

View file

@ -5,8 +5,7 @@ import (
"fmt"
"github.com/go-jet/jet/generator/internal/metadata"
"github.com/go-jet/jet/generator/internal/metadata/postgres-metadata"
"github.com/go-jet/jet/generator/internal/utils"
"github.com/go-jet/jet/internal/util"
"github.com/go-jet/jet/internal/utils"
_ "github.com/lib/pq"
"path"
"path/filepath"
@ -116,7 +115,7 @@ func generate(schemaInfo postgres_metadata.SchemaInfo, dirPath, packageName stri
return err
}
err = utils.SaveGoFile(modelDirPath, util.ToGoFileName(metaData.Name()), append(autoGenWarning, text...))
err = utils.SaveGoFile(modelDirPath, utils.ToGoFileName(metaData.Name()), append(autoGenWarning, text...))
if err != nil {
return err

View file

@ -1,25 +0,0 @@
package util
import (
"github.com/go-jet/jet/internal/3rdparty/snaker"
"strings"
)
func ToGoIdentifier(databaseIdentifier string) string {
if len(databaseIdentifier) == 0 {
return databaseIdentifier
}
return snaker.SnakeToCamel(replaceInvalidChars(databaseIdentifier))
}
func ToGoFileName(databaseIdentifier string) string {
return strings.ToLower(replaceInvalidChars(databaseIdentifier))
}
func replaceInvalidChars(str string) string {
str = strings.Replace(str, " ", "_", -1)
str = strings.Replace(str, "-", "_", -1)
return str
}

View file

@ -2,14 +2,28 @@ package utils
import (
"bytes"
"github.com/go-jet/jet/internal/util"
"github.com/go-jet/jet/internal/3rdparty/snaker"
"go/format"
"os"
"path/filepath"
"strconv"
"strings"
"text/template"
"time"
)
func ToGoIdentifier(databaseIdentifier string) string {
if len(databaseIdentifier) == 0 {
return databaseIdentifier
}
return snaker.SnakeToCamel(replaceInvalidChars(databaseIdentifier))
}
func ToGoFileName(databaseIdentifier string) string {
return strings.ToLower(replaceInvalidChars(databaseIdentifier))
}
func SaveGoFile(dirPath, fileName string, text []byte) error {
newGoFilePath := filepath.Join(dirPath, fileName) + ".go"
@ -50,7 +64,7 @@ func EnsureDirPath(dirPath string) error {
func GenerateTemplate(templateText string, templateData interface{}) ([]byte, error) {
t, err := template.New("sqlBuilderTableTemplate").Funcs(template.FuncMap{
"ToGoIdentifier": util.ToGoIdentifier,
"ToGoIdentifier": ToGoIdentifier,
"now": func() string {
return time.Now().Format(time.RFC850)
},
@ -96,3 +110,45 @@ func DirExists(path string) (bool, error) {
}
return true, err
}
func replaceInvalidChars(str string) string {
str = strings.Replace(str, " ", "_", -1)
str = strings.Replace(str, "-", "_", -1)
return str
}
// github.com/lib/pq
// FormatTimestamp formats t into Postgres' text format for timestamps.
func FormatTimestamp(t time.Time) []byte {
// Need to send dates before 0001 A.D. with " BC" suffix, instead of the
// minus sign preferred by Go.
// Beware, "0000" in ISO is "1 BC", "-0001" is "2 BC" and so on
bc := false
if t.Year() <= 0 {
// flip year sign, and add 1, e.g: "0" will be "1", and "-10" will be "11"
t = t.AddDate((-t.Year())*2+1, 0, 0)
bc = true
}
b := []byte(t.Format("2006-01-02 15:04:05.999999999Z07:00"))
_, offset := t.Zone()
offset = offset % 60
if offset != 0 {
// RFC3339Nano already printed the minus sign
if offset < 0 {
offset = -offset
}
b = append(b, ':')
if offset < 10 {
b = append(b, '0')
}
b = strconv.AppendInt(b, int64(offset), 10)
}
if bc {
b = append(b, " BC"...)
}
return b
}

View file

@ -1,4 +1,4 @@
package util
package utils
import (
"github.com/stretchr/testify/assert"

View file

@ -2,7 +2,7 @@ package jet
import (
"errors"
"github.com/go-jet/jet/internal/util"
"github.com/go-jet/jet/internal/utils"
"reflect"
"strings"
)
@ -145,7 +145,7 @@ func unwindRowFromModel(columns []column, data interface{}) []clause {
for _, column := range columns {
columnName := column.Name()
structFieldName := util.ToGoIdentifier(columnName)
structFieldName := utils.ToGoIdentifier(columnName)
structField := structValue.FieldByName(structFieldName)