Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 28 additions & 73 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,82 +80,37 @@ func ResolveImportPath(clientDir string) (string, error) {
}

func GenerateClient(sch schema.Schema, pkgName string, parentImportPath string, embedPath string, defaultDiskPath string, defaultLogs []string) (map[string]string, error) {
tmpl := template.New("").Funcs(template.FuncMap{
"capitalize": capitalize,
"lowercase": lowercase,
"fkForRelation": fkForRelation,
"fieldPredType": func(f *schema.ScalarField, parentPkg string) string {
if f.EnumRef != nil {
if f.IsArray {
return "[]" + parentPkg + "." + f.EnumRef.Name + "Type"
}
return parentPkg + "." + f.EnumRef.Name + "Type"
}
t := f.GoType
if f.Optional {
t = strings.TrimPrefix(t, "*")
}
return t
},
"hasLog": func(level string) bool {
for _, l := range defaultLogs {
if l == "all" || l == level {
return true
}
}
return false
},
"hasAnyLog": func() bool {
for _, l := range defaultLogs {
if l != "none" {
return true
}
}
return false
},
"hasJsonField": func(m *schema.Model) bool {
for _, sf := range m.ScalarFields {
if sf.Type == "Json" || strings.Contains(sf.GoType, "json.RawMessage") {
return true
}
}
return false
},
"hasTimeField": func(m *schema.Model) bool {
for _, sf := range m.ScalarFields {
if sf.Type == "DateTime" || strings.Contains(sf.GoType, "time.Time") {
return true
}
}
return false
},
"trimPrefix": strings.TrimPrefix,
"isKnownDefaultFunc": func(funcName string) bool {
switch funcName {
case "autoincrement", "cuid", "uuid", "now":
hasLog := func(level string) bool {
for _, l := range defaultLogs {
if l == "all" || l == level {
return true
}
return false
},
"defaultFuncCall": func(funcName string) string {
switch funcName {
case "cuid":
return "generateCUID()"
case "uuid":
return "generateUUID()"
case "now":
return "time.Now()"
}
return ""
},
"hasStringField": func(m *schema.Model) bool {
for _, sf := range m.ScalarFields {
if sf.GoType == "string" || strings.Contains(sf.GoType, "string") {
return true
}
}
return false
}

hasAnyLog := func() bool {
for _, l := range defaultLogs {
if l != "none" {
return true
}
return false
},
}
return false
}

tmpl := template.New("").Funcs(template.FuncMap{
"capitalize": capitalize,
"lowercase": lowercase,
"fkForRelation": fkForRelation,
"fieldPredType": fieldPredType,
"hasLog": hasLog,
"hasAnyLog": hasAnyLog,
"hasJsonField": hasJsonField,
"hasTimeField": hasTimeField,
"trimPrefix": strings.TrimPrefix,
"isKnownDefaultFunc": isKnownDefaultFunc,
"defaultFuncCall": defaultFuncCall,
"hasStringField": hasStringField,
})
tmpl, err := tmpl.ParseFS(templatesFS, "templates/*.gotpl")
if err != nil {
Expand Down
58 changes: 58 additions & 0 deletions generator/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package generator

import (
"strings"

"github.com/voidclancy/valk/schema"
)

Expand Down Expand Up @@ -36,3 +37,60 @@ func fkForRelation(model *schema.Model, field *schema.ScalarField) string {
}
return ""
}

func fieldPredType(f *schema.ScalarField, parentPkg string) string {
if f.EnumRef != nil {
if f.IsArray {
return "[]" + parentPkg + "." + f.EnumRef.Name + "Type"
}
return parentPkg + "." + f.EnumRef.Name + "Type"
}
t := f.GoType
if f.Optional {
t = strings.TrimPrefix(t, "*")
}
return t
}

func hasJsonField(m *schema.Model) bool {
for _, sf := range m.ScalarFields {
if sf.Type == "Json" || strings.Contains(sf.GoType, "json.RawMessage") {
return true
}
}
return false
}
func hasTimeField(m *schema.Model) bool {
for _, sf := range m.ScalarFields {
if sf.Type == "DateTime" || strings.Contains(sf.GoType, "time.Time") {
return true
}
}
return false
}
func isKnownDefaultFunc(funcName string) bool {
switch funcName {
case "autoincrement", "cuid", "uuid", "now":
return true
}
return false
}
func defaultFuncCall(funcName string) string {
switch funcName {
case "cuid":
return "generateCUID()"
case "uuid":
return "generateUUID()"
case "now":
return "time.Now()"
}
return ""
}
func hasStringField(m *schema.Model) bool {
for _, sf := range m.ScalarFields {
if sf.GoType == "string" || strings.Contains(sf.GoType, "string") {
return true
}
}
return false
}
Loading