2019-06-23 18:55:57 +02:00
|
|
|
package utils
|
2019-03-04 19:35:49 +01:00
|
|
|
|
|
|
|
|
import (
|
2019-08-08 12:02:32 +02:00
|
|
|
"database/sql"
|
2019-09-20 18:20:26 +02:00
|
|
|
"fmt"
|
2020-06-27 18:48:19 +02:00
|
|
|
"github.com/go-jet/jet/v2/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
|
|
|
"strings"
|
2019-12-01 18:25:30 +01: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 {
|
2021-07-27 17:39:21 +02:00
|
|
|
newGoFilePath := filepath.Join(dirPath, fileName)
|
|
|
|
|
|
|
|
|
|
if !strings.HasSuffix(newGoFilePath, ".go") {
|
|
|
|
|
newGoFilePath += ".go"
|
|
|
|
|
}
|
2019-03-04 19:35:49 +01:00
|
|
|
|
|
|
|
|
file, err := os.Create(newGoFilePath)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
|
|
p, err := format.Source(text)
|
2023-07-21 13:20:44 +02:00
|
|
|
|
|
|
|
|
// if there is a format error we will write unformulated text for debug purposes
|
2019-03-04 19:35:49 +01:00
|
|
|
if err != nil {
|
2023-07-21 13:20:44 +02:00
|
|
|
file.Write(text)
|
|
|
|
|
return fmt.Errorf("failed to format '%s', check '%s' for syntax errors: %w", fileName, newGoFilePath, err)
|
2019-03-04 19:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = file.Write(p)
|
|
|
|
|
if err != nil {
|
2023-07-21 13:20:44 +02:00
|
|
|
return fmt.Errorf("failed to save '%s' file: %w", newGoFilePath, err)
|
2019-03-04 19:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 {
|
2023-07-21 13:20:44 +02:00
|
|
|
return fmt.Errorf("can't create directory - %s: %w", dirPath, err)
|
2019-03-04 19:35:49 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 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-08-17 10:43:16 +02:00
|
|
|
// DBClose closes non nil db connection
|
2019-08-08 12:02:32 +02:00
|
|
|
func DBClose(db *sql.DB) {
|
|
|
|
|
if db == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
db.Close()
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
2019-09-20 12:53:52 +02:00
|
|
|
str = strings.Replace(str, ".", "_", -1)
|
2019-07-04 17:54:15 +02:00
|
|
|
|
|
|
|
|
return str
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-17 18:32:01 +02:00
|
|
|
// IsNil check if v is nil
|
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())
|
|
|
|
|
}
|
2019-08-13 13:57:26 +02:00
|
|
|
|
2019-10-18 10:09:56 +02:00
|
|
|
// MustBeTrue panics when condition is false
|
|
|
|
|
func MustBeTrue(condition bool, errorStr string) {
|
|
|
|
|
if !condition {
|
|
|
|
|
panic(errorStr)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-17 10:43:16 +02:00
|
|
|
// MustBe panics with errorStr error, if v interface is not of reflect kind
|
2019-08-13 13:57:26 +02:00
|
|
|
func MustBe(v interface{}, kind reflect.Kind, errorStr string) {
|
|
|
|
|
if reflect.TypeOf(v).Kind() != kind {
|
|
|
|
|
panic(errorStr)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-17 10:43:16 +02:00
|
|
|
// ValueMustBe panics with errorStr error, if v value is not of reflect kind
|
2019-08-13 13:57:26 +02:00
|
|
|
func ValueMustBe(v reflect.Value, kind reflect.Kind, errorStr string) {
|
|
|
|
|
if v.Kind() != kind {
|
|
|
|
|
panic(errorStr)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-17 10:43:16 +02:00
|
|
|
// TypeMustBe panics with errorStr error, if v type is not of reflect kind
|
2019-08-13 13:57:26 +02:00
|
|
|
func TypeMustBe(v reflect.Type, kind reflect.Kind, errorStr string) {
|
|
|
|
|
if v.Kind() != kind {
|
|
|
|
|
panic(errorStr)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-17 10:43:16 +02:00
|
|
|
// MustBeInitializedPtr panics with errorStr if val interface is nil
|
2019-08-13 13:57:26 +02:00
|
|
|
func MustBeInitializedPtr(val interface{}, errorStr string) {
|
|
|
|
|
if IsNil(val) {
|
|
|
|
|
panic(errorStr)
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-20 13:55:07 +02:00
|
|
|
|
2019-09-20 18:20:26 +02:00
|
|
|
// ErrorCatch is used in defer to recover from panics and to set err
|
|
|
|
|
func ErrorCatch(err *error) {
|
|
|
|
|
recovered := recover()
|
|
|
|
|
|
|
|
|
|
if recovered == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
recoveredErr, isError := recovered.(error)
|
|
|
|
|
|
|
|
|
|
if isError {
|
|
|
|
|
*err = recoveredErr
|
|
|
|
|
} else {
|
|
|
|
|
*err = fmt.Errorf("%v", recovered)
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-27 11:34:12 +02:00
|
|
|
|
2019-10-18 10:09:56 +02:00
|
|
|
// StringSliceContains checks if slice of strings contains a string
|
2019-09-27 11:34:12 +02:00
|
|
|
func StringSliceContains(strings []string, contains string) bool {
|
|
|
|
|
for _, str := range strings {
|
|
|
|
|
if str == contains {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|
2019-12-01 18:25:30 +01:00
|
|
|
|
2019-12-08 11:07:49 +01:00
|
|
|
// ExtractDateTimeComponents extracts number of days, hours, minutes, seconds, microseconds from duration
|
2019-12-01 18:25:30 +01:00
|
|
|
func ExtractDateTimeComponents(duration time.Duration) (days, hours, minutes, seconds, microseconds int64) {
|
|
|
|
|
days = int64(duration / (24 * time.Hour))
|
|
|
|
|
reminder := duration % (24 * time.Hour)
|
|
|
|
|
|
|
|
|
|
hours = int64(reminder / time.Hour)
|
|
|
|
|
reminder = reminder % time.Hour
|
|
|
|
|
|
|
|
|
|
minutes = int64(reminder / time.Minute)
|
|
|
|
|
reminder = reminder % time.Minute
|
|
|
|
|
|
|
|
|
|
seconds = int64(reminder / time.Second)
|
|
|
|
|
reminder = reminder % time.Second
|
|
|
|
|
|
|
|
|
|
microseconds = int64(reminder / time.Microsecond)
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|