mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
ent/field: add update_default option time field
Reviewed By: alexsn Differential Revision: D17070907 fbshipit-source-id: 63c9ce75c58e524044c38f9461cb04e8e45c8017
This commit is contained in:
committed by
Facebook Github Bot
parent
bd07c86b60
commit
772b8a33f8
@@ -26,9 +26,9 @@ type {{ $builder }} struct {
|
||||
// Save creates the {{ $.Name }} in the database.
|
||||
func ({{ $receiver }} *{{ $builder }}) Save(ctx context.Context) (*{{ $.Name }}, error) {
|
||||
{{ range $_, $f := $.Fields -}}
|
||||
{{- if or $f.HasDefault (not $f.Optional) -}}
|
||||
{{- if or $f.Default (not $f.Optional) -}}
|
||||
if {{ $receiver }}.{{ $f.StructField }} == nil {
|
||||
{{ if $f.HasDefault -}}
|
||||
{{ if $f.Default -}}
|
||||
v := {{ $.Package }}.{{ $f.DefaultName }}{{ if $f.IsTime }}(){{ end }}
|
||||
{{ $receiver }}.{{ $f.StructField }} = &v
|
||||
{{ else -}}
|
||||
@@ -38,7 +38,7 @@ func ({{ $receiver }} *{{ $builder }}) Save(ctx context.Context) (*{{ $.Name }},
|
||||
{{ end -}}
|
||||
{{ with $f.Validators -}}
|
||||
{{/* add nullable check only for optional fields without default value */ -}}
|
||||
{{ $nullable := and $f.Optional (not $f.HasDefault) -}}
|
||||
{{ $nullable := and $f.Optional (not $f.Default) -}}
|
||||
{{- if $nullable }} if {{ $receiver }}.{{ $f.StructField }} != nil { {{ end -}}
|
||||
if err := {{ $.Package }}.{{ $f.Validator }}(*{{ $receiver }}.{{ $f.StructField }}); err != nil {
|
||||
return nil, fmt.Errorf("{{ $pkg }}: validator failed for field \"{{ $f.Name }}\": %v", err)
|
||||
|
||||
Reference in New Issue
Block a user