Skip to content

Commit

Permalink
entc/gen: allow adding extra fields to the generated edges
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m committed Mar 29, 2022
1 parent 317594e commit b983f65
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
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
19 changes: 18 additions & 1 deletion entc/gen/template/ent.tmpl
Expand Up @@ -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 }}

0 comments on commit b983f65

Please sign in to comment.