// generateAPI is the main code-generation function. It evaluates
// the embedded template and then calls go/format on the resulting
// code.
generateAPI is the main code-generation function. It evaluates
the embedded template and then calls go/format on the resulting
code.
// generateAPI is the main code - generation function . It evaluates // the embedded template and then calls go / format on the resulting // code .
func (v *visitation) generateAPI() error {
	sorted := make([]string, 0, len(allTemplates))
	var err error
	for key := range allTemplates {
		sorted = append(sorted, key)
	}
	sort.Strings(sorted)
	var buf bytes.Buffer
	for _, key := range sorted {
		if err := allTemplates[key].ExecuteTemplate(&buf, key, v); err != nil {
			return errors.Wrap(err, key)
		}
	}
	formatted, err := format.Source(buf.Bytes())
	if err != nil {
		println(buf.String())
		return err
	}
	outName := v.gen.outFile
	if outName == "" {
		outName = strings.ToLower(v.Root.String()) + "_walkabout.g"
		if v.inTest {
			outName += "_test"
		}
		outName += ".go"
		outName = filepath.Join(v.gen.dir, outName)
	}
	out, err := v.gen.writeCloser(outName)
	if err != nil {
		return err
	}
	_, err = out.Write(formatted)
	if x := out.Close(); x != nil && err == nil {
		err = x
	}
	return err
}
func ( v * visitation ) generateAPI ( ) error { sorted := make ( [ ] string , 0 , len ( allTemplates ) ) var err error for key := range allTemplates { sorted = append ( sorted , key ) } sort . Strings ( sorted ) var buf bytes . Buffer for _ , key := range sorted { if err := allTemplates [ key ] . ExecuteTemplate ( & buf , key , v ) ; err != nil { return errors . Wrap ( err , key ) } } formatted , err := format . Source ( buf . Bytes ( ) ) if err != nil { println ( buf . String ( ) ) return err } outName := v . gen . outFile if outName == " " { outName = strings . ToLower ( v . Root . String ( ) ) + " " if v . inTest { outName += " " } outName += " " outName = filepath . Join ( v . gen . dir , outName ) } out , err := v . gen . writeCloser ( outName ) if err != nil { return err } _ , err = out . Write ( formatted ) if x := out . Close ( ) ; x != nil && err == nil { err = x } return err }
generateAPI is the main code-generation function.
generateAPI is the main code - generation function .
// Parse each template and sort the keys. // Execute each template in sorted order.