Support optional fields being empty.

This commit is contained in:
mikestefanello 2025-04-12 14:56:38 -04:00
parent 356cf21b3e
commit 56f8a619a5
5 changed files with 46 additions and 27 deletions

View file

@ -8,6 +8,7 @@ import (
"entgo.io/ent/entc"
"entgo.io/ent/entc/gen"
"entgo.io/ent/schema/field"
)
var (
@ -24,8 +25,9 @@ func (*Extension) Templates() []*gen.Template {
gen.MustParse(
gen.NewTemplate("admin").
Funcs(template.FuncMap{
"fieldName": fieldName,
"fieldLabel": fieldLabel,
"fieldName": fieldName,
"fieldLabel": fieldLabel,
"fieldIsPointer": fieldIsPointer,
}).
ParseFS(templateDir, "templates/*tmpl"),
),
@ -58,6 +60,17 @@ func fieldLabel(name string) string {
return upperFirst(out)
}
func fieldIsPointer(f *gen.Field) bool {
switch {
case f.Type.Type == field.TypeBool:
return false
case f.Optional,
f.Default:
return true
}
return false
}
func upperFirst(s string) string {
if len(s) == 0 {
return s