Skip to content

Commit

Permalink
Adds Dot, removes Sel
Browse files Browse the repository at this point in the history
  • Loading branch information
dave committed Mar 9, 2017
1 parent 46354c4 commit 4a54cd6
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 115 deletions.
30 changes: 12 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ go get -u github.com/davelondon/jennifer/jen
```

# Examples
Jennifer has a comprahensive suite of examples - [see godoc.org](https://godoc.org/github.com/davelondon/jennifer/jen#pkg-examples) for an index.
Jennifer has a comprehensive suite of examples - [see godoc.org](https://godoc.org/github.com/davelondon/jennifer/jen#pkg-examples) for an index.

The code that powers jennifer is generated by jennifer itself, see the
[genjen package](https://github.com/davelondon/jennifer/tree/master/genjen) -
Expand All @@ -62,7 +62,7 @@ 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.

# Id
# Id, Dot
Id renders an identifier.

```go
Expand All @@ -76,6 +76,15 @@ fmt.Printf("%#v", c)
// }
```

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")
fmt.Printf("%#v", c)
// Output:
// c.Foo().Bar[0].Baz
```

# Qual
Qual renders a qualified identifier.

Expand Down Expand Up @@ -184,7 +193,6 @@ 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) { ... }` |
Expand Down Expand Up @@ -353,20 +361,6 @@ fmt.Printf("%#v", c)

# Groups

### Sel
Sel renders a period separated list. Use for a chain of selectors.

```go
c := Sel(
Qual("a.b/c", "Foo").Call(),
Id("Bar").Index(Lit(0)),
Id("Baz"),
)
fmt.Printf("%#v", c)
// Output:
// c.Foo().Bar[0].Baz
```

### List
List renders a comma separated list. Use for multiple return functions.

Expand Down Expand Up @@ -860,4 +854,4 @@ fmt.Printf("%#v", c)
// a()
// a()
// }
```
```
12 changes: 5 additions & 7 deletions README.md.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,15 @@ 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.

# Id
# Id, Dot
{{ "Id" | doc }}

{{ "ExampleId" | example }}

{{ "Dot" | doc }}

{{ "ExampleDot" | example }}

# Qual
{{ "Qual[0]" | doc }}

Expand Down Expand Up @@ -108,7 +112,6 @@ 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) { ... }` |
Expand Down Expand Up @@ -171,11 +174,6 @@ If and For render the keyword followed by a semicolon separated list.

# Groups

### Sel
{{ "Sel" | doc }}

{{ "ExampleSel" | example }}

### List
{{ "List" | doc }}

Expand Down
9 changes: 0 additions & 9 deletions genjen/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,6 @@ var groups = []struct {
separator: ",",
parameters: []string{"cases"},
},
{
name: "Sel",
comment: "renders a period separated list. Use for a chain of selectors.",
variadic: true,
opening: "",
closing: "",
separator: ".",
parameters: []string{"selectors"},
},
{
name: "Append",
comment: "renders the append built-in function.",
Expand Down
9 changes: 2 additions & 7 deletions genjen/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,7 @@ func addFunctionAndGroupMethod(
*/
file.Add(comment)
file.Func().Id(name).Params(funcParams...).Op("*").Id("Statement").Block(
Return(
Sel(
Id("newStatement").Call(),
Id(name).Call(callParams...),
),
),
Return(Id("newStatement").Call().Dot(name).Call(callParams...)),
)
/*
// <comment>
Expand All @@ -235,7 +230,7 @@ func addFunctionAndGroupMethod(
Id("g").Op("*").Id("Group"),
).Id(name).Params(funcParams...).Op("*").Id("Statement").Block(
Id("s").Op(":=").Id(name).Params(callParams...),
Sel(Id("g"), Id("items")).Op("=").Append(Sel(Id("g"), Id("items")), Id("s")),
Id("g").Dot("items").Op("=").Append(Id("g").Dot("items"), Id("s")),
Return(Id("s")),
)
}
12 changes: 3 additions & 9 deletions jen/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,6 @@ func Recover(c ...Code) *Statement
func Return(c ...Code) *Statement
func ReturnFunc(f func(*Group)) *Statement
func Rune() *Statement
func Sel(c ...Code) *Statement
func SelFunc(f func(*Group)) *Statement
func Select() *Statement
func String() *Statement
func Struct() *Statement
Expand Down Expand Up @@ -656,12 +654,8 @@ func ExampleLitFunc() {
// a := 2
}

func ExampleSel() {
c := Sel(
Qual("a.b/c", "Foo").Call(),
Id("Bar").Index(Lit(0)),
Id("Baz"),
)
func ExampleDot() {
c := Qual("a.b/c", "Foo").Call().Dot("Bar").Index(Lit(0)).Dot("Baz")
fmt.Printf("%#v", c)
// Output:
// c.Foo().Bar[0].Baz
Expand Down Expand Up @@ -985,7 +979,7 @@ func ExampleId_local() {
}

func ExampleId_select() {
c := Sel(Id("a"), Id("b"), Id("c")).Call()
c := Id("a").Dot("b").Dot("c").Call()
fmt.Printf("%#v", c)
// Output:
// a.b.c()
Expand Down
50 changes: 0 additions & 50 deletions jen/generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -779,56 +779,6 @@ func (s *Statement) CaseFunc(f func(*Group)) *Statement {
return s
}

// Sel renders a period separated list. Use for a chain of selectors.
func Sel(selectors ...Code) *Statement {
return newStatement().Sel(selectors...)
}

// Sel renders a period separated list. Use for a chain of selectors.
func (g *Group) Sel(selectors ...Code) *Statement {
s := Sel(selectors...)
g.items = append(g.items, s)
return s
}

// Sel renders a period separated list. Use for a chain of selectors.
func (s *Statement) Sel(selectors ...Code) *Statement {
g := &Group{
close: "",
items: selectors,
name: "sel",
open: "",
separator: ".",
}
*s = append(*s, g)
return s
}

// SelFunc renders a period separated list. Use for a chain of selectors.
func SelFunc(f func(*Group)) *Statement {
return newStatement().SelFunc(f)
}

// SelFunc renders a period separated list. Use for a chain of selectors.
func (g *Group) SelFunc(f func(*Group)) *Statement {
s := SelFunc(f)
g.items = append(g.items, s)
return s
}

// SelFunc renders a period separated list. Use for a chain of selectors.
func (s *Statement) SelFunc(f func(*Group)) *Statement {
g := &Group{
close: "",
name: "sel",
open: "",
separator: ".",
}
f(g)
*s = append(*s, g)
return s
}

// Append renders the append built-in function.
func Append(args ...Code) *Statement {
return newStatement().Append(args...)
Expand Down
10 changes: 5 additions & 5 deletions jen/jen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,12 @@ var cases = []tc{
},
{
desc: `field`,
code: Sel(Id("a"), Id("b")),
code: Id("a").Dot("b"),
expect: `a.b`,
},
{
desc: `method`,
code: Sel(Id("a"), Id("b")).Call(Id("c"), Id("d")),
code: Id("a").Dot("b").Call(Id("c"), Id("d")),
expect: `a.b(c, d)`,
},
{
Expand Down Expand Up @@ -204,13 +204,13 @@ var cases = []tc{
expect: `a := "b"`,
},
{
desc: `sel`,
code: Sel(Id("a"), Id("b"), Id("c")),
desc: `dot`,
code: Id("a").Dot("b").Dot("c"),
expect: `a.b.c`,
},
{
desc: `do`,
code: Sel(Id("a"), Do(func(s *Statement) { s.Id("b") })),
code: Id("a").Do(func(s *Statement) { s.Dot("b") }),
expect: `a.b`,
},
{
Expand Down
52 changes: 42 additions & 10 deletions jen/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (t token) render(f *File, w io.Writer, s *Statement) error {
if _, err := w.Write([]byte(fmt.Sprintf("%#v", t.content))); err != nil {
return err
}
case keywordToken, operatorToken, layoutToken:
case keywordToken, operatorToken, layoutToken, delimiterToken:
if _, err := w.Write([]byte(fmt.Sprintf("%s", t.content))); err != nil {
return err
}
Expand Down Expand Up @@ -130,6 +130,32 @@ func (s *Statement) Op(op string) *Statement {
return s
}

// Dot renders a period followed by an identifier. Use for fields and selectors.
func Dot(name string) *Statement {
return newStatement().Dot(name)
}

// Dot renders a period followed by an identifier. Use for fields and selectors.
func (g *Group) Dot(name string) *Statement {
s := Dot(name)
g.items = append(g.items, s)
return s
}

// Dot renders a period followed by an identifier. Use for fields and selectors.
func (s *Statement) Dot(name string) *Statement {
d := token{
typ: delimiterToken,
content: ".",
}
t := token{
typ: identifierToken,
content: name,
}
*s = append(*s, d, t)
return s
}

// Id renders an identifier.
func Id(name string) *Statement {
return newStatement().Id(name)
Expand Down Expand Up @@ -172,16 +198,22 @@ func (g *Group) Qual(path, name string) *Statement {
// used with a File. If the path matches the local path, the package name is
// omitted. If package names conflict they are automatically renamed.
func (s *Statement) Qual(path, name string) *Statement {
g := Sel(
token{
typ: packageToken,
content: path,
g := &Group{
close: "",
items: []Code{
token{
typ: packageToken,
content: path,
},
token{
typ: identifierToken,
content: name,
},
},
token{
typ: identifierToken,
content: name,
},
)
name: "qual",
open: "",
separator: ".",
}
*s = append(*s, g)
return s
}
Expand Down

0 comments on commit 4a54cd6

Please sign in to comment.