Add the ability to fully customize jet generated files.

This commit is contained in:
go-jet 2021-07-27 17:39:21 +02:00
parent caa81930dc
commit 8864667f47
40 changed files with 2274 additions and 882 deletions

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)
}
}

View file

@ -10,7 +10,6 @@ import (
"reflect"
"strings"
"time"
"unicode"
)
// ToGoIdentifier converts database to Go identifier.
@ -18,16 +17,6 @@ func ToGoIdentifier(databaseIdentifier string) string {
return snaker.SnakeToCamel(replaceInvalidChars(databaseIdentifier))
}
// ToGoEnumValueIdentifier converts enum value name to Go identifier name.
func ToGoEnumValueIdentifier(enumName, enumValue string) string {
enumValueIdentifier := ToGoIdentifier(enumValue)
if !unicode.IsLetter([]rune(enumValueIdentifier)[0]) {
return ToGoIdentifier(enumName) + enumValueIdentifier
}
return enumValueIdentifier
}
// ToGoFileName converts database identifier to Go file name.
func ToGoFileName(databaseIdentifier string) string {
return strings.ToLower(replaceInvalidChars(databaseIdentifier))
@ -35,7 +24,11 @@ func ToGoFileName(databaseIdentifier string) string {
// 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) + ".go"
newGoFilePath := filepath.Join(dirPath, fileName)
if !strings.HasSuffix(newGoFilePath, ".go") {
newGoFilePath += ".go"
}
file, err := os.Create(newGoFilePath)
@ -160,13 +153,6 @@ func MustBeInitializedPtr(val interface{}, errorStr string) {
}
}
// PanicOnError panics if err is not nil
func PanicOnError(err error) {
if err != nil {
panic(err)
}
}
// ErrorCatch is used in defer to recover from panics and to set err
func ErrorCatch(err *error) {
recovered := recover()

View file

@ -25,11 +25,6 @@ func TestToGoIdentifier(t *testing.T) {
require.Equal(t, ToGoIdentifier("My-Table"), "MyTable")
}
func TestToGoEnumValueIdentifier(t *testing.T) {
require.Equal(t, ToGoEnumValueIdentifier("enum_name", "enum_value"), "EnumValue")
require.Equal(t, ToGoEnumValueIdentifier("NumEnum", "100"), "NumEnum100")
}
func TestErrorCatchErr(t *testing.T) {
var err error