Created
October 21, 2020 12:55
-
-
Save aight8/ac072ad3bb76c945ee3fdbedb9d50658 to your computer and use it in GitHub Desktop.
Revisions
-
aight8 renamed this gist
Oct 21, 2020 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
aight8 created this gist
Oct 21, 2020 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,53 @@ {{define "node"}} // ... {{range $node := $.Nodes}} {{$requiredFieldIndices := xrange 0}} {{range $index, $field := $node.Fields}} {{if and (not $field.Optional) (not $field.Default) (not $field.Immutable) -}} {{$requiredFieldIndices = append $requiredFieldIndices $index}} {{end}} {{end}} {{$requiredEdgeIndices := xrange 0}} {{range $index, $edge := $node.Edges}} {{if and (not $edge.Optional) -}} {{$requiredEdgeIndices = append $requiredEdgeIndices $index}} {{end}} {{end}} func ({{$r}} {{$createTypePtr}}) SetRequiredFields( {{range $index := $requiredFieldIndices -}} {{$field := index $node.Fields $index -}} {{if $field.Unique}}{{camel (printf "%s%s" "unique_" $field.BuilderField)}}{{else}}{{camel $field.BuilderField}}{{end}} {{$field.Type}}, {{end -}} {{- range $index := $requiredEdgeIndices -}} {{- $e := index $node.Edges $index -}} {{$idsParams := print (singular $e.BuilderField | camel) "IDs"}}{{if $e.Unique}}{{$idsParams = print (camel $e.BuilderField) "ID"}}{{end}} {{$idsParams}} {{if not $e.Unique}}[]{{end}}{{$e.Type.ID.Type}}, {{- end}} ) {{$createTypePtr}} { {{- range $index := $requiredFieldIndices}} {{$field := index $node.Fields $index -}} {{$r}}.Set{{$field.StructField}}( {{- if $field.Unique}}{{camel (printf "%s%s" "unique_" $field.BuilderField)}}{{else}}{{camel $field.BuilderField}}{{end -}} ) {{- end -}} {{- range $index := $requiredEdgeIndices -}} {{- $e := index $node.Edges $index -}} {{$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}} {{- $idsParams := print (singular $e.BuilderField | camel) "IDs"}}{{if $e.Unique}}{{$idsParams = print (camel $e.BuilderField) "ID"}}{{end -}} {{- $r}}.{{$idsFunc}}({{$idsParams}}{{if not $e.Unique}}...{{end}}) {{- end}} return {{$r}} } // ... {{end}} // ... {{end}}