entc/gen: allow scanning nil valuescanner types directly from database

Fixed https://github.com/ent/ent/issues/2116
This commit is contained in:
Ariel Mashraki
2021-11-07 22:55:02 +02:00
committed by Ariel Mashraki
parent dd6c034c2a
commit e915765f3b
10 changed files with 57 additions and 11 deletions

View File

@@ -23,10 +23,20 @@ func ({{ $receiver }} *{{ $builder }}) sqlSave(ctx context.Context) (*{{ $.Name
if _spec.ID.Value != nil {
{{- /* If the ID type is not a pointer, but implements the ValueScanner interface (e.g. UUID fields). */}}
{{- if and $.ID.Type.ValueScanner (not $.ID.Type.RType.IsPtr) }}
_node.ID = *_spec.ID.Value.(*{{ $.ID.Type }})
if id, ok := _spec.ID.Value.(*{{ $.ID.Type }}); ok {
_node.ID = *id
{{- else }}
_node.ID = _spec.ID.Value.({{ $.ID.Type }})
if id, ok := _spec.ID.Value.({{ $.ID.Type }}); ok {
_node.ID = id
{{- end }}
{{- if $.ID.Type.ValueScanner }}
} else if err := _node.ID.Scan(_spec.ID.Value); err != nil {
return nil, err
{{- else }}
} else {
return nil, fmt.Errorf("unexpected {{ $.Name }}.ID type: %T", _spec.ID.Value)
{{- end }}
}
}
{{- else }}
{{- if $.ID.UserDefined }}