entc/gen: expose edge mutation setters for external templates (#1032)

This commit is contained in:
Ariel Mashraki
2020-12-12 00:01:30 +02:00
committed by GitHub
parent 9d38e68484
commit 70f2b1355a
3 changed files with 13 additions and 4 deletions

File diff suppressed because one or more lines are too long

View File

@@ -254,8 +254,7 @@ func (m *{{ $mutation }}) ID() (id {{ $n.ID.Type }}, exists bool) {
{{ range $e := $n.Edges }}
{{ $op := "add" }}{{ if $e.Unique }}{{ $op = "set" }}{{ end }}
{{ $idsFunc := print (pascal $op) (singular $e.Name | pascal) "IDs" }}{{ if $e.Unique }}{{ $idsFunc = print (pascal $op) (pascal $e.Name) "ID" }}{{ end }}
{{ $op := "add" }}{{ $idsFunc := $e.MutationAdd }}{{ if $e.Unique }}{{ $op = "set" }}{{ $idsFunc = $e.MutationSet }}{{ end }}
// {{ $idsFunc }} {{ $op }}s the {{ $e.Name }} edge to {{ $e.Type.Name }} by id{{ if not $e.Unique }}s{{ end }}.
func (m *{{ $mutation }}) {{ $idsFunc }}({{ if $e.Unique }}id{{ else }}ids ...{{ end }} {{ $e.Type.ID.Type }}) {
{{- if $e.Unique }}

View File

@@ -1148,6 +1148,16 @@ func (e Edge) OwnFK() bool {
return false
}
// MutationSet returns the method name for setting the edge id.
func (e Edge) MutationSet() string {
return "Set" + pascal(e.Name) + "ID"
}
// MutationAdd returns the method name for adding edge ids.
func (e Edge) MutationAdd() string {
return "Add" + pascal(rules.Singularize(e.Name)) + "IDs"
}
// MutationReset returns the method name for resetting the edge value.
// The default name is "Reset<EdgeName>". If the the method conflicts
// with the mutation methods, suffix the method with "Edge".