Created
May 12, 2022 23:32
-
-
Save manakuro/2b4e1a28521615ca365a2ac2d0cd6d97 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{ define "mutation_input" }} | |
{{- /*gotype: entgo.io/ent/entc/gen.Graph*/ -}} | |
{{ $pkg := base $.Config.Package }} | |
{{- with extend $ "Package" $pkg }} | |
{{ template "header" . }} | |
{{- end }} | |
{{ template "import" $ }} | |
{{- range $n := $.Nodes }} | |
{{ $input := print "Create" $n.Name "Input" }} | |
// {{ $input }} represents a mutation input for creating {{ plural $n.Name | lower }}. | |
type {{ $input }} struct { | |
{{- range $f := $n.Fields }} | |
{{- if not $f.IsEdgeField }} | |
{{ $f.StructField }} {{ if and (or $f.Optional $f.Default) (not $f.Type.RType.IsPtr) }}*{{ end }}{{ $f.Type }} | |
{{- end }} | |
{{- end }} | |
{{- range $e := $n.Edges }} | |
{{- if $e.Unique }} | |
{{- $structField := print (pascal $e.Annotations.Edge.FieldName) }} | |
{{ $structField }} {{ if $e.Optional }}*{{ end }}{{ $e.Type.ID.Type }} | |
{{- else }} | |
{{- $structField := print (singular $e.Name | pascal) "IDs" }} | |
{{ $structField }} []{{ $e.Type.ID.Type }} | |
{{- end }} | |
{{- end }} | |
{{- if $n.Annotations.MutationInput }} | |
{{- range $i := $n.Annotations.MutationInput.Create }} | |
{{ $i.Key }} {{ $i.Type }} | |
{{- end }} | |
{{- end }} | |
RequestID string | |
} | |
// Mutate applies the {{ $input }} on the {{ $n.CreateName }} builder. | |
func (i *{{ $input }}) Mutate(m *{{ $n.CreateName }}) { | |
{{- range $f := $n.Fields }} | |
{{- if not $f.IsEdgeField }} | |
{{- if or $f.Optional $f.Default }} | |
if v := i.{{ $f.StructField }}; v != nil { | |
m.{{ $f.MutationSet }}(*v) | |
} | |
{{- else }} | |
m.{{ $f.MutationSet }}(i.{{ $f.StructField }}) | |
{{- end }} | |
{{- end }} | |
{{- end }} | |
{{- range $e := $n.Edges }} | |
{{- if $e.Unique }} | |
{{- $structField := print (pascal $e.Annotations.Edge.FieldName) }} | |
{{- if $e.Optional }} | |
if v := i.{{ $structField }}; v != nil { | |
m.{{ $e.MutationSet }}(*v) | |
} | |
{{- else }} | |
m.{{ $e.MutationSet }}(i.{{ $structField }}) | |
{{- end }} | |
{{- else }} | |
{{- $structField := print (singular $e.Name | pascal) "IDs" }} | |
if ids := i.{{ $structField }}; len(ids) > 0 { | |
m.{{ $e.MutationAdd }}(ids...) | |
} | |
{{- end }} | |
{{- end }} | |
} | |
// SetInput applies the change-set in the {{ $input }} on the create builder. | |
func(c *{{ $n.CreateName }}) SetInput(i {{ $input }}) *{{ $n.CreateName }} { | |
i.Mutate(c) | |
return c | |
} | |
{{ $input = print "Update" $n.Name "Input" }} | |
// {{ $input }} represents a mutation input for updating {{ plural $n.Name | lower }}. | |
type {{ $input }} struct { | |
{{ print "ID" }} {{ $n.ID.Type }} | |
{{- range $f := $n.MutableFields }} | |
{{- if not $f.IsEdgeField }} | |
{{ $f.StructField }} {{ if and (not $f.Type.RType.IsPtr) (not $f.Annotations.MutationInput.SkipPtr) }}*{{ end }}{{ $f.Type }} | |
{{- if $f.Optional }} | |
{{ print "Clear" $f.StructField }} bool | |
{{- end }} | |
{{- end }} | |
{{- end }} | |
{{- range $e := $n.Edges }} | |
{{- if $e.Unique }} | |
{{- $structField := print (pascal $e.Annotations.Edge.FieldName) }} | |
{{ $structField }} *{{ $e.Type.ID.Type }} | |
{{ $e.MutationClear }} bool | |
{{- else }} | |
{{ $e.MutationAdd }} []{{ $e.Type.ID.Type }} | |
{{ $e.MutationRemove }} []{{ $e.Type.ID.Type }} | |
{{- end }} | |
{{- end }} | |
{{- if $n.Annotations.MutationInput }} | |
{{- range $i := $n.Annotations.MutationInput.Update }} | |
{{ $i.Key }} {{ $i.Type }} | |
{{- end }} | |
{{- end }} | |
RequestID string | |
} | |
// Mutate applies the {{ $input }} on the {{ $n.MutationName }}. | |
func (i *{{ $input }}) Mutate(m *{{ $n.MutationName }}) { | |
{{- range $f := $n.MutableFields }} | |
{{- if not $f.IsEdgeField }} | |
{{- if $f.Optional }} | |
if i.{{ print "Clear" $f.StructField }} { | |
m.{{ print "Clear" $f.StructField }}() | |
} | |
{{- end }} | |
if v := i.{{ $f.StructField }}; v != nil { | |
m.{{ $f.MutationSet }}({{ if and (not $f.Type.RType.IsPtr) (not $f.Annotations.MutationInput.SkipPtr) }}*{{ end }}v) | |
} | |
{{- end }} | |
{{- end }} | |
{{- range $e := $n.Edges }} | |
{{- if $e.Unique }} | |
if i.{{ $e.MutationClear }} { | |
m.{{ $e.MutationClear }}() | |
} | |
{{- $structField := print (pascal $e.Annotations.Edge.FieldName) }} | |
if v := i.{{ $structField }}; v != nil { | |
m.{{ $e.MutationSet }}(*v) | |
} | |
{{- else }} | |
if ids := i.{{ $e.MutationAdd }}; len(ids) > 0 { | |
m.{{ $e.MutationAdd }}(ids...) | |
} | |
if ids := i.{{ $e.MutationRemove }}; len(ids) > 0 { | |
m.{{ $e.MutationRemove }}(ids...) | |
} | |
{{- end }} | |
{{- end }} | |
} | |
// SetInput applies the change-set in the {{ $input }} on the update builder. | |
func(u *{{ $n.UpdateName }}) SetInput(i {{ $input }}) *{{ $n.UpdateName }} { | |
i.Mutate(u.Mutation()) | |
return u | |
} | |
// SetInput applies the change-set in the {{ $input }} on the update-one builder. | |
func(u *{{ $n.UpdateOneName }}) SetInput(i {{ $input }}) *{{ $n.UpdateOneName }} { | |
i.Mutate(u.Mutation()) | |
return u | |
} | |
{{- end }} | |
{{ end }} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment