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

@ -9,8 +9,12 @@ import (
)
// SnakeToCamel returns a string converted from snake case to uppercase
func SnakeToCamel(s string) string {
return snakeToCamel(s, true)
func SnakeToCamel(s string, firstLetterUppercase ...bool) string {
upperCase := true
if len(firstLetterUppercase) > 0 {
upperCase = firstLetterUppercase[0]
}
return snakeToCamel(s, upperCase)
}
func snakeToCamel(s string, upperCase bool) string {

View file

@ -5,7 +5,7 @@ import (
"encoding/json"
"fmt"
"github.com/go-jet/jet/v2/internal/jet"
"github.com/go-jet/jet/v2/internal/utils"
"github.com/go-jet/jet/v2/internal/utils/throw"
"github.com/go-jet/jet/v2/qrm"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
@ -66,7 +66,7 @@ func SaveJSONFile(v interface{}, testRelativePath string) {
filePath := getFullPath(testRelativePath)
err := ioutil.WriteFile(filePath, jsonText, 0644)
utils.PanicOnError(err)
throw.OnError(err)
}
// AssertJSONFile check if data json representation is the same as json at testRelativePath

View file

@ -1,7 +1,7 @@
package testutils
import (
"github.com/go-jet/jet/v2/internal/utils"
"github.com/go-jet/jet/v2/internal/utils/throw"
"strings"
"time"
)
@ -10,7 +10,7 @@ import (
func Date(t string) *time.Time {
newTime, err := time.Parse("2006-01-02", t)
utils.PanicOnError(err)
throw.OnError(err)
return &newTime
}
@ -26,7 +26,7 @@ func TimestampWithoutTimeZone(t string, precision int) *time.Time {
newTime, err := time.Parse("2006-01-02 15:04:05"+precisionStr+" +0000", t+" +0000")
utils.PanicOnError(err)
throw.OnError(err)
return &newTime
}
@ -35,7 +35,7 @@ func TimestampWithoutTimeZone(t string, precision int) *time.Time {
func TimeWithoutTimeZone(t string) *time.Time {
newTime, err := time.Parse("15:04:05", t)
utils.PanicOnError(err)
throw.OnError(err)
return &newTime
}
@ -44,7 +44,7 @@ func TimeWithoutTimeZone(t string) *time.Time {
func TimeWithTimeZone(t string) *time.Time {
newTimez, err := time.Parse("15:04:05 -0700", t)
utils.PanicOnError(err)
throw.OnError(err)
return &newTimez
}
@ -60,7 +60,7 @@ func TimestampWithTimeZone(t string, precision int) *time.Time {
newTime, err := time.Parse("2006-01-02 15:04:05"+precisionStr+" -0700 MST", t)
utils.PanicOnError(err)
throw.OnError(err)
return &newTime
}

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