Created
December 10, 2021 04:37
-
-
Save manakuro/dfd6b8e6f646056b6f1c8b55ab476f2b 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.Name) "ID" }} | |
| {{ $structField }} {{ if $e.Optional }}*{{ end }}{{ $e.Type.ID.Type }} | |
| {{- else }} | |
| {{- $structField := print (singular $e.Name | pascal) "IDs" }} | |
| {{ $structField }} []{{ $e.Type.ID.Type }} | |
| {{- end }} | |
| {{- end }} | |
| } | |
| // 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.Name) "ID" }} | |
| {{- 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 not $f.Type.RType.IsPtr }}*{{ 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.Name) "ID" }} | |
| {{ $structField }} *{{ $e.Type.ID.Type }} | |
| {{ $e.MutationClear }} bool | |
| {{- else }} | |
| {{ $e.MutationAdd }} []{{ $e.Type.ID.Type }} | |
| {{ $e.MutationRemove }} []{{ $e.Type.ID.Type }} | |
| {{- end }} | |
| {{- end }} | |
| } | |
| // 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 }}(*v) | |
| } | |
| {{- end }} | |
| {{- end }} | |
| {{- range $e := $n.Edges }} | |
| {{- if $e.Unique }} | |
| if i.{{ $e.MutationClear }} { | |
| m.{{ $e.MutationClear }}() | |
| } | |
| {{- $structField := print (pascal $e.Name) "ID" }} | |
| 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