entc/gen: currently handle user-defined string ids (#338)

Fixes #127
This commit is contained in:
Ariel Mashraki
2020-02-10 12:04:50 +02:00
committed by GitHub
parent ab71992b7c
commit 9733051cc3
16 changed files with 1270 additions and 9 deletions

View File

@@ -6,7 +6,7 @@ in the LICENSE file in the root directory of this source tree.
{{ define "dialect/sql/predicate/id" -}}
func(s *sql.Selector) {
{{- if $.ID.IsString }}id, _ := strconv.Atoi(id){{- end }}
{{- if and $.ID.IsString (not $.ID.UserDefined) }}id, _ := strconv.Atoi(id){{- end }}
s.Where(sql.EQ(s.C({{ $.ID.Constant }}), id))
}
{{- end }}
@@ -15,6 +15,7 @@ in the LICENSE file in the root directory of this source tree.
{{- $op := $.Scope.Op -}}
{{- $arg := $.Scope.Arg -}}
{{- $storage := $.Scope.Storage -}}
{{- $convid := and $.ID.IsString (not $.ID.UserDefined) -}}
func(s *sql.Selector) {
{{- if $op.Variadic }}
// if not arguments were provided, append the FALSE constants,
@@ -25,9 +26,9 @@ in the LICENSE file in the root directory of this source tree.
}
v := make([]interface{}, len({{ $arg }}))
for i := range v {
{{ if $.ID.IsString }}v[i], _ = strconv.Atoi({{ $arg }}[i]){{ else }}v[i] = {{ $arg }}[i]{{ end }}
{{- if $convid }}v[i], _ = strconv.Atoi({{ $arg }}[i]){{ else }}v[i] = {{ $arg }}[i]{{ end }}
}
{{- else if $.ID.IsString }}
{{- else if $convid }}
id, _ := strconv.Atoi({{ $arg }})
{{- end }}
s.Where(sql.{{ call $storage.OpCode $op }}(s.C({{ $.ID.Constant }}), {{ if $op.Variadic }}v...{{ else }}id{{ end }}))