//====================================================
//the code template (split into separate strings for somewhat better readability)
761983
func buildCode() string {

	var clientFields = `{{if eq .Name "client" }}	//Use TLS when Send()ing
	UseTLS bool
	HttpClient *http.Client
{{end}}`

	var fields = `{{range $index, $element := .Fields}}	{{.PrivateName}} {{.Type}}{{if not .Required}}
	{{.PrivateName}}Set bool{{end}}
{{end}}`

	var paramVal = `{{if eq .Type "string"}}h.{{.PrivateName}}{{end}}` +
		`{{if eq .Type "bool"}}bool2str(h.{{.PrivateName}}){{end}}` +
		`{{if eq .Type "int64"}}int2str(h.{{.PrivateName}}){{end}}` +
		`{{if eq .Type "float64"}}float2str(h.{{.PrivateName}}){{end}}`

	var params = `{{range $index, $element := .Fields}}{{if ne .Param ""}}{{if not .Required}}	if h.{{.PrivateName}}Set {
{{end}}		v.Add({{.ParamStr}}, ` + paramVal + `){{if not .Required}}
	}{{end}}
{{end}}{{end}}`

	var constructorDocs = `{{if ne .Name "client" }}// New{{ .StructName }} ` +
		`creates a new {{ .StructName }} Hit Type.` +
		`{{range $index, $element := .Fields}}
		{{if .Required}}{{.Docs}}{{end}}{{end}}`

	var constructorParams = `{{range $index, $element := .Fields}}{{if .Required}}` +
		`{{if ne $index 0}},{{end}}{{.PrivateName}} {{.Type}}` +
		`{{end}}{{end}}`

	var constructor = constructorDocs + `
func New{{ .StructName }}(` + constructorParams + `) *{{.StructName}}	{
	h := &{{ .StructName }}{
{{range $index, $element := .Fields}}{{if .Required}}		{{.PrivateName}}: {{.PrivateName}},
{{end}}{{end}}	}
	return h
}{{end}}
`

	var clientFuncs = `{{if eq .Name "client" }}
func (c *Client) setType(h hitType) {
	switch h.(type) {
{{range $index, $id := .HitTypeIDs}}	case *{{ exportName $id}}:
		c.hitType = "{{ $id }}"
{{end}}	}
}
{{end}}`

	var setterFuncs = `{{range $index, $element := .Fields}}{{if not .Required}}{{.Docs}}
func (h *{{$.StructName}}) {{.Name}}({{.PrivateName}} {{.Type}}) *{{$.StructName}}	{
	h.{{.PrivateName}} = {{.PrivateName}}
	h.{{.PrivateName}}Set = true
	return h
}

{{end}}{{end}}`

	return `package ga

//WARNING: This file was generated. Do not edit.

//{{.StructName}} Hit Type
type {{.StructName}} struct {
` + clientFields + fields + `}

` + constructor + clientFuncs + `

func (h *{{.StructName}}) addFields(v url.Values) error {
` + params + `	return nil
}

` + setterFuncs + `

func (h *{{.StructName}}) Copy() *{{.StructName}} {
	c := *h
	return &c
}
`
}
924757
func emailTemplateFilenameRegexp(c context.Context) *regexp.Regexp {
	appId := info.AppID(c)
	pattern := fmt.Sprintf(`^%s+/email-templates/([a-z][a-z0-9_]*)\.template$`, regexp.QuoteMeta(appId))
	return regexp.MustCompile(pattern)
}
226831
func (set *TemplateSet) RenderTemplateString(s string, ctx Context) (string, error) {
	set.firstTemplateCreated = true

	tpl := Must(set.FromString(s))
	result, err := tpl.Execute(ctx)
	if err != nil {
		return "", err
	}
	return result, nil
}
932657
func NewTemplate(descs []*protokit.FileDescriptor) *Template {
	files := make([]*File, 0, len(descs))

	for _, f := range descs {
		file := &File{
			Name:          f.GetName(),
			Description:   description(f.GetSyntaxComments().String()),
			Package:       f.GetPackage(),
			HasEnums:      len(f.Enums) > 0,
			HasExtensions: len(f.Extensions) > 0,
			HasMessages:   len(f.Messages) > 0,
			HasServices:   len(f.Services) > 0,
			Enums:         make(orderedEnums, 0, len(f.Enums)),
			Extensions:    make(orderedExtensions, 0, len(f.Extensions)),
			Messages:      make(orderedMessages, 0, len(f.Messages)),
			Services:      make(orderedServices, 0, len(f.Services)),
			Options:       mergeOptions(extractOptions(f.GetOptions()), extensions.Transform(f.OptionExtensions)),
		}

		for _, e := range f.Enums {
			file.Enums = append(file.Enums, parseEnum(e))
		}

		for _, e := range f.Extensions {
			file.Extensions = append(file.Extensions, parseFileExtension(e))
		}

		// Recursively add nested types from messages
		var addFromMessage func(*protokit.Descriptor)
		addFromMessage = func(m *protokit.Descriptor) {
			file.Messages = append(file.Messages, parseMessage(m))
			for _, e := range m.Enums {
				file.Enums = append(file.Enums, parseEnum(e))
			}
			for _, n := range m.Messages {
				addFromMessage(n)
			}
		}
		for _, m := range f.Messages {
			addFromMessage(m)
		}

		for _, s := range f.Services {
			file.Services = append(file.Services, parseService(s))
		}

		sort.Sort(file.Enums)
		sort.Sort(file.Extensions)
		sort.Sort(file.Messages)
		sort.Sort(file.Services)

		files = append(files, file)
	}

	return &Template{Files: files, Scalars: makeScalars()}
}
163035
func (c *Clientset) Template() templateinternalversion.TemplateInterface {
	return &faketemplateinternalversion.FakeTemplate{Fake: &c.Fake}
}
831361
func NewTemplate() *Template {
	return &Template{
		Conditions: make(map[string]interface{}),
		Outputs:    make(map[string]Output),
		Parameters: make(map[string]Parameter),
		Resources:  make(map[string]Resource),
	}
}
434648
func (p *partial) template() (*Template, error) {
	if p.tpl == nil {
		var err error

		p.tpl, err = Parse(p.source)
		if err != nil {
			return nil, err
		}
	}

	return p.tpl, nil
}
858962
func (c *Client) CopyTemplate(p *CopyTemplateParameter) (*Template, error) {
	obj, err := c.Request("copyTemplate", convertParamToMap(p))
	if err != nil {
		return nil, err
	}
	return obj.(*Template), err
}
324868
func (s *TemplateService) CreateTemplate(p *CreateTemplateParams) (*CreateTemplateResponse, error) {
	resp, err := s.cs.newRequest("createTemplate", p.toURLValues())
	if err != nil {
		return nil, err
	}

	var r CreateTemplateResponse
	if err := json.Unmarshal(resp, &r); err != nil {
		return nil, err
	}

	// If we have a async client, we need to wait for the async result
	if s.cs.async {
		b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
		if err != nil {
			if err == AsyncTimeoutErr {
				return &r, err
			}
			return nil, err
		}

		b, err = getRawValue(b)
		if err != nil {
			return nil, err
		}

		if err := json.Unmarshal(b, &r); err != nil {
			return nil, err
		}
	}

	return &r, nil
}
740593
func (a *ArgType) TemplateSet() *TemplateSet {
	if a.templateSet == nil {
		a.templateSet = &TemplateSet{
			funcs: a.NewTemplateFuncs(),
			l:     a.TemplateLoader,
			tpls:  map[string]*template.Template{},
		}
	}

	return a.templateSet
}
239424
func RunTemplate(tmpl *template.Template, data interface{}) string {
	var b bytes.Buffer
	err := tmpl.Execute(&b, data)
	if err != nil {
		panic(err) // should never happen, bug if it does.
	}
	return b.String()
}
433294
func ValidateTemplate(template *templateapi.Template) (allErrs field.ErrorList) {
	allErrs = validation.ValidateObjectMeta(&template.ObjectMeta, true, ValidateTemplateName, field.NewPath("metadata"))
	allErrs = append(allErrs, validateTemplateBody(template)...)
	return
}
789639
func prepareTemplates(templatesPath string) *templates.Bundle {
	return &templates.Bundle{
		Loader:          templates.FileSystemLoader(templatesPath),
		DebugMode:       info.IsDevAppServer,
		DefaultTemplate: "base",
		FuncMap: template.FuncMap{
			// Count returns sequential integers in range [0, n] (inclusive).
			"Count": func(n int) []int {
				out := make([]int, n+1)
				for i := range out {
					out[i] = i
				}
				return out
			},
			// Pair combines two args into one map with keys "First" and "Second", to
			// pass pairs to templates (that in golang can accept only one argument).
			"Pair": func(a1, a2 interface{}) map[string]interface{} {
				return map[string]interface{}{
					"First":  a1,
					"Second": a2,
				}
			},
		},
		DefaultArgs: func(c context.Context, e *templates.Extra) (templates.Args, error) {
			loginURL, err := auth.LoginURL(c, e.Request.URL.RequestURI())
			if err != nil {
				return nil, err
			}
			logoutURL, err := auth.LogoutURL(c, e.Request.URL.RequestURI())
			if err != nil {
				return nil, err
			}
			token, err := xsrf.Token(c)
			if err != nil {
				return nil, err
			}
			return templates.Args{
				"AppVersion":  strings.Split(info.VersionID(c), ".")[0],
				"IsAnonymous": auth.CurrentIdentity(c) == "anonymous:anonymous",
				"User":        auth.CurrentUser(c),
				"LoginURL":    loginURL,
				"LogoutURL":   logoutURL,
				"XsrfToken":   token,
				"HandlerDuration": func() time.Duration {
					return clock.Now(c).Sub(startTime(c))
				},
			}, nil
		},
	}
}
101124
func Convert_v1_Template_To_template_Template(in *v1.Template, out *template.Template, s conversion.Scope) error {
	return autoConvert_v1_Template_To_template_Template(in, out, s)
}
472899
func (p *Lexer) TemplateText() (string, bool) {
	var buf []byte

	for {
		c, _ := p.Byte()
		if !isTemplateTextChar(c) {
			break
		}
		buf = append(buf, c)
	}

	p.UnreadByte()
	return string(buf), len(buf) > 0
}
1344590
func (g *Generator) ProcessTemplate(tmplName, tmpl string, data interface{}) error {
	vars := []string{}
	for varName, varValue := range g.vars {
		vars = append(vars, fmt.Sprintf("{{ $%s := %q }}", varName, varValue))
	}

	if len(vars) > 0 {
		tmpl = strings.Join(vars, "\n") + "\n" + tmpl
	}

	t, err := template.New(tmplName).Funcs(g.templateFuncs).Parse(tmpl)
	if err != nil {
		return fmt.Errorf("failed to parse template: %v\n%s", err, tmpl)
	}

	if err := t.Execute(g.body, data); err != nil {
		return fmt.Errorf("failed to execute template: %v", err)
	}

	return nil
}
191074
func (runtimeLoader *templateRuntime) templateLoad(name, lang string) (tmpl Template) {
	langName := name
	found := false
	if lang != "" {
		// Look up and return the template.
		langName = name + "." + lang
		tmpl, found = runtimeLoader.templateMap[langName]
		if found {
			return
		}
		tmpl, found = runtimeLoader.templateMap[name]
	} else {
		tmpl, found = runtimeLoader.templateMap[name]
		if found {
			return
		}
	}

	if !found {
		// Neither name is found
		// Look up and return the template.
		for _, engine := range runtimeLoader.templatesAndEngineList {
			if tmpl = engine.Lookup(langName); tmpl != nil {
				found = true
				break
			}
			if tmpl = engine.Lookup(name); tmpl != nil {
				found = true
				break
			}
		}
		if !found {
			return
		}
	}

	// If we found anything store it in the map, we need to copy so we do not
	// run into concurrency issues
	runtimeLoader.loader.templateMutex.Lock()
	defer runtimeLoader.loader.templateMutex.Unlock()

	// In case another thread has loaded the map, reload the atomic value and check
	newRuntimeLoader := runtimeLoader.loader.runtimeLoader.Load().(*templateRuntime)
	if newRuntimeLoader.version != runtimeLoader.version {
		return
	}

	newTemplateMap := map[string]Template{}
	for k, v := range newRuntimeLoader.templateMap {
		newTemplateMap[k] = v
	}
	newTemplateMap[langName] = tmpl
	if _, found := newTemplateMap[name]; !found {
		newTemplateMap[name] = tmpl
	}
	runtimeCopy := &templateRuntime{}
	*runtimeCopy = *newRuntimeLoader
	runtimeCopy.templateMap = newTemplateMap

	// Set the atomic value
	runtimeLoader.loader.runtimeLoader.Store(runtimeCopy)
	return
}