From e69b739955acd2e2bd744ec84abc61661ca6a626 Mon Sep 17 00:00:00 2001 From: marwan-at-work Date: Sun, 14 Apr 2019 22:24:17 -0400 Subject: [PATCH] add docs to the templates package --- codegen/templates/templates.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/codegen/templates/templates.go b/codegen/templates/templates.go index ad2cb86a6db..8acc897ccf5 100644 --- a/codegen/templates/templates.go +++ b/codegen/templates/templates.go @@ -19,19 +19,36 @@ import ( "github.com/pkg/errors" ) +// CurrentImports keeps track of all the import declarations that are needed during the execution of a plugin. // this is done with a global because subtemplates currently get called in functions. Lets aim to remove this eventually. var CurrentImports *Imports +// Options specify various parameters to rendering a template. type Options struct { - PackageName string - Template string + // PackageName is a helper that specifies the package header declaration. + // In other words, when you write the template you don't need to specify `package X` + // at the top of the file. By providing PackageName in the Options, the Render + // function will do that for you. + PackageName string + // Template is a string of the entire template that + // will be parsed and rendered. If it's empty, + // the plugin processor will look for .gotpl files + // in the same directory of where you wrote the plugin. + Template string + // Filename is the name of the file that will be + // written to the system disk once the template is rendered. Filename string RegionTags bool GeneratedHeader bool - Data interface{} - Funcs template.FuncMap + // Data will be passed to the template execution. + Data interface{} + Funcs template.FuncMap } +// Render renders a gql plugin template from the given Options. Render is an +// abstraction of the text/template package that makes it easier to gqlgen plugins. +// If Options.Template is empty, the Render function will look for `.gotpl` files +// inside the directory where you wrote the plugin. func Render(cfg Options) error { if CurrentImports != nil { panic(fmt.Errorf("recursive or concurrent call to RenderToFile detected"))