Files
ent/entc/gen/template/dialect/sql/decode.tmpl
Joel Courtney e4d57a47f7 entc/gen: fix eager loading when using uuid for foreign keys failing if loading more than a single relation (#533)
* Bugfix: eager lodaing when using uuid for foreign keys failing if loading more than a single relation (#532)

* go generate ./... output
2020-06-09 08:10:14 +03:00

122 lines
4.2 KiB
Cheetah

{{/*
Copyright 2019-present Facebook Inc. All rights reserved.
This source code is licensed under the Apache 2.0 license found
in the LICENSE file in the root directory of this source tree.
*/}}
{{ define "dialect/sql/decode/one" }}
{{ $receiver := $.Receiver }}
// scanValues returns the types for scanning values from sql.Rows.
func (*{{ $.Name }}) scanValues() []interface{} {
return []interface{} {
&{{ if not $.ID.UserDefined }}sql.NullInt64{{ else }}{{ $.ID.NullType }}{{ end }}{}, // {{ $.ID.Name }}
{{- range $_, $f := $.Fields }}
&{{ $f.NullType }}{}, // {{ $f.Name }}
{{- end }}
}
}
{{- with $.ForeignKeys }}
// fkValues returns the types for scanning foreign-keys values from sql.Rows.
func (*{{ $.Name }}) fkValues() []interface{} {
return []interface{} {
{{- range $fk := . }}
{{- $f := $fk.Field }}
&{{ if not $f.UserDefined }}sql.NullInt64{{ else }}{{ $f.NullType }}{{ end }}{}, // {{ $f.Name }}
{{- end }}
}
}
{{- end }}
// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the {{ $.Name }} fields.
func ({{ $receiver }} *{{ $.Name }}) assignValues(values ...interface{}) error {
if m, n := len(values), len({{ $.Package }}.Columns); m < n {
return fmt.Errorf("mismatch number of scan values: %d != %d", m, n)
}
{{- if and $.ID.UserDefined (or $.ID.IsString $.ID.IsUUID) }}
{{- with extend $ "Idx" 0 "Field" $.ID "Rec" $receiver }}
{{ template "dialect/sql/decode/field" . }}
{{- end }}
{{- else }}
value, ok := values[0].(*sql.NullInt64)
if !ok {
return fmt.Errorf("unexpected type %T for field id", value)
}
{{ $receiver }}.ID = {{ $.ID.Type }}(value.Int64)
{{- end }}
values = values[1:]
{{- range $i, $f := $.Fields }}
{{- with extend $ "Idx" $i "Field" $f "Rec" $receiver }}
{{ template "dialect/sql/decode/field" . }}
{{- end }}
{{- end }}
{{- with $.ForeignKeys }}
values = values[{{ len $.Fields }}:]
if len(values) == len({{ $.Package }}.ForeignKeys) {
{{- range $i, $fk := . }}
{{- $f := $fk.Field }}
{{- if and $f.UserDefined (or $f.IsString $f.IsUUID) }}
{{- with extend $ "Idx" $i "Field" $f "Rec" $receiver "StructField" $f.Name }}
{{ template "dialect/sql/decode/field" . }}
{{- end }}
{{- else }}
if value, ok := values[{{ $i }}].(*sql.NullInt64); !ok {
return fmt.Errorf("unexpected type %T for edge-field {{ $f.Name}}", value)
} else if value.Valid {
{{ $receiver }}.{{ $f.Name }} = new({{ $f.Type }})
*{{ $receiver }}.{{ $f.Name }} = {{ $f.Type }}(value.Int64)
}
{{- end }}
{{- end }}
}
{{- end }}
return nil
}
{{ end }}
{{ define "dialect/sql/decode/field" }}
{{- $i := $.Scope.Idx -}}
{{- $f := $.Scope.Field -}}
{{- $ret := $.Scope.Rec -}}
{{- $field := $f.StructField }}{{ with $.Scope.StructField }}{{ $field = . }}{{ end }}
{{- if $f.IsJSON }}
if value, ok := values[{{ $i }}].(*{{ $f.NullType }}); !ok {
return fmt.Errorf("unexpected type %T for field {{ $f.Name }}", values[{{ $i }}])
} else if value != nil && len(*value) > 0 {
if err := json.Unmarshal(*value, &{{ $ret }}.{{ $field }}); err != nil {
return fmt.Errorf("unmarshal field {{ $f.Name }}: %v", err)
}
}
{{- else }}
{{- $nulltype := $f.NullType -}}
if value, ok := values[{{ $i }}].(*{{ $nulltype }}); !ok {
return fmt.Errorf("unexpected type %T for field {{ $f.Name }}", values[{{ $i }}])
{{- if and (not $f.Type.ValueScanner) (hasPrefix $nulltype "sql") }}
} else if value.Valid {
{{- if $f.Nillable }}
{{ $ret }}.{{ $field }} = new({{ $f.Type }})
*{{ $ret }}.{{ $field }} = {{ $f.NullTypeField "value" }}
{{- else }}
{{ $ret }}.{{ $field }} = {{ $f.NullTypeField "value" }}
{{- end }}
{{- else }}
} else if value != nil {
{{ $ret }}.{{ $field }} = {{ if not $f.Nillable }}*{{ end }}value
{{- end }}
}
{{- end }}
{{- end }}
{{ define "dialect/sql/decode/many" }}
{{ end }}
{{/* Additional fields for the generated model for holding the foreign-keys */}}
{{ define "dialect/sql/model/fields" }}
{{- range $fk := $.ForeignKeys }}
{{- $f := $fk.Field }}
{{ $f.Name }} {{ if $f.Nillable }}*{{ end }}{{ $f.Type }}
{{- end }}
{{ end }}