Skip to content

Commit

Permalink
feat: Add omit wrap support
Browse files Browse the repository at this point in the history
Signed-off-by: Xuanwo <[email protected]>
  • Loading branch information
Xuanwo committed Sep 3, 2021
1 parent 5106691 commit 86fb472
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 4 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,38 @@

`gg` is a General Golang Code Generator: A Good Game to play with Golang.

```go
package main

import (
"fmt"

"github.com/Xuanwo/gg"
)

func main() {
f := Group()
f.Package("main")
f.Imports().Path("fmt")
f.Function("main").Body(
StringF(`fmt.Println("%s")`, "Hello, World!"),
)
fmt.Println(f.String())
}
```

Output (after `go fmt`)

```go
package main

import "fmt"

func main() {
fmt.Println("Hello, World!")
}
```

## Acknowledgement

- `gg` is inspired by [dave/jennifer](https://github.com/dave/jennifer), I borrowed most ideas and some code from it. Nice work!
7 changes: 6 additions & 1 deletion function.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ type ifunction struct {
}

func Function(name string) *ifunction {
return &ifunction{
i := &ifunction{
name: name,
comments: newGroup("", "", "\n"),
parameters: newGroup("(", ")", ","),
results: newGroup("(", ")", ","),
body: newGroup("{\n", "}", "\n"),
}
// We should omit the `()` if result is empty
i.results.omitWrapIf = func() bool {
return i.results.length() == 0
}
return i
}

func (f *ifunction) render(w io.Writer) {
Expand Down
26 changes: 24 additions & 2 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ type group struct {
open string
close string
separator string

// If this result is true, we will omit the wrap like `()`, `{}`.
omitWrapIf func() bool
}

func (g *group) length() int {
return len(g.items)
}

func (g *group) shouldOmitWrap() bool {
if g.omitWrapIf == nil {
return false
}
return g.omitWrapIf()
}

func (g *group) append(node ...Node) *group {
Expand All @@ -31,7 +45,7 @@ func (g *group) append(node ...Node) *group {
}

func (g *group) render(w io.Writer) {
if g.open != "" {
if g.open != "" && !g.shouldOmitWrap() {
writeString(w, g.open)
}

Expand All @@ -44,7 +58,7 @@ func (g *group) render(w io.Writer) {
isfirst = false
}

if g.close != "" {
if g.close != "" && !g.shouldOmitWrap() {
writeString(w, g.close)
}
}
Expand All @@ -62,6 +76,14 @@ func (g *group) WriteFile(path string) error {
return nil
}

func (g *group) String() string {
buf := pool.Get()
defer buf.Free()

g.render(buf)
return buf.String()
}

func (g *group) Comment(content string) *group {
g.append(Comment(content))
return g
Expand Down
19 changes: 19 additions & 0 deletions group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package gg

import "fmt"

func ExampleGroup() {
f := Group()
f.Package("main")
f.Imports().Path("fmt")
f.Function("main").Body(
StringF(`fmt.Println("%s")`, "Hello, World!"),
)
fmt.Println(f.String())
// Output:
// package main
//
// import "fmt"
// func main(){
// fmt.Println("Hello, World!")}
}
10 changes: 9 additions & 1 deletion import.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@ type iimport struct {
}

func Imports() *iimport {
return &iimport{
i := &iimport{
items: newGroup("(", ")", "\n"),
}
i.items.omitWrapIf = func() bool {
return i.items.length() <= 1
}
return i
}

func (i *iimport) render(w io.Writer) {
// Don't need to render anything if import is empty
if i.items.length() == 0 {
return
}
writeString(w, "import ")
i.items.render(w)
}
Expand Down

0 comments on commit 86fb472

Please sign in to comment.