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)
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
{{ $builder := pascal $.Scope.Builder }}
|
||||
{{ $receiver := receiver $builder }}
|
||||
{{ $fields := $.Fields }}
|
||||
{{ $updater := false }}
|
||||
{{- if or (hasSuffix $builder "Update") (hasSuffix $builder "UpdateOne") }}
|
||||
{{ $updater := true }}
|
||||
{{ $fields = $.MutableFields }}
|
||||
{{- end }}
|
||||
|
||||
@@ -15,7 +17,7 @@
|
||||
return {{ $receiver }}
|
||||
}
|
||||
{{/* avoid generting nillable setters for slices because the nil value for slice is valid */}}
|
||||
{{ if and (not $f.Type.Slice) (or $f.Optional $f.Nillable $f.HasDefault) }}
|
||||
{{ if and (not $f.Type.Slice) (or $f.Optional $f.Default) (not (and $updater $f.UpdateDefault)) }}
|
||||
{{ $nillableFunc := print "SetNillable" (pascal $f.Name) }}
|
||||
// {{ $nillableFunc }} sets the {{ $f.Name }} field if the given value is not nil.
|
||||
func ({{ $receiver }} *{{ $builder }}) {{ $nillableFunc }}({{ $p }} *{{ $f.Type }}) *{{ $builder }} {
|
||||
|
||||
@@ -33,7 +33,7 @@ func ({{ $receiver}} *{{ $builder }}) Where(ps ...predicate.{{ $.Name }}) *{{ $b
|
||||
// Save executes the query and returns the number of rows/vertices matched by this operation.
|
||||
func ({{ $receiver }} *{{ $builder }}) Save(ctx context.Context) (int, error) {
|
||||
{{ with extend $ "Receiver" $receiver "Package" $pkg "ZeroValue" 0 -}}
|
||||
{{ template "update/validators" . }}
|
||||
{{ template "update/save" . }}
|
||||
{{- end -}}
|
||||
{{- if $multistorage -}}
|
||||
switch {{ $receiver }}.driver.Dialect() {
|
||||
@@ -100,7 +100,7 @@ type {{ $onebuilder }} struct {
|
||||
// Save executes the query and returns the updated entity.
|
||||
func ({{ $receiver }} *{{ $onebuilder }} ) Save(ctx context.Context) (*{{ $.Name }}, error) {
|
||||
{{ with extend $ "Receiver" $receiver "Package" $pkg "ZeroValue" "nil" -}}
|
||||
{{ template "update/validators" . }}
|
||||
{{ template "update/save" . }}
|
||||
{{- end -}}
|
||||
{{- if $multistorage -}}
|
||||
switch {{ $receiver }}.driver.Dialect() {
|
||||
@@ -149,8 +149,10 @@ func ({{ $receiver }} *{{ $onebuilder }}) ExecX(ctx context.Context) {
|
||||
|
||||
{{/* shared struct fields between the two updaters */}}
|
||||
{{ define "update/fields"}}
|
||||
{{ range $_, $f := $.MutableFields }}
|
||||
{{- $f.StructField }} *{{ $f.Type }}
|
||||
{{ range $_, $f := $.Fields }}
|
||||
{{- if or (not $f.Immutable) $f.UpdateDefault }}
|
||||
{{- $f.StructField }} *{{ $f.Type }}
|
||||
{{- end }}
|
||||
{{ end }}
|
||||
{{- range $_, $e := $.Edges }}
|
||||
{{- $e.StructField }} map[{{ $.ID.Type }}]struct{}
|
||||
@@ -203,12 +205,18 @@ func ({{ $receiver }} *{{ $onebuilder }}) ExecX(ctx context.Context) {
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
|
||||
{{/* shared templates for validators. */}}
|
||||
{{ define "update/validators" }}
|
||||
{{/* shared template for the save method of the 2 builders */}}
|
||||
{{ define "update/save" }}
|
||||
{{- $pkg := .Scope.Package -}}
|
||||
{{- $zero := .Scope.ZeroValue }}
|
||||
{{- $receiver := .Scope.Receiver -}}
|
||||
{{- range $_, $f := $.Fields -}}
|
||||
{{- if $f.UpdateDefault -}}
|
||||
if {{ $receiver }}.{{ $f.StructField }} == nil {
|
||||
v := {{ $.Package }}.{{ $f.UpdateDefaultName }}{{ if $f.IsTime }}(){{ end }}
|
||||
{{ $receiver }}.{{ $f.StructField }} = &v
|
||||
}
|
||||
{{ end -}}
|
||||
{{ with $f.Validators -}}
|
||||
if {{ $receiver }}.{{ $f.StructField }} != nil {
|
||||
if err := {{ $.Package }}.{{ $f.Validator }}(*{{ $receiver }}.{{ $f.StructField }}); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user