entc/gen: move multistorage logic to Go code

Summary: Pull Request resolved: https://github.com/facebookincubator/ent/pull/94

Reviewed By: alexsn

Differential Revision: D17926186

fbshipit-source-id: b59dc418703bc4faca5230a7354edea1423b7d35
This commit is contained in:
Ariel Mashraki
2019-10-15 06:41:29 -07:00
committed by Facebook Github Bot
parent 36a3971f3d
commit 0c46ce4e5e
10 changed files with 49 additions and 47 deletions

View File

@@ -417,6 +417,11 @@ func (g *Graph) templates() (*template.Template, []GraphTemplate) {
return templates, external
}
// MultiStorage reports whether c has more than 1 storage driver.
func (c *Config) MultiStorage() bool {
return len(c.Storage) > 1
}
// formatFiles runs "goimports" on given paths.
func formatFiles(paths []string) error {
for _, path := range paths {

File diff suppressed because one or more lines are too long

View File

@@ -12,9 +12,9 @@ in the LICENSE file in the root directory of this source tree.
{{ template "import" $ }}
// Order applies an ordering on either graph traversal or sql selector.
type Order func({{ if gt (len $.Storage) 1 }}interface{}{{ else }}{{ (index $.Storage 0).Builder }}{{ end }})
type Order func({{ if $.MultiStorage }}interface{}{{ else }}{{ (index $.Storage 0).Builder }}{{ end }})
{{ if gt (len $.Storage) 1 }}
{{ if $.MultiStorage }}
// OrderPerDialect construct the "order by" clause for graph traversals based on dialect type.
func OrderPerDialect({{ range $i, $storage := $.Storage }}{{ if $i }},{{ end }}f{{ $i }} func({{ $storage.Builder }}){{ end }}) Order {
return Order(func (v interface{}) {
@@ -34,7 +34,7 @@ func OrderPerDialect({{ range $i, $storage := $.Storage }}{{ if $i }},{{ end }}f
{{ $f = pascal $f }}
// {{ $f }} applies the given fields in {{ upper $f }} order.
func {{ $f }}(fields ...string) Order {
return Order{{ if gt (len $.Storage) 1 }}PerDialect{{ end }}(
return Order{{ if $.MultiStorage }}PerDialect{{ end }}(
{{ range $_, $storage := $.Storage -}}
{{- with extend (index $.Nodes 0) "Func" $f "Order" $order -}}
{{ $tmpl := printf "dialect/%s/order" $storage }}

View File

@@ -64,7 +64,7 @@ func ({{ $receiver }} *{{ $builder }}) Save(ctx context.Context) (*{{ $.Name }},
}
{{ end -}}
{{ end -}}
{{- if gt (len $.Storage) 1 -}}
{{- if $.MultiStorage -}}
switch {{ $receiver }}.driver.Dialect() {
{{- range $_, $storage := $.Storage }}
case {{ join $storage.Dialects ", " }}:

View File

@@ -28,7 +28,7 @@ func ({{ $receiver}} *{{ $builder }}) Where(ps ...predicate.{{ $.Name }}) *{{ $b
// Exec executes the deletion query and returns how many vertices were deleted.
func ({{ $receiver}} *{{ $builder }}) Exec(ctx context.Context) (int, error) {
{{- if gt (len $.Storage) 1 -}}
{{- if $.MultiStorage -}}
switch {{ $receiver }}.driver.Dialect() {
{{- range $_, $storage := $.Storage }}
case {{ join $storage.Dialects ", " }}:

View File

@@ -13,7 +13,6 @@ in the LICENSE file in the root directory of this source tree.
{{ $builder := print (pascal $.Name) "Query" }}
{{ $receiver := receiver $builder }}
{{ $multistorage := gt (len $.Storage) 1 }}
// {{ $builder }} is the builder for querying {{ pascal $.Name }} entities.
type {{ $builder }} struct {
@@ -59,7 +58,7 @@ func ({{ $receiver }} *{{ $builder }}) Order(o ...Order) *{{ $builder }} {
// Query{{ pascal $e.Name }} chains the current query on the {{ $e.Name }} edge.
func ({{ $receiver }} *{{ $builder }}) Query{{ pascal $e.Name }}() *{{ $edge_builder }} {
query := &{{ $edge_builder }}{config: {{ $receiver }}.config}
{{- if $multistorage }}
{{- if $.MultiStorage }}
switch {{ $receiver }}.driver.Dialect() {
{{- range $_, $storage := $.Storage -}}
case {{ join $storage.Dialects ", " }}:
@@ -175,7 +174,7 @@ func ({{ $receiver }} *{{ $builder }}) OnlyXID(ctx context.Context) {{ $.ID.Type
// All executes the query and returns a list of {{ plural $.Name }}.
func ({{ $receiver }} *{{ $builder }}) All(ctx context.Context) ([]*{{ $.Name }}, error) {
{{- if $multistorage }}
{{- if $.MultiStorage }}
switch {{ $receiver }}.driver.Dialect() {
{{- range $_, $storage := $.Storage }}
case {{ join $storage.Dialects ", " }}:
@@ -200,7 +199,6 @@ func ({{ $receiver }} *{{ $builder }}) AllX(ctx context.Context) []*{{ $.Name }}
// IDs executes the query and returns a list of {{ $.Name }} ids.
func ({{ $receiver }} *{{ $builder }}) IDs(ctx context.Context) ([]{{ $.ID.Type }}, error) {
var ids []{{ $.ID.Type }}
if err := {{ $receiver }}.Select({{ $.Package }}.FieldID).Scan(ctx, &ids); err != nil {
return nil, err
}
@@ -218,7 +216,7 @@ func ({{ $receiver }} *{{ $builder }}) IDsX(ctx context.Context) []{{ $.ID.Type
// Count returns the count of the given query.
func ({{ $receiver }} *{{ $builder }}) Count(ctx context.Context) (int, error) {
{{- if $multistorage }}
{{- if $.MultiStorage }}
switch {{ $receiver }}.driver.Dialect() {
{{- range $_, $storage := $.Storage }}
case {{ join $storage.Dialects ", " }}:
@@ -243,7 +241,7 @@ func ({{ $receiver }} *{{ $builder }}) CountX(ctx context.Context) int {
// Exist returns true if the query has elements in the graph.
func ({{ $receiver }} *{{ $builder }}) Exist(ctx context.Context) (bool, error) {
{{- if $multistorage }}
{{- if $.MultiStorage }}
switch {{ $receiver }}.driver.Dialect() {
{{- range $_, $storage := $.Storage }}
case {{ join $storage.Dialects ", " }}:
@@ -306,7 +304,7 @@ func ({{ $receiver }} *{{ $builder }}) Clone() *{{ $builder }} {
func ({{ $receiver }} *{{ $builder }}) GroupBy(field string, fields ...string) *{{ $groupBuilder }} {
group := &{{ $groupBuilder }}{config: {{ $receiver }}.config}
group.fields = append([]string{field}, fields...)
{{- if $multistorage }}
{{- if $.MultiStorage }}
switch {{ $receiver }}.driver.Dialect() {
{{- range $_, $storage := $.Storage }}
case {{ join $storage.Dialects ", " }}:
@@ -339,7 +337,7 @@ func ({{ $receiver }} *{{ $builder }}) GroupBy(field string, fields ...string) *
func ({{ $receiver }} *{{ $builder }}) Select(field string, fields ...string) *{{ $selectBuilder }} {
selector := &{{ $selectBuilder }}{config: {{ $receiver }}.config}
selector.fields = append([]string{field}, fields...)
{{- if $multistorage }}
{{- if $.MultiStorage }}
switch {{ $receiver }}.driver.Dialect() {
{{- range $_, $storage := $.Storage }}
case {{ join $storage.Dialects ", " }}:
@@ -382,7 +380,7 @@ func ({{ $groupReceiver }} *{{ $groupBuilder }}) Aggregate(fns ...Aggregate) *{{
// Scan applies the group-by query and scan the result into the given value.
func ({{ $groupReceiver }} *{{ $groupBuilder }}) Scan(ctx context.Context, v interface{}) error {
{{- if $multistorage }}
{{- if $.MultiStorage }}
switch {{ $groupReceiver }}.driver.Dialect() {
{{- range $_, $storage := $.Storage }}
case {{ join $storage.Dialects ", " }}:
@@ -451,7 +449,7 @@ type {{ $selectBuilder }} struct {
// Scan applies the selector query and scan the result into the given value.
func ({{ $selectReceiver }} *{{ $selectBuilder }}) Scan(ctx context.Context, v interface{}) error {
{{- if $multistorage }}
{{- if $.MultiStorage }}
switch {{ $selectReceiver }}.driver.Dialect() {
{{- range $_, $storage := $.Storage }}
case {{ join $storage.Dialects ", " }}:

View File

@@ -13,7 +13,6 @@ in the LICENSE file in the root directory of this source tree.
{{ $builder := print (pascal $.Name) "Update" }}
{{ $receiver := receiver $builder }}
{{ $multistorage := gt (len $.Storage) 1 }}
// {{ $builder }} is the builder for updating {{ $.Name }} entities.
type {{ $builder }} struct {
@@ -41,7 +40,7 @@ func ({{ $receiver }} *{{ $builder }}) Save(ctx context.Context) (int, error) {
{{ with extend $ "Receiver" $receiver "Package" $pkg "ZeroValue" 0 -}}
{{ template "update/save" . }}
{{- end -}}
{{- if $multistorage -}}
{{- if $.MultiStorage -}}
switch {{ $receiver }}.driver.Dialect() {
{{- range $_, $storage := $.Storage }}
case {{ join $storage.Dialects ", " }}:
@@ -108,7 +107,7 @@ func ({{ $receiver }} *{{ $onebuilder }} ) Save(ctx context.Context) (*{{ $.Name
{{ with extend $ "Receiver" $receiver "Package" $pkg "ZeroValue" "nil" -}}
{{ template "update/save" . }}
{{- end -}}
{{- if $multistorage -}}
{{- if $.MultiStorage -}}
switch {{ $receiver }}.driver.Dialect() {
{{- range $_, $storage := $.Storage }}
case {{ join $storage.Dialects ", " }}:

View File

@@ -181,7 +181,7 @@ func (c *{{ $client }}) GetX(ctx context.Context, id {{ $n.ID.Type }}) *{{ $n.Na
// Query{{ pascal $e.Name }} queries the {{ $e.Name }} edge of a {{ $n.Name }}.
func (c *{{ $client }}) Query{{ pascal $e.Name }}({{ $rec }} *{{ $n.Name }}) *{{ $builder }} {
query := &{{ $e.Type.Name }}Query{config: c.config}
{{ if gt (len $.Storage) 1 -}}
{{ if $.MultiStorage -}}
switch c.driver.Dialect() {
{{- range $_, $storage := $.Storage }}
case {{ join $storage.Dialects ", " }}:

View File

@@ -19,9 +19,9 @@ import (
{{ range $_, $n := $.Nodes -}}
// {{ $n.Name }} is the predicate function for {{ $n.Package }} builders.
type {{ $n.Name }} func({{ if gt (len $.Storage) 1 }}interface{}{{ else }}{{ (index $.Storage 0).Builder }}{{ end }})
type {{ $n.Name }} func({{ if $.MultiStorage }}interface{}{{ else }}{{ (index $.Storage 0).Builder }}{{ end }})
{{ if gt (len $.Storage) 1 }}
{{ if $.MultiStorage }}
// {{ $n.Name }}PerDialect construct a predicate for graph traversals based on dialect type.
func {{ $n.Name }}PerDialect({{ range $i, $storage := $.Storage }}{{ if $i }},{{ end }}f{{ $i }} func({{ $storage.Builder }}){{ end }}) {{ $n.Name }} {
return {{ $n.Name }}(func (v interface{}) {

View File

@@ -14,7 +14,7 @@ in the LICENSE file in the root directory of this source tree.
// ID filters vertices based on their identifier.
func ID(id {{ $.ID.Type }}) predicate.{{ $.Name }} {
return predicate.{{ $.Name }}{{ if gt (len $.Storage) 1 }}PerDialect{{ end }}(
return predicate.{{ $.Name }}{{ if $.MultiStorage }}PerDialect{{ end }}(
{{ range $_, $storage := $.Storage -}}
{{ $tmpl := printf "dialect/%s/predicate/id" $storage }}
{{- xtemplate $tmpl $ }},
@@ -27,7 +27,7 @@ func ID(id {{ $.ID.Type }}) predicate.{{ $.Name }} {
{{ $func := printf "ID%s" $op.Name }}
// {{ $func }} applies the {{ $op.Name }} predicate on the ID field.
func {{ $func }}({{ $arg }} {{ if $op.Variadic }}...{{ end }}{{ $.ID.Type }}) predicate.{{ $.Name }} {
return predicate.{{ $.Name }}{{ if gt (len $.Storage) 1 }}PerDialect{{ end }}(
return predicate.{{ $.Name }}{{ if $.MultiStorage }}PerDialect{{ end }}(
{{ range $_, $storage := $.Storage -}}
{{- with extend $ "Arg" $arg "Op" $op "Storage" $storage -}}
{{ $tmpl := printf "dialect/%s/predicate/id/ops" $storage }}
@@ -44,7 +44,7 @@ func ID(id {{ $.ID.Type }}) predicate.{{ $.Name }} {
{{ $func := $f.StructField }}
// {{ $func }} applies equality check predicate on the {{ quote $f.Name }} field. It's identical to {{ $func }}EQ.
func {{ $func }}(v {{ $f.Type }}) predicate.{{ $.Name }} {
return predicate.{{ $.Name }}{{ if gt (len $.Storage) 1 }}PerDialect{{ end }}(
return predicate.{{ $.Name }}{{ if $.MultiStorage }}PerDialect{{ end }}(
{{ range $_, $storage := $.Storage -}}
{{- with extend $ "Field" $f -}}
{{ $tmpl := printf "dialect/%s/predicate/field" $storage }}
@@ -59,7 +59,7 @@ func ID(id {{ $.ID.Type }}) predicate.{{ $.Name }} {
{{ range $_, $f := $.Fields }}
{{ $ops := ops $f }}
{{/* storage specific predicates disabled for multi-storage codegen */}}
{{ if eq (len $.Storage) 1 }}
{{ if not $.MultiStorage }}
{{ $storage := index $.Storage 0 }}
{{ $ops = append $ops (call $storage.Ops $f) }}
{{ end }}
@@ -75,7 +75,7 @@ func ID(id {{ $.ID.Type }}) predicate.{{ $.Name }} {
v[i] = {{ $arg }}[i]
}
{{- end }}
return predicate.{{ $.Name }}{{ if gt (len $.Storage) 1 }}PerDialect{{ end }}(
return predicate.{{ $.Name }}{{ if $.MultiStorage }}PerDialect{{ end }}(
{{ range $_, $storage := $.Storage -}}
{{- with extend $ "Arg" $arg "Field" $f "Op" $op "Storage" $storage -}}
{{ $tmpl := printf "dialect/%s/predicate/field/ops" $storage }}
@@ -91,7 +91,7 @@ func ID(id {{ $.ID.Type }}) predicate.{{ $.Name }} {
{{ $func := print "Has" $e.StructField }}
// {{ $func }} applies the HasEdge predicate on the {{ quote $e.Name }} edge.
func {{ $func }}() predicate.{{ $.Name }} {
return predicate.{{ $.Name }}{{ if gt (len $.Storage) 1 }}PerDialect{{ end }}(
return predicate.{{ $.Name }}{{ if $.MultiStorage }}PerDialect{{ end }}(
{{ range $_, $storage := $.Storage -}}
{{- with extend $ "Edge" $e -}}
{{ $tmpl := printf "dialect/%s/predicate/edge/has" $storage }}
@@ -103,7 +103,7 @@ func ID(id {{ $.ID.Type }}) predicate.{{ $.Name }} {
{{ $func = printf "%sWith" $func }}
// {{ $func }} applies the HasEdge predicate on the {{ quote $e.Name }} edge with a given conditions (other predicates).
func {{ $func }}(preds ...predicate.{{ $e.Type.Name }}) predicate.{{ $.Name }} {
return predicate.{{ $.Name }}{{ if gt (len $.Storage) 1 }}PerDialect{{ end }}(
return predicate.{{ $.Name }}{{ if $.MultiStorage }}PerDialect{{ end }}(
{{ range $_, $storage := $.Storage -}}
{{- with extend $ "Edge" $e -}}
{{ $tmpl := printf "dialect/%s/predicate/edge/haswith" $storage }}
@@ -116,7 +116,7 @@ func ID(id {{ $.ID.Type }}) predicate.{{ $.Name }} {
// And groups list of predicates with the AND operator between them.
func And(predicates ...predicate.{{ $.Name }}) predicate.{{ $.Name }} {
return predicate.{{ $.Name }}{{ if gt (len $.Storage) 1 }}PerDialect{{ end }}(
return predicate.{{ $.Name }}{{ if $.MultiStorage }}PerDialect{{ end }}(
{{ range $_, $storage := $.Storage -}}
{{ $tmpl := printf "dialect/%s/predicate/and" $storage }}
{{- xtemplate $tmpl . }},
@@ -126,7 +126,7 @@ func And(predicates ...predicate.{{ $.Name }}) predicate.{{ $.Name }} {
// Or groups list of predicates with the OR operator between them.
func Or(predicates ...predicate.{{ $.Name }}) predicate.{{ $.Name }} {
return predicate.{{ $.Name }}{{ if gt (len $.Storage) 1 }}PerDialect{{ end }}(
return predicate.{{ $.Name }}{{ if $.MultiStorage }}PerDialect{{ end }}(
{{ range $_, $storage := $.Storage -}}
{{ $tmpl := printf "dialect/%s/predicate/or" $storage }}
{{- xtemplate $tmpl . }},
@@ -136,7 +136,7 @@ func Or(predicates ...predicate.{{ $.Name }}) predicate.{{ $.Name }} {
// Not applies the not operator on the given predicate.
func Not(p predicate.{{ $.Name }}) predicate.{{ $.Name }} {
return predicate.{{ $.Name }}{{ if gt (len $.Storage) 1 }}PerDialect{{ end }}(
return predicate.{{ $.Name }}{{ if $.MultiStorage }}PerDialect{{ end }}(
{{ range $_, $storage := $.Storage -}}
{{ $tmpl := printf "dialect/%s/predicate/not" $storage }}
{{- xtemplate $tmpl . }},