Skip to content

Commit

Permalink
Merge pull request dave#5 from davelondon/stateful
Browse files Browse the repository at this point in the history
Block behaves like CaseBlock when following Case or Default
  • Loading branch information
dave authored Mar 3, 2017
2 parents 2a67594 + 2ae7854 commit 46354c4
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 137 deletions.
42 changes: 23 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,25 +174,27 @@ items and render them as follows:
| [Return](#return) | | `,` | | `return a, b` |
| [If](#if-for) | | `;` | | `if i, err := a(); err != nil { ... }` |
| [For](#if-for) | | `;` | | `for i := 0; i < 10; i++ { ... }` |
| [Switch](#switch-case-caseblock) | | `;` | | `switch a { ... }` |
| [Case](#switch-case-caseblock) | | `,` | | `case a, b: ...` |
| [Switch](#switch-case) | | `;` | | `switch a { ... }` |
| [Case](#switch-case) | | `,` | | `case a, b: ...` |
| [Interface](#interface-struct) | `{` | `\n` | `}` | `interface { ... }` |
| [Struct](#interface-struct) | `{` | `\n` | `}` | `struct { ... }` |
| [Map](#map) | `[` | | `]` | `map[string]` |

Groups accept a list of items and render them as follows:

| Group | Opening | Separator | Closing | Usage |
| ----------------- | ------------- | --------- | ------- | --------------------------------- |
| [Sel](#sel) | | `.` | | `foo.bar[0].baz()` |
| [List](#list) | | `,` | | `a, b := c()` |
| [Call](#call) | `(` | `,` | `)` | `fmt.Println(b, c)` |
| [Params](#params) | `(` | `,` | `)` | `func (a *A) Foo(i int) { ... }` |
| [Index](#index) | `[` | `:` | `]` | `a[1:2]` or `[]int{}` |
| [Values](#values) | `{` | `,` | `}` | `[]int{1, 2}` |
| [Block](#block) | `{` | `\n` | `}` | `func a() { ... }` |
| [Defs](#defs) | `(` | `\n` | `)` | `const ( ... )` |
| [CaseBlock](#switch-case-caseblock) | `:` | `\n` | | `case a: ...` |
| Group | Opening | Separator | Closing | Usage |
| ---------------------- | ------- | --------- | ------- | --------------------------------- |
| [Sel](#sel) | | `.` | | `foo.bar[0].baz()` |
| [List](#list) | | `,` | | `a, b := c()` |
| [Call](#call) | `(` | `,` | `)` | `fmt.Println(b, c)` |
| [Params](#params) | `(` | `,` | `)` | `func (a *A) Foo(i int) { ... }` |
| [Index](#index) | `[` | `:` | `]` | `a[1:2]` or `[]int{}` |
| [Values](#values) | `{` | `,` | `}` | `[]int{1, 2}` |
| [Defs](#defs) | `(` | `\n` | `)` | `const ( ... )` |
| [Block](#block) | `{` | `\n` | `}` | `func a() { ... }` |
| [Block](#switch-case)* | `:` | `\n` | | `switch a { case b: ... }` |

<nowiki>*</nowiki> Block: A special case applies when used directly after Case or Default, where it renders a statement list preceded by a colon. This allows use in switch and select statements.

These groups accept a single item:

Expand Down Expand Up @@ -265,21 +267,21 @@ fmt.Printf("%#v", c)
// }
```

### Switch, Case, CaseBlock
Switch, Case and CaseBlock are used to build switch statements:
### Switch, Case
Switch, Case and Block are used to build switch statements:

```go
c := Switch(Id("a")).Block(
Case(Lit("1")).CaseBlock(
Case(Lit("1")).Block(
Return(Lit(1)),
),
Case(Lit("2"), Lit("3")).CaseBlock(
Case(Lit("2"), Lit("3")).Block(
Return(Lit(2)),
),
Case(Lit("4")).CaseBlock(
Case(Lit("4")).Block(
Fallthrough(),
),
Default().CaseBlock(
Default().Block(
Return(Lit(3)),
),
)
Expand Down Expand Up @@ -471,6 +473,8 @@ fmt.Printf("%#v", c)
// }
```

A special case applies when used directly after Case or Default, where it renders a statement list preceded by a colon. This allows use in switch and select statements. [See example](#switch-case).

### Defs
Defs renders a statement list enclosed in parenthesis. Use for definition lists.

Expand Down
36 changes: 20 additions & 16 deletions README.md.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -98,25 +98,27 @@ items and render them as follows:
| [Return](#return) | | `,` | | `return a, b` |
| [If](#if-for) | | `;` | | `if i, err := a(); err != nil { ... }` |
| [For](#if-for) | | `;` | | `for i := 0; i < 10; i++ { ... }` |
| [Switch](#switch-case-caseblock) | | `;` | | `switch a { ... }` |
| [Case](#switch-case-caseblock) | | `,` | | `case a, b: ...` |
| [Switch](#switch-case) | | `;` | | `switch a { ... }` |
| [Case](#switch-case) | | `,` | | `case a, b: ...` |
| [Interface](#interface-struct) | `{` | `\n` | `}` | `interface { ... }` |
| [Struct](#interface-struct) | `{` | `\n` | `}` | `struct { ... }` |
| [Map](#map) | `[` | | `]` | `map[string]` |

Groups accept a list of items and render them as follows:

| Group | Opening | Separator | Closing | Usage |
| ----------------- | ------------- | --------- | ------- | --------------------------------- |
| [Sel](#sel) | | `.` | | `foo.bar[0].baz()` |
| [List](#list) | | `,` | | `a, b := c()` |
| [Call](#call) | `(` | `,` | `)` | `fmt.Println(b, c)` |
| [Params](#params) | `(` | `,` | `)` | `func (a *A) Foo(i int) { ... }` |
| [Index](#index) | `[` | `:` | `]` | `a[1:2]` or `[]int{}` |
| [Values](#values) | `{` | `,` | `}` | `[]int{1, 2}` |
| [Block](#block) | `{` | `\n` | `}` | `func a() { ... }` |
| [Defs](#defs) | `(` | `\n` | `)` | `const ( ... )` |
| [CaseBlock](#switch-case-caseblock) | `:` | `\n` | | `case a: ...` |
| Group | Opening | Separator | Closing | Usage |
| ---------------------- | ------- | --------- | ------- | --------------------------------- |
| [Sel](#sel) | | `.` | | `foo.bar[0].baz()` |
| [List](#list) | | `,` | | `a, b := c()` |
| [Call](#call) | `(` | `,` | `)` | `fmt.Println(b, c)` |
| [Params](#params) | `(` | `,` | `)` | `func (a *A) Foo(i int) { ... }` |
| [Index](#index) | `[` | `:` | `]` | `a[1:2]` or `[]int{}` |
| [Values](#values) | `{` | `,` | `}` | `[]int{1, 2}` |
| [Defs](#defs) | `(` | `\n` | `)` | `const ( ... )` |
| [Block](#block) | `{` | `\n` | `}` | `func a() { ... }` |
| [Block](#switch-case)* | `:` | `\n` | | `switch a { case b: ... }` |

<nowiki>*</nowiki> Block: {{ "Block[2:]" | doc }}

These groups accept a single item:

Expand Down Expand Up @@ -145,8 +147,8 @@ by curly braces.

{{ "ExampleStruct" | example }}

### Switch, Case, CaseBlock
Switch, Case and CaseBlock are used to build switch statements:
### Switch, Case
Switch, Case and Block are used to build switch statements:

{{ "ExampleSwitch" | example }}

Expand Down Expand Up @@ -204,12 +206,14 @@ If and For render the keyword followed by a semicolon separated list.
{{ "ExampleValues" | example }}

### Block
{{ "Block" | doc }}
{{ "Block[:2]" | doc }}

{{ "ExampleBlock" | example }}

{{ "ExampleBlock_if" | example }}

{{ "Block[2:]" | doc }} [See example](#switch-case).

### Defs
{{ "Defs" | doc }}

Expand Down
11 changes: 1 addition & 10 deletions genjen/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var groups = []struct {
},
{
name: "Block",
comment: "renders a statement list enclosed by curly braces. Use for code blocks.",
comment: "renders a statement list enclosed by curly braces. Use for code blocks. A special case applies when used directly after Case or Default, where it renders a statement list preceded by a colon. This allows use in switch and select statements.",
variadic: true,
opening: "{",
closing: "}",
Expand Down Expand Up @@ -86,15 +86,6 @@ var groups = []struct {
separator: ",",
parameters: []string{"params"},
},
{
name: "CaseBlock",
comment: "renders a statement list preceded by a colon. Use to build switch / select statements.",
variadic: true,
opening: ":",
closing: "",
separator: "\n",
parameters: []string{"statements"},
},
{
name: "Assert",
comment: "renders a period followed by a single item enclosed by parenthesis. Use for type assertions.",
Expand Down
4 changes: 4 additions & 0 deletions genjen/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func render(w io.Writer) error {
func (s *Statement) <name>(<funcParams>) *Statement {
g := &Group{
items: []Code{<paramNames>}|<paramNames[0]>,
name: "<name>",
open: "<opening>",
close: "<closing>",
separator: "<separator>",
Expand All @@ -74,6 +75,7 @@ func render(w io.Writer) error {
})
}
}),
Id("name"): Lit(strings.ToLower(b.name)),
Id("open"): Lit(b.opening),
Id("close"): Lit(b.closing),
Id("separator"): Lit(b.separator),
Expand All @@ -100,6 +102,7 @@ func render(w io.Writer) error {
// <funcComment>
func (s *Statement) <funcName>(f func(*Group)) *Statement {
g := &Group{
name: "<name>",
open: "<opening>",
close: "<closing>",
separator: "<separator>",
Expand All @@ -116,6 +119,7 @@ func render(w io.Writer) error {
funcFuncParams...,
).Op("*").Id("Statement").Block(
Id("g").Op(":=").Op("&").Id("Group").Dict(map[Code]Code{
Id("name"): Lit(strings.ToLower(b.name)),
Id("open"): Lit(b.opening),
Id("close"): Lit(b.closing),
Id("separator"): Lit(b.separator),
Expand Down
2 changes: 1 addition & 1 deletion jen/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (c comment) isNull(f *File) bool {
return false
}

func (c comment) render(f *File, w io.Writer) error {
func (c comment) render(f *File, w io.Writer, s *Statement) error {
if strings.HasPrefix(c.comment, "//") || strings.HasPrefix(c.comment, "/*") {
// automatic formatting disabled.
if _, err := w.Write([]byte(c.comment)); err != nil {
Expand Down
28 changes: 14 additions & 14 deletions jen/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,10 @@ func ExampleCap() {

func ExampleCase() {
c := Switch(Id("person")).Block(
Case(Id("John"), Id("Peter")).CaseBlock(
Case(Id("John"), Id("Peter")).Block(
Return(Lit("male")),
),
Case(Id("Gill")).CaseBlock(
Case(Id("Gill")).Block(
Return(Lit("female")),
),
)
Expand All @@ -182,12 +182,12 @@ func ExampleCase() {
// }
}

func ExampleCaseBlock() {
func ExampleBlock_case() {
c := Select().Block(
Case(Op("<-").Id("done")).CaseBlock(
Case(Op("<-").Id("done")).Block(
Return(Nil()),
),
Case(List(Err(), Id("open")).Op(":=").Op("<-").Id("fail")).CaseBlock(
Case(List(Err(), Id("open")).Op(":=").Op("<-").Id("fail")).Block(
If(Op("!").Id("open")).Block(
Return(Err()),
),
Expand All @@ -205,13 +205,13 @@ func ExampleCaseBlock() {
// }
}

func ExampleCaseBlockFunc() {
func ExampleBlockFunc_case() {
preventExitOnError := true
c := Select().Block(
Case(Op("<-").Id("done")).CaseBlock(
Case(Op("<-").Id("done")).Block(
Return(Nil()),
),
Case(Err().Op(":=").Op("<-").Id("fail")).CaseBlockFunc(func(g *Group) {
Case(Err().Op(":=").Op("<-").Id("fail")).BlockFunc(func(g *Group) {
if !preventExitOnError {
g.Return(Err())
} else {
Expand All @@ -238,15 +238,15 @@ func ExampleCaseFunc() {
if samIsMale {
g.Id("Sam")
}
}).CaseBlock(
}).Block(
Return(Lit("male")),
),
CaseFunc(func(g *Group) {
g.Id("Gill")
if !samIsMale {
g.Id("Sam")
}
}).CaseBlock(
}).Block(
Return(Lit("female")),
),
)
Expand Down Expand Up @@ -745,16 +745,16 @@ func ExampleErr() {

func ExampleSwitch() {
c := Switch(Id("a")).Block(
Case(Lit("1")).CaseBlock(
Case(Lit("1")).Block(
Return(Lit(1)),
),
Case(Lit("2"), Lit("3")).CaseBlock(
Case(Lit("2"), Lit("3")).Block(
Return(Lit(2)),
),
Case(Lit("4")).CaseBlock(
Case(Lit("4")).Block(
Fallthrough(),
),
Default().CaseBlock(
Default().Block(
Return(Lit(3)),
),
)
Expand Down
Loading

0 comments on commit 46354c4

Please sign in to comment.