// Package jen is a code generator for Go
package jen import ( ) // Code represents an item of code that can be rendered. type Code interface { render(f *File, w io.Writer, s *Statement) error isNull(f *File) bool } // Save renders the file and saves to the filename provided. func ( *File) ( string) error { // notest := &bytes.Buffer{} if := .Render(); != nil { return } if := os.WriteFile(, .Bytes(), 0644); != nil { return } return nil } // Render renders the file to the provided writer. func ( *File) ( io.Writer) error { := &bytes.Buffer{} if := .render(, , nil); != nil { return } := &bytes.Buffer{} if len(.headers) > 0 { for , := range .headers { if := Comment().render(, , nil); != nil { return } if , := fmt.Fprint(, "\n"); != nil { return } } // Append an extra newline so that header comments don't get lumped in // with package comments. if , := fmt.Fprint(, "\n"); != nil { return } } for , := range .comments { if := Comment().render(, , nil); != nil { return } if , := fmt.Fprint(, "\n"); != nil { return } } if , := fmt.Fprintf(, "package %s", .name); != nil { return } if .CanonicalPath != "" { if , := fmt.Fprintf(, " // import %q", .CanonicalPath); != nil { return } } if , := fmt.Fprint(, "\n\n"); != nil { return } if := .renderImports(); != nil { return } if , := .Write(.Bytes()); != nil { return } var []byte if .NoFormat { = .Bytes() } else { var error , = format.Source(.Bytes()) if != nil { return fmt.Errorf("Error %s while formatting source:\n%s", , .String()) } } if , := .Write(); != nil { return } return nil } func ( *File) ( io.Writer) error { // Render the "C" import if it's been used in a `Qual`, `Anon` or if there's a preamble comment := .imports["C"].name != "" || len(.cgoPreamble) > 0 // Only separate the import from the main imports block if there's a preamble := && len(.cgoPreamble) > 0 := map[string]importdef{} for , := range .imports { // filter out the "C" pseudo-package so it's not rendered in a block with the other // imports, but only if it is accompanied by a preamble comment if == "C" && { continue } [] = } if len() == 1 { for , := range { if .alias && != "C" { // "C" package should be rendered without alias even when used as an anonymous import // (e.g. should never have an underscore). if , := fmt.Fprintf(, "import %s %s\n\n", .name, strconv.Quote()); != nil { return } } else { if , := fmt.Fprintf(, "import %s\n\n", strconv.Quote()); != nil { return } } } } else if len() > 1 { if , := fmt.Fprint(, "import (\n"); != nil { return } // We must sort the imports to ensure repeatable // source. := []string{} for := range { = append(, ) } sort.Strings() for , := range { := [] if .alias && != "C" { // "C" package should be rendered without alias even when used as an anonymous import // (e.g. should never have an underscore). if , := fmt.Fprintf(, "%s %s\n", .name, strconv.Quote()); != nil { return } } else { if , := fmt.Fprintf(, "%s\n", strconv.Quote()); != nil { return } } } if , := fmt.Fprint(, ")\n\n"); != nil { return } } if { for , := range .cgoPreamble { if := Comment().render(, , nil); != nil { return } if , := fmt.Fprint(, "\n"); != nil { return } } if , := fmt.Fprint(, "import \"C\"\n\n"); != nil { return } } return nil }