Skip to content

Commit

Permalink
NoFormat tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
dave committed Oct 13, 2022
1 parent 290fbca commit 0b606cf
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 52 deletions.
50 changes: 27 additions & 23 deletions README.md
Expand Up @@ -38,9 +38,9 @@ func main() {
go get -u github.com/dave/jennifer/jen
```

### Need help?
If you get stuck, have a question, would like a code review, or just want a
chat: I'm happy to help! Feel free to open an issue, email me or mention @dave
### Need help?
If you get stuck, have a question, would like a code review, or just want a
chat: I'm happy to help! Feel free to open an issue, email me or mention @dave
in your PR.

### Examples
Expand All @@ -51,7 +51,7 @@ Jennifer has a comprehensive suite of examples - see [godoc](https://godoc.org/g
* [go-contentful-generator](https://github.com/nicolai86/go-contentful-generator)

### Rendering
For testing, a File or Statement can be rendered with the fmt package
For testing, a File or Statement can be rendered with the fmt package
using the %#v verb.

```go
Expand All @@ -61,8 +61,8 @@ fmt.Printf("%#v", c)
// a("b")
```

This is not recommended for use in production because any error will cause a
panic. For production use, [File.Render](#render) or [File.Save](#save) are
This is not recommended for use in production because any error will cause a
panic. For production use, [File.Render](#render) or [File.Save](#save) are
preferred.

# Identifiers
Expand All @@ -83,7 +83,7 @@ fmt.Printf("%#v", c)
```

### Dot
Dot renders a period followed by an identifier. Use for fields and selectors.
Dot renders a period followed by an identifier. Use for fields and selectors.

```go
c := Qual("a.b/c", "Foo").Call().Dot("Bar").Index(Lit(0)).Dot("Baz")
Expand Down Expand Up @@ -149,7 +149,7 @@ fmt.Printf("%#v", c)
# Keywords
[Identifiers](#identifiers) **Keywords** [Operators](#operators) [Braces](#braces) [Parentheses](#parentheses) [Control flow](#control-flow) [Collections](#collections) [Literals](#literals) [Comments](#comments) [Generics](#generics) [Helpers](#helpers) [Misc](#misc) [File](#file)

Simple keywords, predeclared identifiers and built-in functions are self
Simple keywords, predeclared identifiers and built-in functions are self
explanatory:

| Construct | Name |
Expand Down Expand Up @@ -208,7 +208,7 @@ fmt.Printf("%#v", c)
# Braces
[Identifiers](#identifiers) [Keywords](#keywords) [Operators](#operators) **Braces** [Parentheses](#parentheses) [Control flow](#control-flow) [Collections](#collections) [Literals](#literals) [Comments](#comments) [Generics](#generics) [Helpers](#helpers) [Misc](#misc) [File](#file)

Several methods render curly braces, summarized below:
Several methods render curly braces, summarized below:

| Name | Prefix | Separator | Example |
| ------------------------------ | ------------ | --------- | -------------------------------------|
Expand Down Expand Up @@ -249,7 +249,7 @@ fmt.Printf("%#v", c)
A special case applies when used directly after Case or Default, where the braces are omitted. This allows use in switch and select statements. [See example](#switch-select).

### Interface, Struct
Interface and Struct render the keyword followed by a statement list enclosed
Interface and Struct render the keyword followed by a statement list enclosed
by curly braces.

```go
Expand Down Expand Up @@ -581,7 +581,7 @@ fmt.Printf("%#v", c)
// a := 2
```

For the default constant types (bool, int, float64, string, complex128), Lit
For the default constant types (bool, int, float64, string, complex128), Lit
will render the untyped constant.

| Code | Output |
Expand All @@ -592,7 +592,7 @@ will render the untyped constant.
| `Lit("foo")` | `"foo"` |
| `Lit(0 + 1i)` | `(0 + 1i)` |

For all other built-in types (float32, int8, int16, int32, int64, uint, uint8,
For all other built-in types (float32, int8, int16, int32, int64, uint, uint8,
uint16, uint32, uint64, uintptr, complex64), Lit will also render the type.

| Code | Output |
Expand All @@ -602,7 +602,7 @@ uint16, uint32, uint64, uintptr, complex64), Lit will also render the type.
| `Lit(uint8(0x1))` | `uint8(0x1)` |
| `Lit(complex64(0 + 1i))` | `complex64(0 + 1i)` |

The built-in alias types byte and rune need a special case. LitRune and LitByte
The built-in alias types byte and rune need a special case. LitRune and LitByte
render rune and byte literals.

| Code | Output |
Expand Down Expand Up @@ -726,7 +726,7 @@ fmt.Printf("%#v", c)
[Identifiers](#identifiers) [Keywords](#keywords) [Operators](#operators) [Braces](#braces) [Parentheses](#parentheses) [Control flow](#control-flow) [Collections](#collections) [Literals](#literals) [Comments](#comments) [Generics](#generics) **Helpers** [Misc](#misc) [File](#file)

### Func methods
All constructs that accept a variadic list of items are paired with GroupFunc
All constructs that accept a variadic list of items are paired with GroupFunc
functions that accept a func(*Group). Use for embedding logic.

```go
Expand Down Expand Up @@ -853,7 +853,7 @@ fmt.Printf("%#v", c)
Line inserts a blank line.

### Clone
Be careful when passing *Statement. Consider the following...
Be careful when passing *Statement. Consider the following...

```go
a := Id("a")
Expand All @@ -869,9 +869,9 @@ fmt.Printf("%#v", c)
// }
```

Id("a") returns a *Statement, which the Call() method appends to twice. To
Id("a") returns a *Statement, which the Call() method appends to twice. To
avoid this, use Clone. Clone makes a copy of the Statement, so further tokens can be appended
without affecting the original.
without affecting the original.

```go
a := Id("a")
Expand All @@ -888,10 +888,10 @@ fmt.Printf("%#v", c)
```

### Cgo
The cgo "C" pseudo-package is a special case, and always renders without a package alias. The
import can be added with `Qual`, `Anon` or by supplying a preamble. The preamble is added with
`File.CgoPreamble` which has the same semantics as [Comment](#comments). If a preamble is provided,
the import is separated, and preceded by the preamble.
The cgo "C" pseudo-package is a special case, and always renders without a package alias. The
import can be added with `Qual`, `Anon` or by supplying a preamble. The preamble is added with
`File.CgoPreamble` which has the same semantics as [Comment](#comments). If a preamble is provided,
the import is separated, and preceded by the preamble.

```go
f := NewFile("a")
Expand Down Expand Up @@ -928,7 +928,7 @@ fmt.Printf("%#v", f)
// C.myprint(cs)
// C.free(unsafe.Pointer(cs))
// }
```
```

# File
[Identifiers](#identifiers) [Keywords](#keywords) [Operators](#operators) [Braces](#braces) [Parentheses](#parentheses) [Control flow](#control-flow) [Collections](#collections) [Literals](#literals) [Comments](#comments) [Generics](#generics) [Helpers](#helpers) [Misc](#misc) **File**
Expand Down Expand Up @@ -1029,7 +1029,7 @@ fmt.Printf("%#v", f)

### ImportNames
ImportNames allows multiple names to be imported as a map. Use the [gennames](gennames) command to
automatically generate a go file containing a map of a selection of package names.
automatically generate a go file containing a map of a selection of package names.

### ImportAlias
ImportAlias provides the alias for a package path that should be used in the import block. A
Expand Down Expand Up @@ -1108,3 +1108,7 @@ fmt.Printf("%#v", f)
// pkg_d.E()
// }
```

### NoFormat
NoFormat can be set to true to disable formatting of the generated source. This may be useful
when performance is critical, and readable code is not required.
49 changes: 26 additions & 23 deletions README.md.tpl
Expand Up @@ -26,9 +26,9 @@ Output:
go get -u github.com/dave/jennifer/jen
```

### Need help?
If you get stuck, have a question, would like a code review, or just want a
chat: I'm happy to help! Feel free to open an issue, email me or mention @dave
### Need help?
If you get stuck, have a question, would like a code review, or just want a
chat: I'm happy to help! Feel free to open an issue, email me or mention @dave
in your PR.

### Examples
Expand All @@ -39,13 +39,13 @@ Jennifer has a comprehensive suite of examples - see [godoc](https://godoc.org/g
* [go-contentful-generator](https://github.com/nicolai86/go-contentful-generator)

### Rendering
For testing, a File or Statement can be rendered with the fmt package
For testing, a File or Statement can be rendered with the fmt package
using the %#v verb.

{{ "ExampleCall_fmt" | example }}

This is not recommended for use in production because any error will cause a
panic. For production use, [File.Render](#render) or [File.Save](#save) are
This is not recommended for use in production because any error will cause a
panic. For production use, [File.Render](#render) or [File.Save](#save) are
preferred.

# Identifiers
Expand All @@ -57,7 +57,7 @@ preferred.
{{ "ExampleId" | example }}

### Dot
{{ "Dot" | doc }}
{{ "Dot" | doc }}

{{ "ExampleDot" | example }}

Expand All @@ -80,7 +80,7 @@ preferred.
# Keywords
[Identifiers](#identifiers) **Keywords** [Operators](#operators) [Braces](#braces) [Parentheses](#parentheses) [Control flow](#control-flow) [Collections](#collections) [Literals](#literals) [Comments](#comments) [Generics](#generics) [Helpers](#helpers) [Misc](#misc) [File](#file)

Simple keywords, predeclared identifiers and built-in functions are self
Simple keywords, predeclared identifiers and built-in functions are self
explanatory:

| Construct | Name |
Expand Down Expand Up @@ -113,7 +113,7 @@ Special cases for [If, For](#if-for), [Interface, Struct](#interface-struct), [S
# Braces
[Identifiers](#identifiers) [Keywords](#keywords) [Operators](#operators) **Braces** [Parentheses](#parentheses) [Control flow](#control-flow) [Collections](#collections) [Literals](#literals) [Comments](#comments) [Generics](#generics) [Helpers](#helpers) [Misc](#misc) [File](#file)

Several methods render curly braces, summarized below:
Several methods render curly braces, summarized below:

| Name | Prefix | Separator | Example |
| ------------------------------ | ------------ | --------- | -------------------------------------|
Expand All @@ -132,7 +132,7 @@ Several methods render curly braces, summarized below:
{{ "Block[2:]" | doc }} [See example](#switch-select).

### Interface, Struct
Interface and Struct render the keyword followed by a statement list enclosed
Interface and Struct render the keyword followed by a statement list enclosed
by curly braces.

{{ "ExampleInterface_empty" | example }}
Expand Down Expand Up @@ -251,7 +251,7 @@ Note: the items are ordered by key when rendered to ensure repeatable code.

{{ "ExampleLitFunc" | example }}

For the default constant types (bool, int, float64, string, complex128), Lit
For the default constant types (bool, int, float64, string, complex128), Lit
will render the untyped constant.

| Code | Output |
Expand All @@ -262,7 +262,7 @@ will render the untyped constant.
| `Lit("foo")` | `"foo"` |
| `Lit(0 + 1i)` | `(0 + 1i)` |

For all other built-in types (float32, int8, int16, int32, int64, uint, uint8,
For all other built-in types (float32, int8, int16, int32, int64, uint, uint8,
uint16, uint32, uint64, uintptr, complex64), Lit will also render the type.

| Code | Output |
Expand All @@ -272,7 +272,7 @@ uint16, uint32, uint64, uintptr, complex64), Lit will also render the type.
| `Lit(uint8(0x1))` | `uint8(0x1)` |
| `Lit(complex64(0 + 1i))` | `complex64(0 + 1i)` |

The built-in alias types byte and rune need a special case. LitRune and LitByte
The built-in alias types byte and rune need a special case. LitRune and LitByte
render rune and byte literals.

| Code | Output |
Expand Down Expand Up @@ -326,7 +326,7 @@ emit the approximation (`~`) token, use `Op("~")`.
[Identifiers](#identifiers) [Keywords](#keywords) [Operators](#operators) [Braces](#braces) [Parentheses](#parentheses) [Control flow](#control-flow) [Collections](#collections) [Literals](#literals) [Comments](#comments) [Generics](#generics) **Helpers** [Misc](#misc) [File](#file)

### Func methods
All constructs that accept a variadic list of items are paired with GroupFunc
All constructs that accept a variadic list of items are paired with GroupFunc
functions that accept a func(*Group). Use for embedding logic.

{{ "ExampleValuesFunc" | example }}
Expand Down Expand Up @@ -371,22 +371,22 @@ In lists, nil will produce the same effect.
{{ "Line" | doc }}

### Clone
Be careful when passing *Statement. Consider the following...
Be careful when passing *Statement. Consider the following...

{{ "ExampleStatement_Clone_broken" | example }}

Id("a") returns a *Statement, which the Call() method appends to twice. To
avoid this, use Clone. {{ "Statement.Clone" | doc }}
Id("a") returns a *Statement, which the Call() method appends to twice. To
avoid this, use Clone. {{ "Statement.Clone" | doc }}

{{ "ExampleStatement_Clone_fixed" | example }}

### Cgo
The cgo "C" pseudo-package is a special case, and always renders without a package alias. The
import can be added with `Qual`, `Anon` or by supplying a preamble. The preamble is added with
`File.CgoPreamble` which has the same semantics as [Comment](#comments). If a preamble is provided,
the import is separated, and preceded by the preamble.
The cgo "C" pseudo-package is a special case, and always renders without a package alias. The
import can be added with `Qual`, `Anon` or by supplying a preamble. The preamble is added with
`File.CgoPreamble` which has the same semantics as [Comment](#comments). If a preamble is provided,
the import is separated, and preceded by the preamble.

{{ "ExampleFile_CgoPreamble" | example }}
{{ "ExampleFile_CgoPreamble" | example }}

# File
[Identifiers](#identifiers) [Keywords](#keywords) [Operators](#operators) [Braces](#braces) [Parentheses](#parentheses) [Control flow](#control-flow) [Collections](#collections) [Literals](#literals) [Comments](#comments) [Generics](#generics) [Helpers](#helpers) [Misc](#misc) **File**
Expand Down Expand Up @@ -423,7 +423,7 @@ the import is separated, and preceded by the preamble.
{{ "ExampleFile_ImportName" | example }}

### ImportNames
{{ "File.ImportNames" | doc }}
{{ "File.ImportNames" | doc }}

### ImportAlias
{{ "File.ImportAlias" | doc }}
Expand All @@ -445,3 +445,6 @@ the import is separated, and preceded by the preamble.
{{ "File.PackagePrefix" | doc }}

{{ "ExampleFile_PackagePrefix" | example }}

### NoFormat
{{ "File.NoFormat" | doc }}
13 changes: 13 additions & 0 deletions jen/examples_test.go
Expand Up @@ -1691,3 +1691,16 @@ func ExampleFile_PackagePrefix() {
// pkg_d.E()
// }
}

func ExampleFile_NoFormat() {
f := NewFile("main")
f.NoFormat = true

f.Func().Id("main").Params().Block(
Qual("fmt", "Println").Call(Lit("foo")),
)
fmt.Printf("%q", fmt.Sprintf("%#v", f)) // Special case because Go Examples don't handle multiple newlines well.

// Output:
// "package main\n\nimport \"fmt\"\n\n\nfunc main () {\nfmt.Println (\"foo\")\n}"
}
3 changes: 2 additions & 1 deletion jen/file.go
Expand Up @@ -59,7 +59,8 @@ type File struct {
comments []string
headers []string
cgoPreamble []string
// NoFormat can be set to true to disable formatting of the generated source.
// NoFormat can be set to true to disable formatting of the generated source. This may be useful
// when performance is critical, and readable code is not required.
NoFormat bool
// If you're worried about generated package aliases conflicting with local variable names, you
// can set a prefix here. Package foo becomes {prefix}_foo.
Expand Down
12 changes: 7 additions & 5 deletions jen/jen.go
Expand Up @@ -77,15 +77,17 @@ func (f *File) Render(w io.Writer) error {
if _, err := source.Write(body.Bytes()); err != nil {
return err
}
finalSource := source.Bytes()
if !f.NoFormat {
formatted, err := format.Source(source.Bytes())
var output []byte
if f.NoFormat {
output = source.Bytes()
} else {
var err error
output, err = format.Source(source.Bytes())
if err != nil {
return fmt.Errorf("Error %s while formatting source:\n%s", err, source.String())
}
finalSource = formatted
}
if _, err := w.Write(finalSource); err != nil {
if _, err := w.Write(output); err != nil {
return err
}
return nil
Expand Down

0 comments on commit 0b606cf

Please sign in to comment.