Skip to content

Commit

Permalink
Merge branch 'master' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
dave committed Oct 13, 2022
2 parents 132e596 + 6c02405 commit 5fa2894
Show file tree
Hide file tree
Showing 18 changed files with 799 additions and 372 deletions.
14 changes: 0 additions & 14 deletions .travis.yml

This file was deleted.

137 changes: 97 additions & 40 deletions README.md

Large diffs are not rendered by default.

99 changes: 61 additions & 38 deletions README.md.tpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
[![Build Status](https://travis-ci.org/dave/jennifer.svg?branch=master)](https://travis-ci.org/dave/jennifer) [![Go Report Card](https://goreportcard.com/badge/github.com/dave/jennifer)](https://goreportcard.com/report/github.com/dave/jennifer)
[![codecov](https://img.shields.io/badge/codecov-100%25-brightgreen.svg)](https://codecov.io/gh/dave/jennifer)
![stability-stable](https://img.shields.io/badge/stability-stable-brightgreen.svg)
[![Sourcegraph](https://sourcegraph.com/github.com/dave/jennifer/-/badge.svg)](https://sourcegraph.com/github.com/dave/jennifer?badge)

# Jennifer
Jennifer is a code generator for Go.
Expand All @@ -27,9 +24,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 @@ -40,25 +37,25 @@ 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
**Identifiers** [Keywords](#keywords) [Operators](#operators) [Braces](#braces) [Parentheses](#parentheses) [Control flow](#control-flow) [Collections](#collections) [Literals](#literals) [Comments](#comments) [Helpers](#helpers) [Misc](#misc) [File](#file)
**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](#file)

### Id
{{ "Id" | doc }}

{{ "ExampleId" | example }}

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

{{ "ExampleDot" | example }}

Expand All @@ -79,9 +76,9 @@ preferred.
{{ "ExampleList" | example }}

# Keywords
[Identifiers](#identifiers) **Keywords** [Operators](#operators) [Braces](#braces) [Parentheses](#parentheses) [Control flow](#control-flow) [Collections](#collections) [Literals](#literals) [Comments](#comments) [Helpers](#helpers) [Misc](#misc) [File](#file)
[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 All @@ -99,7 +96,7 @@ Built-in functions take a list of parameters and render them appropriately:
Special cases for [If, For](#if-for), [Interface, Struct](#interface-struct), [Switch, Case](#switch-select), [Return](#return) and [Map](#map) are explained below.

# Operators
[Identifiers](#identifiers) [Keywords](#keywords) **Operators** [Braces](#braces) [Parentheses](#parentheses) [Control flow](#control-flow) [Collections](#collections) [Literals](#literals) [Comments](#comments) [Helpers](#helpers) [Misc](#misc) [File](#file)
[Identifiers](#identifiers) [Keywords](#keywords) **Operators** [Braces](#braces) [Parentheses](#parentheses) [Control flow](#control-flow) [Collections](#collections) [Literals](#literals) [Comments](#comments) [Generics](#generics) [Helpers](#helpers) [Misc](#misc) [File](#file)

{{ "Op" | doc }}

Expand All @@ -112,9 +109,9 @@ Special cases for [If, For](#if-for), [Interface, Struct](#interface-struct), [S
{{ "ExampleOp_complex_conditions" | example }}

# Braces
[Identifiers](#identifiers) [Keywords](#keywords) [Operators](#operators) **Braces** [Parentheses](#parentheses) [Control flow](#control-flow) [Collections](#collections) [Literals](#literals) [Comments](#comments) [Helpers](#helpers) [Misc](#misc) [File](#file)
[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 @@ -133,7 +130,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 All @@ -145,7 +142,7 @@ by curly braces.
{{ "ExampleStruct" | example }}

# Parentheses
[Identifiers](#identifiers) [Keywords](#keywords) [Operators](#operators) [Braces](#braces) **Parentheses** [Control flow](#control-flow) [Collections](#collections) [Literals](#literals) [Comments](#comments) [Helpers](#helpers) [Misc](#misc) [File](#file)
[Identifiers](#identifiers) [Keywords](#keywords) [Operators](#operators) [Braces](#braces) **Parentheses** [Control flow](#control-flow) [Collections](#collections) [Literals](#literals) [Comments](#comments) [Generics](#generics) [Helpers](#helpers) [Misc](#misc) [File](#file)

Several methods output parenthesis, summarized below:

Expand Down Expand Up @@ -185,7 +182,7 @@ Several methods output parenthesis, summarized below:
{{ "ExampleAssert" | example }}

# Control flow
[Identifiers](#identifiers) [Keywords](#keywords) [Operators](#operators) [Braces](#braces) [Parentheses](#parentheses) **Control flow** [Collections](#collections) [Literals](#literals) [Comments](#comments) [Helpers](#helpers) [Misc](#misc) [File](#file)
[Identifiers](#identifiers) [Keywords](#keywords) [Operators](#operators) [Braces](#braces) [Parentheses](#parentheses) **Control flow** [Collections](#collections) [Literals](#literals) [Comments](#comments) [Generics](#generics) [Helpers](#helpers) [Misc](#misc) [File](#file)

### If, For
If and For render the keyword followed by a semicolon separated list.
Expand All @@ -205,7 +202,7 @@ Switch, Select, Case and Block are used to build switch or select statements:
{{ "ExampleReturn" | example }}

# Collections
[Identifiers](#identifiers) [Keywords](#keywords) [Operators](#operators) [Braces](#braces) [Parentheses](#parentheses) [Control flow](#control-flow) **Collections** [Literals](#literals) [Comments](#comments) [Helpers](#helpers) [Misc](#misc) [File](#file)
[Identifiers](#identifiers) [Keywords](#keywords) [Operators](#operators) [Braces](#braces) [Parentheses](#parentheses) [Control flow](#control-flow) **Collections** [Literals](#literals) [Comments](#comments) [Generics](#generics) [Helpers](#helpers) [Misc](#misc) [File](#file)

### Map
{{ "Map" | doc }}
Expand Down Expand Up @@ -239,7 +236,7 @@ Switch, Select, Case and Block are used to build switch or select statements:
Note: the items are ordered by key when rendered to ensure repeatable code.

# Literals
[Identifiers](#identifiers) [Keywords](#keywords) [Operators](#operators) [Braces](#braces) [Parentheses](#parentheses) [Control flow](#control-flow) [Collections](#collections) **Literals** [Comments](#comments) [Helpers](#helpers) [Misc](#misc) [File](#file)
[Identifiers](#identifiers) [Keywords](#keywords) [Operators](#operators) [Braces](#braces) [Parentheses](#parentheses) [Control flow](#control-flow) [Collections](#collections) **Literals** [Comments](#comments) [Generics](#generics) [Helpers](#helpers) [Misc](#misc) [File](#file)

### Lit
{{ "Lit" | doc }}
Expand All @@ -252,7 +249,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 @@ -263,7 +260,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 @@ -273,7 +270,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 All @@ -282,7 +279,7 @@ render rune and byte literals.
| `LitByte(byte(0x1))` | `byte(0x1)` |

# Comments
[Identifiers](#identifiers) [Keywords](#keywords) [Operators](#operators) [Braces](#braces) [Parentheses](#parentheses) [Control flow](#control-flow) [Collections](#collections) [Literals](#literals) **Comments** [Helpers](#helpers) [Misc](#misc) [File](#file)
[Identifiers](#identifiers) [Keywords](#keywords) [Operators](#operators) [Braces](#braces) [Parentheses](#parentheses) [Control flow](#control-flow) [Collections](#collections) [Literals](#literals) **Comments** [Generics](#generics) [Helpers](#helpers) [Misc](#misc) [File](#file)

### Comment
{{ "Comment[:2]" | doc }}
Expand All @@ -300,11 +297,34 @@ render rune and byte literals.

{{ "ExampleCommentf" | example }}

# Generics
[Identifiers](#identifiers) [Keywords](#keywords) [Operators](#operators) [Braces](#braces) [Parentheses](#parentheses) [Control flow](#control-flow) [Collections](#collections) [Literals](#literals) [Comments](#comments) **Generics** [Helpers](#helpers) [Misc](#misc) [File](#file)

It is hoped that with the introduction of generics with Go 1.18, the need to generate code
will be reduced. However, for the sake of completeness, we now support generics including
the `any` and `comparable` predeclared identifiers, and the `Types` and `Union` lists. To
emit the approximation (`~`) token, use `Op("~")`.

### Types

{{ "Types" | doc }}

### Union

{{ "Union" | doc }}

### Examples

{{ "ExampleGenericsTypesDefinition" | example }}
{{ "ExampleGenericsTypesUsage" | example }}
{{ "ExampleGenericsUnion" | example }}
{{ "ExampleGenericsApproximate" | example }}

# Helpers
[Identifiers](#identifiers) [Keywords](#keywords) [Operators](#operators) [Braces](#braces) [Parentheses](#parentheses) [Control flow](#control-flow) [Collections](#collections) [Literals](#literals) [Comments](#comments) **Helpers** [Misc](#misc) [File](#file)
[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 All @@ -324,7 +344,7 @@ functions that accept a func(*Group). Use for embedding logic.
{{ "ExampleDo" | example }}

# Misc
[Identifiers](#identifiers) [Keywords](#keywords) [Operators](#operators) [Braces](#braces) [Parentheses](#parentheses) [Control flow](#control-flow) [Collections](#collections) [Literals](#literals) [Comments](#comments) [Helpers](#helpers) **Misc** [File](#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** [File](#file)

### Tag
{{ "Tag" | doc }}
Expand All @@ -349,25 +369,25 @@ 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) [Helpers](#helpers) [Misc](#misc) **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**

{{ "File" | doc }}

Expand Down Expand Up @@ -401,7 +421,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 @@ -423,3 +443,6 @@ the import is separated, and preceded by the preamble.
{{ "File.PackagePrefix" | doc }}

{{ "ExampleFile_PackagePrefix" | example }}

### NoFormat
{{ "File.NoFormat" | doc }}
20 changes: 19 additions & 1 deletion genjen/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

var keywords = []string{"break", "default", "func", "select", "chan", "else", "const", "fallthrough", "type", "continue", "var", "goto", "defer", "go", "range"}

var identifiers = []string{"bool", "byte", "complex64", "complex128", "error", "float32", "float64", "int", "int8", "int16", "int32", "int64", "rune", "string", "uint", "uint8", "uint16", "uint32", "uint64", "uintptr", "true", "false", "iota", "nil", "err"}
var identifiers = []string{"bool", "byte", "complex64", "complex128", "error", "float32", "float64", "int", "int8", "int16", "int32", "int64", "rune", "string", "uint", "uint8", "uint16", "uint32", "uint64", "uintptr", "true", "false", "iota", "nil", "err", "any", "comparable"}

var groups = []struct {
name string // name of the function / method
Expand Down Expand Up @@ -304,4 +304,22 @@ var groups = []struct {
separator: ",",
parameters: []string{},
},
{
name: "Types",
comment: "renders a comma separated list enclosed by square brackets. Use for type parameters and constraints.",
variadic: true,
opening: "[",
closing: "]",
separator: ",",
parameters: []string{"types"},
},
{
name: "Union",
comment: "renders a pipe separated list. Use for union type constraints.",
variadic: true,
opening: "",
closing: "",
separator: "|",
parameters: []string{"types"},
},
}
3 changes: 3 additions & 0 deletions gennames/hints.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"go/build"
"io"
"os"
"os/exec"
"strings"

Expand Down Expand Up @@ -53,9 +54,11 @@ func getPackages(goListPath, filter string, standard, novendor bool) (map[string
}

cmd := exec.Command("go", "list", "-e", "-f", "{{ .Standard }} {{ .ImportPath }} {{ .Name }}", goListPath)
home, _ := os.UserHomeDir()
cmd.Env = []string{
fmt.Sprintf("GOPATH=%s", build.Default.GOPATH),
fmt.Sprintf("GOROOT=%s", build.Default.GOROOT),
fmt.Sprintf("HOME=%s", home),
}
if standard {
cmd.Dir = filepath.Join(build.Default.GOROOT, "src")
Expand Down
13 changes: 12 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
module github.com/dave/jennifer

go 1.15
go 1.19

require (
github.com/dave/astrid v0.0.0-20170323122508-8c2895878b14 // indirect
github.com/dave/brenda v1.1.0 // indirect
github.com/dave/courtney v0.3.0 // indirect
github.com/dave/gopackages v0.0.0-20170318123100-46e7023ec56e // indirect
github.com/dave/kerr v0.0.0-20170318121727-bc25dd6abe8e // indirect
github.com/dave/patsy v0.0.0-20210517141501-957256f50cba // indirect
github.com/dave/rebecca v0.9.1 // indirect
golang.org/x/tools v0.1.8 // indirect
)

0 comments on commit 5fa2894

Please sign in to comment.