init - add project files

This commit is contained in:
2025-03-06 23:54:11 -05:00
commit e724ff1120
1363 changed files with 897467 additions and 0 deletions

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,107 @@
package dbidentifier
import (
"github.com/go-jet/jet/v2/internal/3rdparty/snaker"
"strings"
"unicode"
)
// 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(identifier string) string {
increase, needs := needsCharReplacement(identifier)
if !needs {
return identifier
}
var b strings.Builder
b.Grow(len(identifier) + increase)
for _, c := range identifier {
switch {
case unicode.IsSpace(c):
b.WriteByte('_')
case unicode.IsControl(c):
continue
default:
replacement, ok := asciiCharacterReplacement[c]
if ok {
b.WriteByte('_')
b.WriteString(replacement)
b.WriteByte('_')
} else {
b.WriteRune(c)
}
}
}
return b.String()
}
func needsCharReplacement(identifier string) (increase int, needs bool) {
for _, c := range identifier {
switch {
case unicode.IsSpace(c):
needs = true
case unicode.IsControl(c):
increase += -1
needs = true
continue
default:
replacement, ok := asciiCharacterReplacement[c]
if ok {
increase += len(replacement) + 1
needs = true
}
}
}
return increase, needs
}
var asciiCharacterReplacement = map[rune]string{
'!': "exclamation",
'"': "quotation",
'#': "number",
'$': "dollar",
'%': "percent",
'&': "ampersand",
'\'': "apostrophe",
'(': "opening_parentheses",
')': "closing_parentheses",
'*': "asterisk",
'+': "plus",
',': "comma",
'-': "_",
'.': "_",
'/': "slash",
':': "colon",
';': "semicolon",
'<': "less",
'=': "equal",
'>': "greater",
'?': "question",
'@': "at",
'[': "opening_bracket",
'\\': "backslash",
']': "closing_bracket",
'^': "caret",
'`': "accent",
'{': "opening_braces",
'|': "vertical_bar",
'}': "closing_braces",
'~': "tilde",
}

View File

@@ -0,0 +1,68 @@
package dbidentifier
import (
"github.com/stretchr/testify/require"
"testing"
)
func TestToGoIdentifier(t *testing.T) {
require.Equal(t, ToGoIdentifier(""), "")
require.Equal(t, ToGoIdentifier("uuid"), "UUID")
require.Equal(t, ToGoIdentifier("uuid_ptr"), "UUIDPtr")
require.Equal(t, ToGoIdentifier("col1"), "Col1")
require.Equal(t, ToGoIdentifier("PG-13"), "Pg13")
require.Equal(t, ToGoIdentifier("13_pg"), "13Pg")
require.Equal(t, ToGoIdentifier("mytable"), "Mytable")
require.Equal(t, ToGoIdentifier("MYTABLE"), "Mytable")
require.Equal(t, ToGoIdentifier("MyTaBlE"), "MyTaBlE")
require.Equal(t, ToGoIdentifier("myTaBlE"), "MyTaBlE")
require.Equal(t, ToGoIdentifier("my_table"), "MyTable")
require.Equal(t, ToGoIdentifier("my_____table"), "MyTable")
require.Equal(t, ToGoIdentifier("MY_TABLE"), "MyTable")
require.Equal(t, ToGoIdentifier("My_Table"), "MyTable")
require.Equal(t, ToGoIdentifier("My Table"), "MyTable")
require.Equal(t, ToGoIdentifier("My-Table"), "MyTable")
require.Equal(t, ToGoIdentifier("EN\bUM"), "Enum") // control character
require.Equal(t, ToGoIdentifier("EN\tUM"), "EnUm") // space character
require.Equal(t, ToGoIdentifier("S3:INIT"), "S3ColonInit") // replacement chars
require.Equal(t, ToGoIdentifier("Entity-"), "Entity")
require.Equal(t, ToGoIdentifier("Entity+"), "EntityPlus")
require.Equal(t, ToGoIdentifier("="), "Equal")
require.Equal(t, ToGoIdentifier("<="), "LessEqual")
require.Equal(t, ToGoIdentifier(">="), "GreaterEqual")
require.Equal(t, ToGoIdentifier("some#$%name"), "SomeNumberDollarPercentName")
require.Equal(t, ToGoIdentifier(`An!"them`), "AnExclamationQuotationThem")
require.Equal(t, ToGoIdentifier(`An(Um)`),
"AnOpeningParenthesesUmClosingParentheses")
}
func TestNeedsCharReplacement(t *testing.T) {
increase, needs := needsCharReplacement("some_name")
require.False(t, needs)
require.Zero(t, increase)
increase, needs = needsCharReplacement("some name")
require.True(t, needs)
require.Zero(t, increase)
increase, needs = needsCharReplacement("some\bname")
require.True(t, needs)
require.Equal(t, increase, -1)
increase, needs = needsCharReplacement("some#$%name")
require.True(t, needs)
require.Equal(t, increase, 22)
}
func TestToGoFileName(t *testing.T) {
require.Equal(t, ToGoFileName("FileName"), "filename")
require.Equal(t, ToGoFileName("File_Name"), "file_name")
require.Equal(t, ToGoFileName("File___Name__"), "file___name__")
require.Equal(t, ToGoFileName("File___Name__"), "file___name__")
require.Equal(t, ToGoFileName("File\bName"), "filename")
require.Equal(t, ToGoFileName("File\tName"), "file_name")
require.Equal(t, ToGoFileName("File^^Name"), "file_caret__caret_name")
}

View File

@@ -0,0 +1,14 @@
package errfmt
import (
"github.com/go-jet/jet/v2/internal/utils/is"
"strings"
)
// Trace returns well formatted wrapped error trace string
func Trace(err error) string {
if is.Nil(err) {
return ""
}
return "Error trace:\n" + " - " + strings.Replace(err.Error(), ": ", ":\n - ", -1)
}

View File

@@ -0,0 +1,89 @@
package filesys
import (
"errors"
"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) // #nosec 304
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 {
_, writeErr := file.Write(text)
if writeErr != nil {
return errors.Join(writeErr, fmt.Errorf("failed to format '%s', check '%s' for syntax errors: %w", fileName, newGoFilePath, err))
}
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, 0o750)
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
}

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,9 @@
package min
// Int returns minimum of two int values
func Int(a, b int) int {
if a < b {
return a
}
return b
}

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,6 @@
package ptr
// Of returns the address of any given parameter
func Of[T any](value T) *T {
return &value
}

View File

@@ -0,0 +1,66 @@
package semantic
import (
"fmt"
"strconv"
"strings"
)
// Version struct holds semantic versioning information
type Version struct {
Major int
Minor int
Patch int
}
// VersionFromString creates new semantic Version by parsing version string
func VersionFromString(version string) (Version, error) {
parts := strings.Split(version, ".")
var ret Version
if len(parts) > 0 {
major, err := strconv.Atoi(parts[0])
if err != nil {
return ret, fmt.Errorf("major is not a number: %w", err)
}
ret.Major = major
}
if len(parts) > 1 {
minor, err := strconv.Atoi(parts[1])
if err != nil {
return ret, fmt.Errorf("minor is not a number: %w", err)
}
ret.Minor = minor
}
if len(parts) > 2 {
patch, err := strconv.Atoi(parts[2])
if err != nil {
return ret, fmt.Errorf("patch is not a number: %w", err)
}
ret.Patch = patch
}
return ret, nil
}
// Lt returns true if this version is less than version parameter
func (v Version) Lt(version Version) bool {
if v.Major < version.Major {
return true
}
if v.Minor < version.Minor {
return true
}
return v.Patch < version.Patch
}

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

@@ -0,0 +1,8 @@
package throw
// OnError will panic if err is not nill
func OnError(err error) {
if err != nil {
panic(err)
}
}