Break utils package into subpackages.

This commit is contained in:
go-jet 2023-07-21 14:11:31 +02:00
parent 06ecd73f67
commit d7a5adb239
25 changed files with 276 additions and 318 deletions

View file

@ -1,8 +1,6 @@
package jet
import (
"github.com/go-jet/jet/v2/internal/utils"
)
import "github.com/go-jet/jet/v2/internal/utils/is"
// Clause interface
type Clause interface {
@ -322,7 +320,7 @@ func (u *ClauseUpdate) Serialize(statementType StatementType, out *SQLBuilder, o
out.WriteString("UPDATE")
u.OptimizerHints.Serialize(statementType, out, options...)
if utils.IsNil(u.Table) {
if is.Nil(u.Table) {
panic("jet: table to update is nil")
}
@ -387,7 +385,7 @@ func (i *ClauseInsert) GetColumns() []Column {
// Serialize serializes clause into SQLBuilder
func (i *ClauseInsert) Serialize(statementType StatementType, out *SQLBuilder, options ...SerializeOption) {
if utils.IsNil(i.Table) {
if is.Nil(i.Table) {
panic("jet: table is nil for INSERT clause")
}

View file

@ -5,7 +5,7 @@ import (
"database/sql/driver"
"fmt"
"github.com/go-jet/jet/v2/internal/3rdparty/pq"
"github.com/go-jet/jet/v2/internal/utils"
"github.com/go-jet/jet/v2/internal/utils/is"
"github.com/google/uuid"
"reflect"
"sort"
@ -206,7 +206,7 @@ func (s *SQLBuilder) insertRawQuery(raw string, namedArg map[string]interface{})
}
func argToString(value interface{}) string {
if utils.IsNil(value) {
if is.Nil(value) {
return "NULL"
}

View file

@ -1,8 +1,6 @@
package jet
import (
"github.com/go-jet/jet/v2/internal/utils"
)
import "github.com/go-jet/jet/v2/internal/utils/is"
// SerializerTable interface
type SerializerTable interface {
@ -158,7 +156,7 @@ func (t *joinTableImpl) serialize(statement StatementType, out *SQLBuilder, opti
panic("jet: Join table is nil. ")
}
if utils.IsNil(t.lhs) {
if is.Nil(t.lhs) {
panic("jet: left hand side of join operation is nil table")
}
@ -179,7 +177,7 @@ func (t *joinTableImpl) serialize(statement StatementType, out *SQLBuilder, opti
out.WriteString("CROSS JOIN")
}
if utils.IsNil(t.rhs) {
if is.Nil(t.rhs) {
panic("jet: right hand side of join operation is nil table")
}

View file

@ -1,7 +1,8 @@
package jet
import (
"github.com/go-jet/jet/v2/internal/utils"
"github.com/go-jet/jet/v2/internal/utils/dbidentifier"
"github.com/go-jet/jet/v2/internal/utils/must"
"reflect"
"strings"
)
@ -150,11 +151,11 @@ func UnwindRowFromModel(columns []Column, data interface{}) []Serializer {
row := []Serializer{}
utils.ValueMustBe(structValue, reflect.Struct, "jet: data has to be a struct")
must.ValueBeOfTypeKind(structValue, reflect.Struct, "jet: data has to be a struct")
for _, column := range columns {
columnName := column.Name()
structFieldName := utils.ToGoIdentifier(columnName)
structFieldName := dbidentifier.ToGoIdentifier(columnName)
structField := structValue.FieldByName(structFieldName)
@ -179,7 +180,7 @@ func UnwindRowFromModel(columns []Column, data interface{}) []Serializer {
// UnwindRowsFromModels func
func UnwindRowsFromModels(columns []Column, data interface{}) [][]Serializer {
sliceValue := reflect.Indirect(reflect.ValueOf(data))
utils.ValueMustBe(sliceValue, reflect.Slice, "jet: data has to be a slice.")
must.ValueBeOfTypeKind(sliceValue, reflect.Slice, "jet: data has to be a slice.")
rows := [][]Serializer{}

View file

@ -0,0 +1,22 @@
package datetime
import "time"
// ExtractTimeComponents extracts number of days, hours, minutes, seconds, microseconds from duration
func ExtractTimeComponents(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
}

View file

@ -0,0 +1,24 @@
package dbidentifier
import (
"github.com/go-jet/jet/v2/internal/3rdparty/snaker"
"strings"
)
// ToGoIdentifier converts database identifier to Go identifier.
func ToGoIdentifier(databaseIdentifier string) string {
return snaker.SnakeToCamel(replaceInvalidChars(databaseIdentifier))
}
// ToGoFileName converts database identifier to Go file name.
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)
str = strings.Replace(str, ".", "_", -1)
return str
}

View file

@ -1,7 +1,6 @@
package utils
package dbidentifier
import (
"fmt"
"github.com/stretchr/testify/require"
"testing"
)
@ -24,27 +23,3 @@ func TestToGoIdentifier(t *testing.T) {
require.Equal(t, ToGoIdentifier("My Table"), "MyTable")
require.Equal(t, ToGoIdentifier("My-Table"), "MyTable")
}
func TestErrorCatchErr(t *testing.T) {
var err error
func() {
defer ErrorCatch(&err)
panic(fmt.Errorf("newError"))
}()
require.Error(t, err, "newError")
}
func TestErrorCatchNonErr(t *testing.T) {
var err error
func() {
defer ErrorCatch(&err)
panic(11)
}()
require.Error(t, err, "11")
}

View file

@ -0,0 +1,85 @@
package filesys
import (
"fmt"
"go/format"
"os"
"path/filepath"
"strings"
)
// FormatAndSaveGoFile saves go file at folder dir, with name fileName and contents text.
func FormatAndSaveGoFile(dirPath, fileName string, text []byte) error {
newGoFilePath := filepath.Join(dirPath, fileName)
if !strings.HasSuffix(newGoFilePath, ".go") {
newGoFilePath += ".go"
}
file, err := os.Create(newGoFilePath)
if err != nil {
return err
}
defer file.Close()
p, err := format.Source(text)
// if there is a format error we will write unformulated text for debug purposes
if err != nil {
file.Write(text)
return fmt.Errorf("failed to format '%s', check '%s' for syntax errors: %w", fileName, newGoFilePath, err)
}
_, err = file.Write(p)
if err != nil {
return fmt.Errorf("failed to save '%s' file: %w", newGoFilePath, err)
}
return nil
}
// EnsureDirPathExist ensures dir path exists. If path does not exist, creates new path.
func EnsureDirPathExist(dirPath string) error {
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
err := os.MkdirAll(dirPath, os.ModePerm)
if err != nil {
return fmt.Errorf("can't create directory - %s: %w", dirPath, err)
}
}
return nil
}
// RemoveDir deletes everything at folder dir.
func RemoveDir(dir string) error {
exist, err := DirExists(dir)
if err != nil {
return err
}
if exist {
err := os.RemoveAll(dir)
if err != nil {
return err
}
}
return nil
}
// DirExists checks if folder at path exist.
func DirExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}

8
internal/utils/is/is.go Normal file
View file

@ -0,0 +1,8 @@
package is
import "reflect"
// Nil check if v is nil
func Nil(v interface{}) bool {
return v == nil || (reflect.ValueOf(v).Kind() == reflect.Ptr && reflect.ValueOf(v).IsNil())
}

View file

@ -0,0 +1,41 @@
package must
import (
"github.com/go-jet/jet/v2/internal/utils/is"
"reflect"
)
// BeTrue panics when condition is false
func BeTrue(condition bool, errorStr string) {
if !condition {
panic(errorStr)
}
}
// BeTypeKind panics with errorStr error, if v interface is not of reflect kind
func BeTypeKind(v interface{}, kind reflect.Kind, errorStr string) {
if reflect.TypeOf(v).Kind() != kind {
panic(errorStr)
}
}
// ValueBeOfTypeKind panics with errorStr error, if v value is not of reflect kind
func ValueBeOfTypeKind(v reflect.Value, kind reflect.Kind, errorStr string) {
if v.Kind() != kind {
panic(errorStr)
}
}
// TypeBeOfKind panics with errorStr error, if v type is not of reflect kind
func TypeBeOfKind(v reflect.Type, kind reflect.Kind, errorStr string) {
if v.Kind() != kind {
panic(errorStr)
}
}
// BeInitializedPtr panics with errorStr if val interface is nil
func BeInitializedPtr(val interface{}, errorStr string) {
if is.Nil(val) {
panic(errorStr)
}
}

View file

@ -0,0 +1,12 @@
package strslice
// Contains checks if slice of strings contains a string
func Contains(strings []string, contains string) bool {
for _, str := range strings {
if str == contains {
return true
}
}
return false
}

View file

@ -1,203 +0,0 @@
package utils
import (
"database/sql"
"fmt"
"github.com/go-jet/jet/v2/internal/3rdparty/snaker"
"go/format"
"os"
"path/filepath"
"reflect"
"strings"
"time"
)
// ToGoIdentifier converts database to Go identifier.
func ToGoIdentifier(databaseIdentifier string) string {
return snaker.SnakeToCamel(replaceInvalidChars(databaseIdentifier))
}
// ToGoFileName converts database identifier to Go file name.
func ToGoFileName(databaseIdentifier string) string {
return strings.ToLower(replaceInvalidChars(databaseIdentifier))
}
// SaveGoFile saves go file at folder dir, with name fileName and contents text.
func SaveGoFile(dirPath, fileName string, text []byte) error {
newGoFilePath := filepath.Join(dirPath, fileName)
if !strings.HasSuffix(newGoFilePath, ".go") {
newGoFilePath += ".go"
}
file, err := os.Create(newGoFilePath)
if err != nil {
return err
}
defer file.Close()
p, err := format.Source(text)
// if there is a format error we will write unformulated text for debug purposes
if err != nil {
file.Write(text)
return fmt.Errorf("failed to format '%s', check '%s' for syntax errors: %w", fileName, newGoFilePath, err)
}
_, err = file.Write(p)
if err != nil {
return fmt.Errorf("failed to save '%s' file: %w", newGoFilePath, err)
}
return nil
}
// EnsureDirPath ensures dir path exists. If path does not exist, creates new path.
func EnsureDirPath(dirPath string) error {
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
err := os.MkdirAll(dirPath, os.ModePerm)
if err != nil {
return fmt.Errorf("can't create directory - %s: %w", dirPath, err)
}
}
return nil
}
// CleanUpGeneratedFiles deletes everything at folder dir.
func CleanUpGeneratedFiles(dir string) error {
exist, err := DirExists(dir)
if err != nil {
return err
}
if exist {
err := os.RemoveAll(dir)
if err != nil {
return err
}
}
return nil
}
// DBClose closes non nil db connection
func DBClose(db *sql.DB) {
if db == nil {
return
}
db.Close()
}
// DirExists checks if folder at path exist.
func DirExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}
func replaceInvalidChars(str string) string {
str = strings.Replace(str, " ", "_", -1)
str = strings.Replace(str, "-", "_", -1)
str = strings.Replace(str, ".", "_", -1)
return str
}
// IsNil check if v is nil
func IsNil(v interface{}) bool {
return v == nil || (reflect.ValueOf(v).Kind() == reflect.Ptr && reflect.ValueOf(v).IsNil())
}
// MustBeTrue panics when condition is false
func MustBeTrue(condition bool, errorStr string) {
if !condition {
panic(errorStr)
}
}
// MustBe panics with errorStr error, if v interface is not of reflect kind
func MustBe(v interface{}, kind reflect.Kind, errorStr string) {
if reflect.TypeOf(v).Kind() != kind {
panic(errorStr)
}
}
// ValueMustBe panics with errorStr error, if v value is not of reflect kind
func ValueMustBe(v reflect.Value, kind reflect.Kind, errorStr string) {
if v.Kind() != kind {
panic(errorStr)
}
}
// TypeMustBe panics with errorStr error, if v type is not of reflect kind
func TypeMustBe(v reflect.Type, kind reflect.Kind, errorStr string) {
if v.Kind() != kind {
panic(errorStr)
}
}
// MustBeInitializedPtr panics with errorStr if val interface is nil
func MustBeInitializedPtr(val interface{}, errorStr string) {
if IsNil(val) {
panic(errorStr)
}
}
// 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)
}
}
// StringSliceContains checks if slice of strings contains a string
func StringSliceContains(strings []string, contains string) bool {
for _, str := range strings {
if str == contains {
return true
}
}
return false
}
// ExtractDateTimeComponents extracts number of days, hours, minutes, seconds, microseconds from duration
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
}