Skip to content

Commit

Permalink
Better templates
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Feb 20, 2018
1 parent f3731c7 commit 278df9d
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
gometalinter --install &&
dep ensure --vendor-only
- run: go install -v . && go get -u github.com/vektah/dataloaden
- run: go generate ./... && if [[ $(git diff) ]] ; then echo "you need to run go generate" ; git diff ; exit 1 ; fi
- run: go generate ./... && if [[ $(git --no-pager diff) ]] ; then echo "you need to run go generate" ; git --no-pager diff ; exit 1 ; fi
- run: go vet ./...
- run: go test -race ./...
- run: gometalinter --disable gocyclo ./example
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/codegen/templates/data.go linguist-generated
/example/dataloader/*_gen.go linguist-generated
8 changes: 1 addition & 7 deletions templates/args.go → codegen/templates/args.gotpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
package templates

const argsTpl = `
{{- define "args" }}
{{- range $i, $arg := . }}
var arg{{$i}} {{$arg.Signature }}
{{- if eq $arg.GoType "map[string]interface{}" }}
Expand All @@ -22,6 +18,4 @@ const argsTpl = `
arg{{$i}} = {{if $arg.Type.IsPtr}}&{{end}}tmp2
}
{{- end}}
{{- end }}
{{- end }}
`
{{- end -}}
10 changes: 10 additions & 0 deletions codegen/templates/data.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 9 additions & 16 deletions templates/file.go → codegen/templates/file.gotpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
package templates

const fileTpl = `
{{define "file" }}
// This file was generated by github.com/vektah/gqlgen, DO NOT EDIT

package {{ .PackageName }}
Expand All @@ -24,8 +20,8 @@ type Resolvers interface {
{{- end }}
}

{{- range $model := .Models }}
{{ template "model" $model }}
{{ range $model := .Models }}
{{ template "model.gotpl" $model }}
{{- end}}

type executableSchema struct {
Expand All @@ -39,10 +35,10 @@ func (e *executableSchema) Schema() *schema.Schema {
func (e *executableSchema) Query(ctx context.Context, doc *query.Document, variables map[string]interface{}, op *query.Operation) *graphql.Response {
{{- if .QueryRoot }}
ec := executionContext{resolvers: e.resolvers, variables: variables, doc: doc, ctx: ctx}

data := ec._{{.QueryRoot.GQLType|lcFirst}}(op.Selections)
ec.wg.Wait()

return &graphql.Response{
Data: data,
Errors: ec.Errors,
Expand All @@ -55,10 +51,10 @@ func (e *executableSchema) Query(ctx context.Context, doc *query.Document, varia
func (e *executableSchema) Mutation(ctx context.Context, doc *query.Document, variables map[string]interface{}, op *query.Operation) *graphql.Response {
{{- if .MutationRoot }}
ec := executionContext{resolvers: e.resolvers, variables: variables, doc: doc, ctx: ctx}

data := ec._{{.MutationRoot.GQLType|lcFirst}}(op.Selections)
ec.wg.Wait()

return &graphql.Response{
Data: data,
Errors: ec.Errors,
Expand Down Expand Up @@ -111,15 +107,15 @@ type executionContext struct {
}

{{- range $object := .Objects }}
{{ template "object" $object }}
{{ template "object.gotpl" $object }}
{{- end}}

{{- range $interface := .Interfaces }}
{{ template "interface" $interface }}
{{ template "interface.gotpl" $interface }}
{{- end }}

{{- range $input := .Inputs }}
{{ template "input" $input }}
{{ template "input.gotpl" $input }}
{{- end }}

var parsedSchema = schema.MustParse({{.SchemaRaw|quote}})
Expand All @@ -135,6 +131,3 @@ func (ec *executionContext) introspectType(name string) *introspection.Type {
}
return introspection.WrapType(t)
}
{{end}}
`
48 changes: 48 additions & 0 deletions codegen/templates/inliner/inliner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"bytes"
"io/ioutil"
"strconv"
"strings"

"golang.org/x/tools/imports"
)

func main() {
dir := "./"

files, err := ioutil.ReadDir(dir)
if err != nil {
panic(err)
}

out := bytes.Buffer{}
out.WriteString("package templates\n\n")
out.WriteString("var data = map[string]string{\n")

for _, f := range files {
if !strings.HasSuffix(f.Name(), ".gotpl") {
continue
}

b, err := ioutil.ReadFile(dir + f.Name())
if err != nil {
panic(err)
}

out.WriteString(strconv.Quote(f.Name()))
out.WriteRune(':')
out.WriteString(strconv.Quote(string(b)))
out.WriteString(",\n")
}

out.WriteString("}\n")

formatted, err := imports.Process(dir+"data.go", out.Bytes(), nil)
if err != nil {
panic(err)
}

ioutil.WriteFile(dir+"data.go", formatted, 0644)
}
14 changes: 4 additions & 10 deletions templates/input.go → codegen/templates/input.gotpl
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package templates

const inputTpl = `
{{- define "input" }}
{{- if .IsMarshaled }}
{{- if .IsMarshaled }}
func Unmarshal{{ .GQLType }}(v interface{}) ({{.FullName}}, error) {
var it {{.FullName}}

for k, v := range v.(map[string]interface{}) {
switch k {
{{- range $field := .Fields }}
Expand All @@ -17,10 +13,8 @@ const inputTpl = `
it.{{$field.GoVarName}} = {{if $field.Type.IsPtr}}&{{end}}val
{{- end }}
}
}
}

return it, nil
}
{{- end }}
{{- end }}
`
7 changes: 0 additions & 7 deletions templates/interface.go → codegen/templates/interface.gotpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
package templates

const interfaceTpl = `
{{- define "interface"}}
{{- $interface := . }}

func (ec *executionContext) _{{$interface.GQLType|lcFirst}}(sel []query.Selection, it *{{$interface.FullName}}) graphql.Marshaler {
Expand All @@ -20,6 +16,3 @@ func (ec *executionContext) _{{$interface.GQLType|lcFirst}}(sel []query.Selectio
panic(fmt.Errorf("unexpected type %T", it))
}
}
{{- end}}
`
10 changes: 2 additions & 8 deletions templates/model.go → codegen/templates/model.gotpl
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
package templates

const modelTpl = `
{{- define "model" }}
type {{.GoType}} struct {
{{- range $field := .Fields }}
{{- if $field.GoVarName }}
{{- if $field.GoVarName }}
{{ $field.GoVarName }} {{$field.Signature}}
{{- else }}
{{- else }}
{{ $field.GoFKName }} {{$field.GoFKType}}
{{- end }}
{{- end }}
}
{{- end }}
`
11 changes: 2 additions & 9 deletions templates/object.go → codegen/templates/object.gotpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
package templates

const objectTpl = `
{{- define "object" }}
{{ $object := . }}

var {{ $object.GQLType|lcFirst}}Implementors = {{$object.Implementors}}
Expand All @@ -22,7 +18,7 @@ func (ec *executionContext) _{{$object.GQLType|lcFirst}}(sel []query.Selection{{
{{- range $field := $object.Fields }}
case "{{$field.GQLName}}":
badArgs := false
{{- template "args" $field.Args }}
{{- template "args.gotpl" $field.Args }}
if badArgs {
return nil
}
Expand Down Expand Up @@ -78,7 +74,7 @@ func (ec *executionContext) _{{$object.GQLType|lcFirst}}(sel []query.Selection{{
{{- range $field := $object.Fields }}
case "{{$field.GQLName}}":
badArgs := false
{{- template "args" $field.Args }}
{{- template "args.gotpl" $field.Args }}
if badArgs {
continue
}
Expand Down Expand Up @@ -123,6 +119,3 @@ func (ec *executionContext) _{{$object.GQLType|lcFirst}}(sel []query.Selection{{
return out
}
{{- end }}
{{- end}}
`
21 changes: 13 additions & 8 deletions templates.go → codegen/templates/templates.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package main
//go:generate go run ./inliner/inliner.go

package templates

import (
"bytes"
Expand All @@ -7,21 +9,24 @@ import (
"unicode"

"github.com/vektah/gqlgen/codegen"
"github.com/vektah/gqlgen/templates"
)

func runTemplate(e *codegen.Build) (*bytes.Buffer, error) {
t, err := template.New("").Funcs(template.FuncMap{
func Run(e *codegen.Build) (*bytes.Buffer, error) {
t := template.New("").Funcs(template.FuncMap{
"ucFirst": ucFirst,
"lcFirst": lcFirst,
"quote": strconv.Quote,
}).Parse(templates.All)
if err != nil {
return nil, err
})

for filename, data := range data {
_, err := t.New(filename).Parse(data)
if err != nil {
panic(err)
}
}

buf := &bytes.Buffer{}
err = t.Lookup("file").Execute(buf, e)
err := t.Lookup("file.gotpl").Execute(buf, e)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"

"github.com/vektah/gqlgen/codegen"
"github.com/vektah/gqlgen/codegen/templates"
"github.com/vektah/gqlgen/neelance/schema"
"golang.org/x/tools/imports"
)
Expand Down Expand Up @@ -55,7 +56,7 @@ func main() {
build.PackageName = *packageName
}

buf, err := runTemplate(build)
buf, err := templates.Run(build)
if err != nil {
fmt.Fprintf(os.Stderr, "unable to generate code: "+err.Error())
os.Exit(1)
Expand Down
3 changes: 0 additions & 3 deletions templates/templates.go

This file was deleted.

0 comments on commit 278df9d

Please sign in to comment.