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. Without Jet these bugs will have to be either caught by some test or by manual testing.
## Dependencies ## 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 ## Contributing

View file

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

View file

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

View file

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

View file

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

View file

@ -5,8 +5,7 @@ import (
"fmt" "fmt"
"github.com/go-jet/jet/generator/internal/metadata" "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/metadata/postgres-metadata"
"github.com/go-jet/jet/generator/internal/utils" "github.com/go-jet/jet/internal/utils"
"github.com/go-jet/jet/internal/util"
_ "github.com/lib/pq" _ "github.com/lib/pq"
"path" "path"
"path/filepath" "path/filepath"
@ -116,7 +115,7 @@ func generate(schemaInfo postgres_metadata.SchemaInfo, dirPath, packageName stri
return err 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 { if err != nil {
return err 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 ( import (
"bytes" "bytes"
"github.com/go-jet/jet/internal/util" "github.com/go-jet/jet/internal/3rdparty/snaker"
"go/format" "go/format"
"os" "os"
"path/filepath" "path/filepath"
"strconv"
"strings"
"text/template" "text/template"
"time" "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 { func SaveGoFile(dirPath, fileName string, text []byte) error {
newGoFilePath := filepath.Join(dirPath, fileName) + ".go" newGoFilePath := filepath.Join(dirPath, fileName) + ".go"
@ -50,7 +64,7 @@ func EnsureDirPath(dirPath string) error {
func GenerateTemplate(templateText string, templateData interface{}) ([]byte, error) { func GenerateTemplate(templateText string, templateData interface{}) ([]byte, error) {
t, err := template.New("sqlBuilderTableTemplate").Funcs(template.FuncMap{ t, err := template.New("sqlBuilderTableTemplate").Funcs(template.FuncMap{
"ToGoIdentifier": util.ToGoIdentifier, "ToGoIdentifier": ToGoIdentifier,
"now": func() string { "now": func() string {
return time.Now().Format(time.RFC850) return time.Now().Format(time.RFC850)
}, },
@ -96,3 +110,45 @@ func DirExists(path string) (bool, error) {
} }
return true, err 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 ( import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"

View file

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