From 55e8c3bbb1b82902ec7b18f91dcb3ba320f8a664 Mon Sep 17 00:00:00 2001 From: go-jet Date: Thu, 4 Jul 2019 17:54:15 +0200 Subject: [PATCH] Add Dependencies info to README.md. --- README.md | 8 ++- clause.go | 4 +- execution/execution.go | 6 +- .../metadata/postgres-metadata/column_info.go | 4 +- .../metadata/postgres-metadata/table_info.go | 4 +- generator/postgresgen/generator.go | 5 +- internal/util/utils.go | 25 -------- .../internal => internal}/utils/utils.go | 60 ++++++++++++++++++- internal/{util => utils}/utils_test.go | 2 +- utils.go | 4 +- 10 files changed, 79 insertions(+), 43 deletions(-) delete mode 100644 internal/util/utils.go rename {generator/internal => internal}/utils/utils.go (50%) rename internal/{util => utils}/utils_test.go (98%) diff --git a/README.md b/README.md index d24c142..213ad7b 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/clause.go b/clause.go index d2a08e9..c3928e7 100644 --- a/clause.go +++ b/clause.go @@ -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]" } diff --git a/execution/execution.go b/execution/execution.go index d836770..751bb86 100644 --- a/execution/execution.go +++ b/execution/execution.go @@ -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 diff --git a/generator/internal/metadata/postgres-metadata/column_info.go b/generator/internal/metadata/postgres-metadata/column_info.go index 20e168d..aaf0949 100644 --- a/generator/internal/metadata/postgres-metadata/column_info.go +++ b/generator/internal/metadata/postgres-metadata/column_info.go @@ -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": diff --git a/generator/internal/metadata/postgres-metadata/table_info.go b/generator/internal/metadata/postgres-metadata/table_info.go index 3b7c67e..29afae9 100644 --- a/generator/internal/metadata/postgres-metadata/table_info.go +++ b/generator/internal/metadata/postgres-metadata/table_info.go @@ -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) { diff --git a/generator/postgresgen/generator.go b/generator/postgresgen/generator.go index dee6026..a291ac8 100644 --- a/generator/postgresgen/generator.go +++ b/generator/postgresgen/generator.go @@ -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 diff --git a/internal/util/utils.go b/internal/util/utils.go deleted file mode 100644 index e0e1830..0000000 --- a/internal/util/utils.go +++ /dev/null @@ -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 -} diff --git a/generator/internal/utils/utils.go b/internal/utils/utils.go similarity index 50% rename from generator/internal/utils/utils.go rename to internal/utils/utils.go index f32147c..21efb05 100644 --- a/generator/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -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 +} diff --git a/internal/util/utils_test.go b/internal/utils/utils_test.go similarity index 98% rename from internal/util/utils_test.go rename to internal/utils/utils_test.go index e77b356..7a3a370 100644 --- a/internal/util/utils_test.go +++ b/internal/utils/utils_test.go @@ -1,4 +1,4 @@ -package util +package utils import ( "github.com/stretchr/testify/assert" diff --git a/utils.go b/utils.go index cfc9bff..27c814e 100644 --- a/utils.go +++ b/utils.go @@ -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)