2019-06-23 18:55:57 +02:00
|
|
|
package utils
|
2019-03-04 19:35:49 +01:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
2019-07-04 17:54:15 +02:00
|
|
|
"github.com/go-jet/jet/internal/3rdparty/snaker"
|
2019-03-04 19:35:49 +01:00
|
|
|
"go/format"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
2019-07-20 17:44:43 +02:00
|
|
|
"reflect"
|
2019-07-04 17:54:15 +02:00
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
2019-03-04 19:35:49 +01:00
|
|
|
"text/template"
|
2019-05-24 13:13:13 +02:00
|
|
|
"time"
|
2019-03-04 19:35:49 +01:00
|
|
|
)
|
|
|
|
|
|
2019-07-18 18:42:03 +02:00
|
|
|
// ToGoIdentifier converts database to Go identifier.
|
2019-07-04 17:54:15 +02:00
|
|
|
func ToGoIdentifier(databaseIdentifier string) string {
|
|
|
|
|
return snaker.SnakeToCamel(replaceInvalidChars(databaseIdentifier))
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-18 18:42:03 +02:00
|
|
|
// ToGoFileName converts database identifier to Go file name.
|
2019-07-04 17:54:15 +02:00
|
|
|
func ToGoFileName(databaseIdentifier string) string {
|
|
|
|
|
return strings.ToLower(replaceInvalidChars(databaseIdentifier))
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-18 18:42:03 +02:00
|
|
|
// SaveGoFile saves go file at folder dir, with name fileName and contents text.
|
2019-06-23 18:55:57 +02:00
|
|
|
func SaveGoFile(dirPath, fileName string, text []byte) error {
|
2019-03-04 19:35:49 +01:00
|
|
|
newGoFilePath := filepath.Join(dirPath, fileName) + ".go"
|
|
|
|
|
|
|
|
|
|
file, err := os.Create(newGoFilePath)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
|
|
p, err := format.Source(text)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = file.Write(p)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-18 18:42:03 +02:00
|
|
|
// EnsureDirPath ensures dir path exists. If path does not exist, creates new path.
|
2019-06-23 18:55:57 +02:00
|
|
|
func EnsureDirPath(dirPath string) error {
|
2019-03-04 19:35:49 +01:00
|
|
|
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
|
|
|
|
|
err := os.MkdirAll(dirPath, os.ModePerm)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-18 18:42:03 +02:00
|
|
|
// GenerateTemplate generates template with template text and template data.
|
2019-06-23 18:55:57 +02:00
|
|
|
func GenerateTemplate(templateText string, templateData interface{}) ([]byte, error) {
|
2019-03-04 19:35:49 +01:00
|
|
|
|
2019-05-24 13:13:13 +02:00
|
|
|
t, err := template.New("sqlBuilderTableTemplate").Funcs(template.FuncMap{
|
2019-07-04 17:54:15 +02:00
|
|
|
"ToGoIdentifier": ToGoIdentifier,
|
2019-05-24 13:13:13 +02:00
|
|
|
"now": func() string {
|
|
|
|
|
return time.Now().Format(time.RFC850)
|
|
|
|
|
},
|
2019-03-04 19:35:49 +01:00
|
|
|
}).Parse(templateText)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
if err := t.Execute(&buf, templateData); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-18 18:42:03 +02:00
|
|
|
// CleanUpGeneratedFiles deletes everything at folder dir.
|
2019-06-23 18:55:57 +02:00
|
|
|
func CleanUpGeneratedFiles(dir string) error {
|
|
|
|
|
exist, err := DirExists(dir)
|
2019-03-04 19:35:49 +01:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if exist {
|
|
|
|
|
err := os.RemoveAll(dir)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-18 18:42:03 +02:00
|
|
|
// DirExists checks if folder at path exist.
|
2019-06-23 18:55:57 +02:00
|
|
|
func DirExists(path string) (bool, error) {
|
2019-03-04 19:35:49 +01:00
|
|
|
_, err := os.Stat(path)
|
|
|
|
|
if err == nil {
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
return true, err
|
|
|
|
|
}
|
2019-07-04 17:54:15 +02:00
|
|
|
|
|
|
|
|
func replaceInvalidChars(str string) string {
|
|
|
|
|
str = strings.Replace(str, " ", "_", -1)
|
|
|
|
|
str = strings.Replace(str, "-", "_", -1)
|
|
|
|
|
|
|
|
|
|
return str
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-18 18:42:03 +02:00
|
|
|
// FormatTimestamp formats t into Postgres' text format for timestamps. From: github.com/lib/pq
|
2019-07-04 17:54:15 +02:00
|
|
|
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
|
|
|
|
|
}
|
2019-07-20 17:44:43 +02:00
|
|
|
|
|
|
|
|
func IsNil(v interface{}) bool {
|
|
|
|
|
return v == nil || (reflect.ValueOf(v).Kind() == reflect.Ptr && reflect.ValueOf(v).IsNil())
|
|
|
|
|
}
|