Skip to content

Instantly share code, notes, and snippets.

@aight8
Created October 21, 2020 12:55
Show Gist options
  • Select an option

  • Save aight8/ac072ad3bb76c945ee3fdbedb9d50658 to your computer and use it in GitHub Desktop.

Select an option

Save aight8/ac072ad3bb76c945ee3fdbedb9d50658 to your computer and use it in GitHub Desktop.

Revisions

  1. aight8 renamed this gist Oct 21, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. aight8 created this gist Oct 21, 2020.
    53 changes: 53 additions & 0 deletions gistfile1.txt
    Original 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}}