Skip to content

Commit

Permalink
entc/gen: allow adding extra fields to the generated edges (ent#2437)
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m authored and gitlawr committed Apr 13, 2022
1 parent 2e36c65 commit fb6e685
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
3 changes: 3 additions & 0 deletions entc/gen/template.go
Expand Up @@ -182,6 +182,8 @@ var (
"dialect/sql/create/additional/*",
"dialect/sql/create_bulk/additional/*",
"dialect/sql/model/additional/*",
"dialect/sql/model/edges/*",
"dialect/sql/model/edges/fields/additional/*",
"dialect/sql/model/fields/*",
"dialect/sql/select/additional/*",
"dialect/sql/predicate/edge/*/*",
Expand All @@ -191,6 +193,7 @@ var (
"import/additional/*",
"model/additional/*",
"model/comment/additional/*",
"model/edges/fields/additional/*",
"update/additional/*",
"query/additional/*",
}
Expand Down
21 changes: 19 additions & 2 deletions entc/gen/template/ent.tmpl
Expand Up @@ -50,7 +50,7 @@ type {{ $.Name }} struct {
{{- xtemplate $tmpl . }}
{{- end }}
{{- /* Additional fields to add by the user. */}}
{{ template "model/fields/additional" $ }}
{{- template "model/fields/additional" $ }}
}

{{- with $.Edges }}
Expand All @@ -63,6 +63,8 @@ type {{ $.Name }}Edges struct {
// loadedTypes holds the information for reporting if a
// type was loaded (or requested) in eager-loading or not.
loadedTypes [{{ len . }}]bool
{{- /* Additional fields to add by the user. */}}
{{- template "model/edges/fields/additional" $ }}
}

{{- range $i, $e := . }}
Expand Down Expand Up @@ -206,7 +208,7 @@ func ({{ $receiver }} {{ $slice }}) config(cfg config) {
{{- end }}
{{- end }}

{{/* A template for adding additional methods or helpers for the generated model.*/}}
{{/* A template for adding additional methods or helpers for the generated model. */}}
{{ define "model/additional" }}
{{- with $tmpls := matchTemplate "model/additional/*" }}
{{- range $tmpl := $tmpls }}
Expand All @@ -219,4 +221,19 @@ func ({{ $receiver }} {{ $slice }}) config(cfg config) {
{{- xtemplate $tmpl $ }}
{{- end }}
{{- end }}
{{ end }}

{{/* A template for adding additional fields for the Edges object of the generated model. */}}
{{ define "model/edges/fields/additional" }}
{{- with $tmpls := matchTemplate "model/edges/fields/additional/*" }}
{{- range $tmpl := $tmpls }}
{{- xtemplate $tmpl $ }}
{{- end }}
{{- end }}

{{- with $tmpls := matchTemplate (printf "dialect/%s/model/edges/fields/additional/*" $.Storage) }}
{{- range $tmpl := $tmpls }}
{{- xtemplate $tmpl $ }}
{{- end }}
{{- end }}
{{ end }}
1 change: 0 additions & 1 deletion entc/integration/gremlin/ent/card.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion examples/entcpkg/ent/user.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fb6e685

Please sign in to comment.