From ed9c75aac2bc7f02e10bdb2ff826255b2e79d013 Mon Sep 17 00:00:00 2001 From: Adam Scarr Date: Tue, 20 Feb 2018 20:18:43 +1100 Subject: [PATCH] Better templates --- .circleci/config.yml | 2 +- .gitattributes | 2 + .../args.go => codegen/templates/args.gotpl | 8 +--- codegen/templates/data.go | 10 ++++ .../file.go => codegen/templates/file.gotpl | 25 ++++------ codegen/templates/inliner/inliner.go | 48 +++++++++++++++++++ .../input.go => codegen/templates/input.gotpl | 14 ++---- .../templates/interface.gotpl | 7 --- .../model.go => codegen/templates/model.gotpl | 10 +--- .../templates/object.gotpl | 11 +---- .../templates/templates.go | 21 ++++---- main.go | 3 +- templates/templates.go | 3 -- 13 files changed, 94 insertions(+), 70 deletions(-) create mode 100644 .gitattributes rename templates/args.go => codegen/templates/args.gotpl (87%) create mode 100644 codegen/templates/data.go rename templates/file.go => codegen/templates/file.gotpl (93%) create mode 100644 codegen/templates/inliner/inliner.go rename templates/input.go => codegen/templates/input.gotpl (80%) rename templates/interface.go => codegen/templates/interface.gotpl (86%) rename templates/model.go => codegen/templates/model.gotpl (60%) rename templates/object.go => codegen/templates/object.gotpl (95%) rename templates.go => codegen/templates/templates.go (61%) delete mode 100644 templates/templates.go diff --git a/.circleci/config.yml b/.circleci/config.yml index d17b1b1bfbe..a1d642f670b 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -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 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000000..f6ffcb7da1e --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +/codegen/templates/data.go linguist-generated +/example/dataloader/*_gen.go linguist-generated diff --git a/templates/args.go b/codegen/templates/args.gotpl similarity index 87% rename from templates/args.go rename to codegen/templates/args.gotpl index cb3df53f55c..554ac888897 100644 --- a/templates/args.go +++ b/codegen/templates/args.gotpl @@ -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{}" }} @@ -22,6 +18,4 @@ const argsTpl = ` arg{{$i}} = {{if $arg.Type.IsPtr}}&{{end}}tmp2 } {{- end}} - {{- end }} -{{- end }} -` + {{- end -}} diff --git a/codegen/templates/data.go b/codegen/templates/data.go new file mode 100644 index 00000000000..009d78c59a0 --- /dev/null +++ b/codegen/templates/data.go @@ -0,0 +1,10 @@ +package templates + +var data = map[string]string{ + "args.gotpl": "\t{{- range $i, $arg := . }}\n\t\tvar arg{{$i}} {{$arg.Signature }}\n\t\t{{- if eq $arg.GoType \"map[string]interface{}\" }}\n\t\t\tif tmp, ok := field.Args[{{$arg.GQLName|quote}}]; ok {\n\t\t\t\t{{- if $arg.Type.IsPtr }}\n\t\t\t\t\ttmp2 := tmp.({{$arg.GoType}})\n\t\t\t\t\targ{{$i}} = &tmp2\n\t\t\t\t{{- else }}\n\t\t\t\t\targ{{$i}} = tmp.({{$arg.GoType}})\n\t\t\t\t{{- end }}\n\t\t\t}\n\t\t{{- else}}\n\t\t\tif tmp, ok := field.Args[{{$arg.GQLName|quote}}]; ok {\n\t\t\t\t{{$arg.Unmarshal \"tmp2\" \"tmp\" }}\n\t\t\t\tif err != nil {\n\t\t\t\t\tbadArgs = true\n\t\t\t\t}\n\t\t\t\targ{{$i}} = {{if $arg.Type.IsPtr}}&{{end}}tmp2\n\t\t\t}\n\t\t{{- end}}\n\t{{- end -}}\n", + "file.gotpl": "// This file was generated by github.com/vektah/gqlgen, DO NOT EDIT\n\npackage {{ .PackageName }}\n\nimport (\n{{- range $import := .Imports }}\n\t{{- $import.Write }}\n{{ end }}\n)\n\nfunc MakeExecutableSchema(resolvers Resolvers) graphql.ExecutableSchema {\n\treturn &executableSchema{resolvers}\n}\n\ntype Resolvers interface {\n{{- range $object := .Objects -}}\n\t{{ range $field := $object.Fields -}}\n\t\t{{ $field.ResolverDeclaration }}\n\t{{ end }}\n{{- end }}\n}\n\n{{ range $model := .Models }}\n\t{{ template \"model.gotpl\" $model }}\n{{- end}}\n\ntype executableSchema struct {\n\tresolvers Resolvers\n}\n\nfunc (e *executableSchema) Schema() *schema.Schema {\n\treturn parsedSchema\n}\n\nfunc (e *executableSchema) Query(ctx context.Context, doc *query.Document, variables map[string]interface{}, op *query.Operation) *graphql.Response {\n\t{{- if .QueryRoot }}\n\t\tec := executionContext{resolvers: e.resolvers, variables: variables, doc: doc, ctx: ctx}\n\n\t\tdata := ec._{{.QueryRoot.GQLType|lcFirst}}(op.Selections)\n\t\tec.wg.Wait()\n\n\t\treturn &graphql.Response{\n\t\t\tData: data,\n\t\t\tErrors: ec.Errors,\n\t\t}\n\t{{- else }}\n\t\treturn &graphql.Response{Errors: []*errors.QueryError{ {Message: \"queries are not supported\"} }}\n\t{{- end }}\n}\n\nfunc (e *executableSchema) Mutation(ctx context.Context, doc *query.Document, variables map[string]interface{}, op *query.Operation) *graphql.Response {\n\t{{- if .MutationRoot }}\n\t\tec := executionContext{resolvers: e.resolvers, variables: variables, doc: doc, ctx: ctx}\n\n\t\tdata := ec._{{.MutationRoot.GQLType|lcFirst}}(op.Selections)\n\t\tec.wg.Wait()\n\n\t\treturn &graphql.Response{\n\t\t\tData: data,\n\t\t\tErrors: ec.Errors,\n\t\t}\n\t{{- else }}\n\t\treturn &graphql.Response{Errors: []*errors.QueryError{ {Message: \"mutations are not supported\"} }}\n\t{{- end }}\n}\n\nfunc (e *executableSchema) Subscription(ctx context.Context, doc *query.Document, variables map[string]interface{}, op *query.Operation) <-chan *graphql.Response {\n\t{{- if .SubscriptionRoot }}\n\t\tevents := make(chan *graphql.Response, 10)\n\n\t\tec := executionContext{resolvers: e.resolvers, variables: variables, doc: doc, ctx: ctx}\n\n\t\teventData := ec._{{.SubscriptionRoot.GQLType|lcFirst}}(op.Selections)\n\t\tif ec.Errors != nil {\n\t\t\tevents<-&graphql.Response{\n\t\t\t\tData: graphql.Null,\n\t\t\t\tErrors: ec.Errors,\n\t\t\t}\n\t\t\tclose(events)\n\t\t} else {\n\t\t\tgo func() {\n\t\t\t\tfor data := range eventData {\n\t\t\t\t\tec.wg.Wait()\n\t\t\t\t\tevents <- &graphql.Response{\n\t\t\t\t\t\tData: data,\n\t\t\t\t\t\tErrors: ec.Errors,\n\t\t\t\t\t}\n\t\t\t\t\ttime.Sleep(20 * time.Millisecond)\n\t\t\t\t}\n\t\t\t}()\n\t\t}\n\t\treturn events\n\t{{- else }}\n\t\tevents := make(chan *graphql.Response, 1)\n\t\tevents<-&graphql.Response{Errors: []*errors.QueryError{ {Message: \"subscriptions are not supported\"} }}\n\t\treturn events\n\t{{- end }}\n}\n\ntype executionContext struct {\n\terrors.Builder\n\tresolvers Resolvers\n\tvariables map[string]interface{}\n\tdoc *query.Document\n\tctx context.Context\n\twg sync.WaitGroup\n}\n\n{{- range $object := .Objects }}\n\t{{ template \"object.gotpl\" $object }}\n{{- end}}\n\n{{- range $interface := .Interfaces }}\n\t{{ template \"interface.gotpl\" $interface }}\n{{- end }}\n\n{{- range $input := .Inputs }}\n\t{{ template \"input.gotpl\" $input }}\n{{- end }}\n\nvar parsedSchema = schema.MustParse({{.SchemaRaw|quote}})\n\nfunc (ec *executionContext) introspectSchema() *introspection.Schema {\n\treturn introspection.WrapSchema(parsedSchema)\n}\n\nfunc (ec *executionContext) introspectType(name string) *introspection.Type {\n\tt := parsedSchema.Resolve(name)\n\tif t == nil {\n\t\treturn nil\n\t}\n\treturn introspection.WrapType(t)\n}\n", + "input.gotpl": "\t{{- if .IsMarshaled }}\n\tfunc Unmarshal{{ .GQLType }}(v interface{}) ({{.FullName}}, error) {\n\t\tvar it {{.FullName}}\n\n\t\tfor k, v := range v.(map[string]interface{}) {\n\t\t\tswitch k {\n\t\t\t{{- range $field := .Fields }}\n\t\t\tcase {{$field.GQLName|quote}}:\n\t\t\t\t{{$field.Unmarshal \"val\" \"v\" }}\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn it, err\n\t\t\t\t}\n\t\t\t\tit.{{$field.GoVarName}} = {{if $field.Type.IsPtr}}&{{end}}val\n\t\t\t{{- end }}\n\t\t\t}\n\t\t}\n\n\t\treturn it, nil\n\t}\n\t{{- end }}\n", + "interface.gotpl": "{{- $interface := . }}\n\nfunc (ec *executionContext) _{{$interface.GQLType|lcFirst}}(sel []query.Selection, it *{{$interface.FullName}}) graphql.Marshaler {\n\tswitch it := (*it).(type) {\n\tcase nil:\n\t\treturn graphql.Null\n\t{{- range $implementor := $interface.Implementors }}\n\tcase {{$implementor.FullName}}:\n\t\treturn ec._{{$implementor.GQLType|lcFirst}}(sel, &it)\n\n\tcase *{{$implementor.FullName}}:\n\t\treturn ec._{{$implementor.GQLType|lcFirst}}(sel, it)\n\n\t{{- end }}\n\tdefault:\n\t\tpanic(fmt.Errorf(\"unexpected type %T\", it))\n\t}\n}\n", + "model.gotpl": "\ttype {{.GoType}} struct {\n\t\t{{- range $field := .Fields }}\n\t\t\t{{- if $field.GoVarName }}\n\t\t\t\t{{ $field.GoVarName }} {{$field.Signature}}\n\t\t\t{{- else }}\n\t\t\t\t{{ $field.GoFKName }} {{$field.GoFKType}}\n\t\t\t{{- end }}\n\t\t{{- end }}\n\t}\n", + "object.gotpl": "{{ $object := . }}\n\nvar {{ $object.GQLType|lcFirst}}Implementors = {{$object.Implementors}}\n\n// nolint: gocyclo, errcheck, gas, goconst\n{{- if .Stream }}\nfunc (ec *executionContext) _{{$object.GQLType|lcFirst}}(sel []query.Selection{{if not $object.Root}}, it *{{$object.FullName}}{{end}}) <-chan graphql.Marshaler {\n\tfields := graphql.CollectFields(ec.doc, sel, {{$object.GQLType|lcFirst}}Implementors, ec.variables)\n\n\tif len(fields) != 1 {\n\t\tec.Errorf(\"must subscribe to exactly one stream\")\n\t\treturn nil\n\t}\n\n\tvar field = fields[0]\n\tchannel := make(chan graphql.Marshaler, 1)\n\tswitch field.Name {\n\t{{- range $field := $object.Fields }}\n\tcase \"{{$field.GQLName}}\":\n\t\tbadArgs := false\n\t\t{{- template \"args.gotpl\" $field.Args }}\n\t\tif badArgs {\n\t\t\treturn nil\n\t\t}\n\n\t\t{{- if $field.GoVarName }}\n\t\t\tresults := it.{{$field.GoVarName}}\n\t\t{{- else if $field.GoMethodName }}\n\t\t\t{{- if $field.NoErr }}\n\t\t\t\tresults := {{$field.GoMethodName}}({{ $field.CallArgs }})\n\t\t\t{{- else }}\n\t\t\t\tresults, err := {{$field.GoMethodName}}({{ $field.CallArgs }})\n\t\t\t\tif err != nil {\n\t\t\t\t\tec.Error(err)\n\t\t\t\t\treturn nil\n\t\t\t\t}\n\t\t\t{{- end }}\n\t\t{{- else }}\n\t\t\tresults, err := ec.resolvers.{{ $object.GQLType }}_{{ $field.GQLName }}({{ $field.CallArgs }})\n\t\t\tif err != nil {\n\t\t\t\tec.Error(err)\n\t\t\t\treturn nil\n\t\t\t}\n\t\t{{- end }}\n\n\t\tgo func() {\n\t\t\tfor res := range results {\n\t\t\t\tvar out graphql.OrderedMap\n\t\t\t\tvar messageRes graphql.Marshaler\n\t\t\t\t{{ $field.WriteJson \"messageRes\" }}\n\t\t\t\tout.Add(field.Alias, messageRes)\n\t\t\t\tchannel <- &out\n\t\t\t}\n\t\t}()\n\n\t{{- end }}\n\tdefault:\n\t\tpanic(\"unknown field \" + strconv.Quote(field.Name))\n\t}\n\n\treturn channel\n}\n{{- else }}\nfunc (ec *executionContext) _{{$object.GQLType|lcFirst}}(sel []query.Selection{{if not $object.Root}}, it *{{$object.FullName}} {{end}}) graphql.Marshaler {\n\tfields := graphql.CollectFields(ec.doc, sel, {{$object.GQLType|lcFirst}}Implementors, ec.variables)\n\tout := graphql.NewOrderedMap(len(fields))\n\tfor i, field := range fields {\n\t\tout.Keys[i] = field.Alias\n\t\tout.Values[i] = graphql.Null\n\n\t\tswitch field.Name {\n\t\tcase \"__typename\":\n\t\t\tout.Values[i] = graphql.MarshalString({{$object.GQLType|quote}})\n\t\t{{- range $field := $object.Fields }}\n\t\tcase \"{{$field.GQLName}}\":\n\t\t\tbadArgs := false\n\t\t\t{{- template \"args.gotpl\" $field.Args }}\n\t\t\tif badArgs {\n\t\t\t\tcontinue\n\t\t\t}\n\n\t\t\t{{- if $field.IsConcurrent }}\n\t\t\t\tec.wg.Add(1)\n\t\t\t\tgo func(i int, field graphql.CollectedField) {\n\t\t\t\t\tdefer ec.wg.Done()\n\t\t\t{{- end }}\n\n\t\t\t{{- if $field.GoVarName }}\n\t\t\t\tres := it.{{$field.GoVarName}}\n\t\t\t{{- else if $field.GoMethodName }}\n\t\t\t\t{{- if $field.NoErr }}\n\t\t\t\t\tres := {{$field.GoMethodName}}({{ $field.CallArgs }})\n\t\t\t\t{{- else }}\n\t\t\t\t\tres, err := {{$field.GoMethodName}}({{ $field.CallArgs }})\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\tec.Error(err)\n\t\t\t\t\t\t{{ if $field.IsConcurrent }}return{{ else }}continue{{end}}\n\t\t\t\t\t}\n\t\t\t\t{{- end }}\n\t\t\t{{- else }}\n\t\t\t\tres, err := ec.resolvers.{{ $object.GQLType }}_{{ $field.GQLName }}({{ $field.CallArgs }})\n\t\t\t\tif err != nil {\n\t\t\t\t\tec.Error(err)\n\t\t\t\t\t{{ if $field.IsConcurrent }}return{{ else }}continue{{end}}\n\t\t\t\t}\n\t\t\t{{- end }}\n\n\t\t\t{{ $field.WriteJson \"out.Values[i]\" }}\n\n\t\t\t{{- if $field.IsConcurrent }}\n\t\t\t\t}(i, field)\n\t\t\t{{- end }}\n\t\t{{- end }}\n\t\tdefault:\n\t\t\tpanic(\"unknown field \" + strconv.Quote(field.Name))\n\t\t}\n\t}\n\n\treturn out\n}\n{{- end }}\n", +} diff --git a/templates/file.go b/codegen/templates/file.gotpl similarity index 93% rename from templates/file.go rename to codegen/templates/file.gotpl index 29247ed13c3..aa48da77159 100644 --- a/templates/file.go +++ b/codegen/templates/file.gotpl @@ -1,7 +1,3 @@ -package templates - -const fileTpl = ` -{{define "file" }} // This file was generated by github.com/vektah/gqlgen, DO NOT EDIT package {{ .PackageName }} @@ -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 { @@ -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, @@ -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, @@ -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}}) @@ -135,6 +131,3 @@ func (ec *executionContext) introspectType(name string) *introspection.Type { } return introspection.WrapType(t) } - -{{end}} -` diff --git a/codegen/templates/inliner/inliner.go b/codegen/templates/inliner/inliner.go new file mode 100644 index 00000000000..82fe4f04a93 --- /dev/null +++ b/codegen/templates/inliner/inliner.go @@ -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) +} diff --git a/templates/input.go b/codegen/templates/input.gotpl similarity index 80% rename from templates/input.go rename to codegen/templates/input.gotpl index 6e86f4e404c..8d2057eb464 100644 --- a/templates/input.go +++ b/codegen/templates/input.gotpl @@ -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 }} @@ -17,10 +13,8 @@ const inputTpl = ` it.{{$field.GoVarName}} = {{if $field.Type.IsPtr}}&{{end}}val {{- end }} } - } - + } + return it, nil } {{- end }} -{{- end }} -` diff --git a/templates/interface.go b/codegen/templates/interface.gotpl similarity index 86% rename from templates/interface.go rename to codegen/templates/interface.gotpl index 3268dd01b4b..491d9a5fb65 100644 --- a/templates/interface.go +++ b/codegen/templates/interface.gotpl @@ -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 { @@ -20,6 +16,3 @@ func (ec *executionContext) _{{$interface.GQLType|lcFirst}}(sel []query.Selectio panic(fmt.Errorf("unexpected type %T", it)) } } - -{{- end}} -` diff --git a/templates/model.go b/codegen/templates/model.gotpl similarity index 60% rename from templates/model.go rename to codegen/templates/model.gotpl index ea1e4d90412..145c41fda43 100644 --- a/templates/model.go +++ b/codegen/templates/model.gotpl @@ -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 }} -` diff --git a/templates/object.go b/codegen/templates/object.gotpl similarity index 95% rename from templates/object.go rename to codegen/templates/object.gotpl index a71f1fe364a..09076bd089c 100644 --- a/templates/object.go +++ b/codegen/templates/object.gotpl @@ -1,7 +1,3 @@ -package templates - -const objectTpl = ` -{{- define "object" }} {{ $object := . }} var {{ $object.GQLType|lcFirst}}Implementors = {{$object.Implementors}} @@ -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 } @@ -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 } @@ -123,6 +119,3 @@ func (ec *executionContext) _{{$object.GQLType|lcFirst}}(sel []query.Selection{{ return out } {{- end }} - -{{- end}} -` diff --git a/templates.go b/codegen/templates/templates.go similarity index 61% rename from templates.go rename to codegen/templates/templates.go index b16faf33728..49eec9efcfa 100644 --- a/templates.go +++ b/codegen/templates/templates.go @@ -1,4 +1,6 @@ -package main +//go:generate go run ./inliner/inliner.go + +package templates import ( "bytes" @@ -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 } diff --git a/main.go b/main.go index 048bc4aad1f..792458faf35 100644 --- a/main.go +++ b/main.go @@ -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" ) @@ -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) diff --git a/templates/templates.go b/templates/templates.go deleted file mode 100644 index dac2f5ff08f..00000000000 --- a/templates/templates.go +++ /dev/null @@ -1,3 +0,0 @@ -package templates - -const All = argsTpl + fileTpl + interfaceTpl + objectTpl + inputTpl + modelTpl