Skip to content

Commit

Permalink
API Redesign
Browse files Browse the repository at this point in the history
Signed-off-by: Xuanwo <[email protected]>
  • Loading branch information
Xuanwo committed Sep 8, 2021
1 parent 3105c69 commit 1905493
Show file tree
Hide file tree
Showing 21 changed files with 137 additions and 150 deletions.
21 changes: 12 additions & 9 deletions call.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ type icall struct {
owner Node
name string
items *group
calls *group
}

// Call is used to generate a function call.
func Call(name string) *icall {
ic := &icall{
name: name,
items: newGroup("(", ")", ","),
calls: newGroup("", "", "."),
}
return ic
}
Expand All @@ -27,25 +29,26 @@ func (i *icall) render(w io.Writer) {
}
writeString(w, i.name)
i.items.render(w)
if i.calls.length() != 0 {
writeString(w, ".")
i.calls.render(w)
}
}

func (i *icall) Owner(name string) *icall {
func (i *icall) WithOwner(name string) *icall {
if i.owner != nil {
panic(fmt.Errorf("icall already have owner %v", i.owner))
}
i.owner = String(name)
return i
}

func (i *icall) Parameter(value interface{}) *icall {
i.items.append(value)
func (i *icall) AddParameter(value ...interface{}) *icall {
i.items.append(value...)
return i
}

func (i *icall) Call(name string) *icall {
ni := Call(name)
// We link the current icall to the new icall's owner, so we can
// generate the function call list.
ni.owner = i
return ni
func (i *icall) AddCall(name string, params ...interface{}) *icall {
i.calls.append(Call(name).AddParameter(params...))
return i
}
11 changes: 5 additions & 6 deletions call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ func TestCalls(t *testing.T) {
buf := pool.Get()
defer buf.Free()

expected := "x.List()"
expected := "x.List(src)"

Call("List").
Owner("x").
WithOwner("x").
AddParameter("src").
render(buf)

compareAST(t, expected, buf.String())
Expand All @@ -34,10 +35,8 @@ func TestCalls(t *testing.T) {
expected := "x.List().Next(src,dst)"

Call("List").
Owner("x").
Call("Next").
Parameter("src").
Parameter("dst").
WithOwner("x").
AddCall("Next", "src", "dst").
render(buf)

compareAST(t, expected, buf.String())
Expand Down
8 changes: 4 additions & 4 deletions const.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func Const() *iconst {
}
i.items.omitWrapIf = func() bool {
// We only need to omit wrap while length == 1.
// If length == 0, we need to keep it, or it will be invalid expr.
// NewIf length == 0, we need to keep it, or it will be invalid expr.
return i.items.length() == 1
}
return i
Expand All @@ -22,16 +22,16 @@ func (i *iconst) render(w io.Writer) {
i.items.render(w)
}

func (i *iconst) Field(name, value interface{}) *iconst {
func (i *iconst) AddField(name, value interface{}) *iconst {
i.items.append(field(name, value, "="))
return i
}
func (i *iconst) TypedField(name, typ, value interface{}) *iconst {
func (i *iconst) AddTypedField(name, typ, value interface{}) *iconst {
i.items.append(typedField(name, typ, value, "="))
return i
}

func (i *iconst) LineComment(value interface{}) *iconst {
func (i *iconst) AddLineComment(value interface{}) *iconst {
i.items.append(value)
return i
}
8 changes: 4 additions & 4 deletions const_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestConst(t *testing.T) {
expected := "const Version=2"

Const().
Field("Version", Lit(2)).
AddField("Version", Lit(2)).
render(buf)

compareAST(t, expected, buf.String())
Expand All @@ -25,7 +25,7 @@ func TestConst(t *testing.T) {
expected := "const Version int =2"

Const().
TypedField("Version", "int", Lit(2)).
AddTypedField("Version", "int", Lit(2)).
render(buf)

compareAST(t, expected, buf.String())
Expand All @@ -42,8 +42,8 @@ Description="Hello, World!"
`

Const().
Field("Version", Lit(2)).
Field("Description", Lit("Hello, World!")).
AddField("Version", Lit(2)).
AddField("Description", Lit("Hello, World!")).
render(buf)

compareAST(t, expected, buf.String())
Expand Down
18 changes: 9 additions & 9 deletions function.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,27 +65,27 @@ func (i *ifunction) render(w io.Writer) {
}
}

func (i *ifunction) Receiver(name, typ interface{}) *ifunction {
func (i *ifunction) WithReceiver(name, typ interface{}) *ifunction {
i.receiver = field(name, typ, " ")
return i
}

func (i *ifunction) Parameter(name, typ interface{}) *ifunction {
func (i *ifunction) WithCall(params ...interface{}) *ifunction {
i.call = Call("").AddParameter(params...)
return i
}

func (i *ifunction) AddParameter(name, typ interface{}) *ifunction {
i.parameters.append(field(name, typ, " "))
return i
}

func (i *ifunction) Result(name, typ interface{}) *ifunction {
func (i *ifunction) AddResult(name, typ interface{}) *ifunction {
i.results.append(field(name, typ, " "))
return i
}

func (i *ifunction) Body(node ...interface{}) *ifunction {
func (i *ifunction) AddBody(node ...interface{}) *ifunction {
i.body.append(node...)
return i
}

func (i *ifunction) Call() *icall {
i.call = Call("")
return i.call
}
28 changes: 14 additions & 14 deletions function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ func TestFunction(t *testing.T) {
expected := `func Test(a int, b string) (d uint) {}`

Function("Test").
Parameter("a", "int").
Parameter("b", "string").
Result("d", "uint").
AddParameter("a", "int").
AddParameter("b", "string").
AddResult("d", "uint").
render(buf)

compareAST(t, expected, buf.String())
Expand All @@ -26,11 +26,11 @@ func TestFunction(t *testing.T) {
return "Hello, World!"
}`
Function("Test").
Receiver("r", "*Q").
Result("a", "int").
Result("b", "int64").
Result("d", "string").
Body(
WithReceiver("r", "*Q").
AddResult("a", "int").
AddResult("b", "int64").
AddResult("d", "string").
AddBody(
String(`return "Hello, World!"`),
).
render(buf)
Expand All @@ -46,11 +46,11 @@ return "Hello, World!"
return "Hello, World!"
}`
Function("Test").
Receiver("r", "*Q").
Result("a", String("int")).
Result("b", "int64").
Result("d", "string").
Body(
WithReceiver("r", "*Q").
AddResult("a", String("int")).
AddResult("b", "int64").
AddResult("d", "string").
AddBody(
String(`return "Hello, World!"`),
).
render(buf)
Expand All @@ -65,7 +65,7 @@ return "Hello, World!"
expected := `func(){}()`

fn := Function("")
fn.Call()
fn.WithCall()
fn.render(buf)

compareAST(t, expected, buf.String())
Expand Down
41 changes: 22 additions & 19 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"
)

func Group() *group {
func NewGroup() *group {
return newGroup("", "", "\n")
}

Expand All @@ -24,7 +24,7 @@ type group struct {
close string
separator string

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

Expand All @@ -40,6 +40,9 @@ func (g *group) shouldOmitWrap() bool {
}

func (g *group) append(node ...interface{}) *group {
if len(node) == 0 {
return g
}
g.items = append(g.items, parseNodes(node)...)
return g
}
Expand Down Expand Up @@ -93,70 +96,70 @@ func (g *group) String() string {
return buf.String()
}

func (g *group) LineComment(content string, args ...interface{}) *group {
func (g *group) AddLineComment(content string, args ...interface{}) *group {
g.append(LineComment(content, args...))
return g
}

func (g *group) Package(name string) *group {
func (g *group) AddPackage(name string) *group {
g.append(Package(name))
return g
}

func (g *group) Imports() *iimport {
i := Imports()
g.append(i)
return i
}

func (g *group) Line() *group {
func (g *group) AddLine() *group {
g.append(Line())
return g
}

func (g *group) If(judge Node) *iif {
func (g *group) NewImport() *iimport {
i := Import()
g.append(i)
return i
}

func (g *group) NewIf(judge Node) *iif {
i := If(judge)
g.append(i)
return i
}

func (g *group) For(judge Node) *ifor {
func (g *group) NewFor(judge Node) *ifor {
i := For(judge)
g.append(i)
return i
}

func (g *group) Switch(judge Node) *iswitch {
func (g *group) NewSwitch(judge Node) *iswitch {
i := Switch(judge)
g.append(i)
return i
}

func (g *group) Var() *ivar {
func (g *group) NewVar() *ivar {
i := Var()
g.append(i)
return i
}

func (g *group) Const() *iconst {
func (g *group) NewConst() *iconst {
i := Const()
g.append(i)
return i
}

func (g *group) Function(name string) *ifunction {
func (g *group) NewFunction(name string) *ifunction {
f := Function(name)
g.append(f)
return f
}

func (g *group) Struct(name string) *istruct {
func (g *group) NewStruct(name string) *istruct {
i := Struct(name)
g.append(i)
return i
}

func (g *group) Interface(name string) *iinterface {
func (g *group) NewInterface(name string) *iinterface {
i := Interface(name)
g.append(i)
return i
Expand Down
10 changes: 5 additions & 5 deletions group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package gg

import "fmt"

func ExampleGroup() {
f := Group()
f.Package("main")
f.Imports().Path("fmt")
f.Function("main").Body(
func ExampleNewGroup() {
f := NewGroup()
f.AddPackage("main")
f.NewImport().AddPath("fmt")
f.NewFunction("main").AddBody(
String(`fmt.Println("%s")`, "Hello, World!"),
)
fmt.Println(f.String())
Expand Down
Loading

0 comments on commit 1905493

Please sign in to comment.