Skip to content

Commit

Permalink
Change programming interface: remove redundant Generator suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
moznion committed Jan 14, 2019
1 parent b9de7da commit 34e3b5d
Show file tree
Hide file tree
Showing 89 changed files with 1,082 additions and 1,082 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ import (
)

func main() {
generator := generator.NewRootGenerator(
generator.NewCommentGenerator(" THIS CODE WAS AUTO GENERATED"),
generator.NewPackageGenerator("main"),
generator.NewNewlineGenerator(),
generator := generator.NewRoot(
generator.NewComment(" THIS CODE WAS AUTO GENERATED"),
generator.NewPackage("main"),
generator.NewNewline(),
).AddStatements(
generator.NewFuncGenerator(
generator.NewFunc(
nil,
generator.NewFuncSignatureGenerator("main"),
generator.NewFuncSignature("main"),
).AddStatements(
generator.NewRawStatementGenerator(`fmt.Println("hello, world!")`),
generator.NewRawStatement(`fmt.Println("hello, world!")`),
),
).
EnableGofmt("-s").
Expand Down Expand Up @@ -63,10 +63,10 @@ Description

Please refer to the godoc: [![GoDoc](https://godoc.org/github.com/moznion/gowrtr/generator?status.svg)](https://godoc.org/github.com/moznion/gowrtr/generator)

### RootGenerator
### Root

- `RootGenerator` is an entry point to generate the go code.
- `RootGenerator` supports following code formatting on code generating phase. It applies such formatters to generated code.
- `Root` is an entry point to generate the go code.
- `Root` supports following code formatting on code generating phase. It applies such formatters to generated code.
- `gofmt`: with `EnableGofmt(gofmtOptions ...string)`
- `goimports`: with `EnableGoimports()`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,39 @@ package generator

import "github.com/moznion/gowrtr/internal/errmsg"

// AnonymousFuncGenerator represents a code generator for anonymous func.
type AnonymousFuncGenerator struct {
// AnonymousFunc represents a code generator for anonymous func.
type AnonymousFunc struct {
GoFunc bool
AnonymousFuncSignature *AnonymousFuncSignatureGenerator
Statements []StatementGenerator
FuncInvocation *FuncInvocationGenerator
AnonymousFuncSignature *AnonymousFuncSignature
Statements []Statement
FuncInvocation *FuncInvocation
}

// NewAnonymousFuncGenerator returns a new `AnonymousFuncGenerator`.
// NewAnonymousFunc returns a new `AnonymousFunc`.
// If `goFunc` is true, the anonymous function will be `go func`.
func NewAnonymousFuncGenerator(goFunc bool, signature *AnonymousFuncSignatureGenerator, statements ...StatementGenerator) *AnonymousFuncGenerator {
return &AnonymousFuncGenerator{
func NewAnonymousFunc(goFunc bool, signature *AnonymousFuncSignature, statements ...Statement) *AnonymousFunc {
return &AnonymousFunc{
GoFunc: goFunc,
AnonymousFuncSignature: signature,
Statements: statements,
}
}

// AddStatements adds statements for the function to `AnonymousFuncGenerator`.
// This method returns a *new* `AnonymousFuncGenerator`; it means this method acts as immutable.
func (ifg *AnonymousFuncGenerator) AddStatements(statements ...StatementGenerator) *AnonymousFuncGenerator {
return &AnonymousFuncGenerator{
// AddStatements adds statements for the function to `AnonymousFunc`.
// This method returns a *new* `AnonymousFunc`; it means this method acts as immutable.
func (ifg *AnonymousFunc) AddStatements(statements ...Statement) *AnonymousFunc {
return &AnonymousFunc{
GoFunc: ifg.GoFunc,
AnonymousFuncSignature: ifg.AnonymousFuncSignature,
Statements: append(ifg.Statements, statements...),
FuncInvocation: ifg.FuncInvocation,
}
}

// SetFuncInvocation sets an invocation of the anonymous func to `AnonymousFuncGenerator`.
// This method returns a *new* `AnonymousFuncGenerator`; it means this method acts as immutable.
func (ifg *AnonymousFuncGenerator) SetFuncInvocation(funcInvocation *FuncInvocationGenerator) *AnonymousFuncGenerator {
return &AnonymousFuncGenerator{
// SetFuncInvocation sets an invocation of the anonymous func to `AnonymousFunc`.
// This method returns a *new* `AnonymousFunc`; it means this method acts as immutable.
func (ifg *AnonymousFunc) SetFuncInvocation(funcInvocation *FuncInvocation) *AnonymousFunc {
return &AnonymousFunc{
GoFunc: ifg.GoFunc,
AnonymousFuncSignature: ifg.AnonymousFuncSignature,
Statements: ifg.Statements,
Expand All @@ -43,7 +43,7 @@ func (ifg *AnonymousFuncGenerator) SetFuncInvocation(funcInvocation *FuncInvocat
}

// Generate generates an anonymous func as golang code.
func (ifg *AnonymousFuncGenerator) Generate(indentLevel int) (string, error) {
func (ifg *AnonymousFunc) Generate(indentLevel int) (string, error) {
indent := buildIndent(indentLevel)

stmt := indent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import (
"log"
)

func ExampleAnonymousFuncGenerator_Generate() {
generator := NewAnonymousFuncGenerator(
func ExampleAnonymousFunc_Generate() {
generator := NewAnonymousFunc(
true,
NewAnonymousFuncSignatureGenerator().
NewAnonymousFuncSignature().
AddFuncParameters(
NewFuncParameter("foo", "string"),
NewFuncParameter("bar", "int64"),
).
AddReturnTypes("string", "error"),
NewCommentGenerator(" do something"),
NewRawStatementGenerator(`fmt.Printf("%d", i)`, true),
).SetFuncInvocation(NewFuncInvocationGenerator("foo", "bar"))
NewComment(" do something"),
NewRawStatement(`fmt.Printf("%d", i)`, true),
).SetFuncInvocation(NewFuncInvocation("foo", "bar"))

generated, err := generator.Generate(0)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,37 @@ import (
"github.com/moznion/gowrtr/internal/errmsg"
)

// AnonymousFuncSignatureGenerator represents a code generator for signature of anonymous func.
type AnonymousFuncSignatureGenerator struct {
// AnonymousFuncSignature represents a code generator for signature of anonymous func.
type AnonymousFuncSignature struct {
FuncParameters []*FuncParameter
ReturnTypes []string
}

// NewAnonymousFuncSignatureGenerator returns a new `AnonymousFuncSignatureGenerator`.
func NewAnonymousFuncSignatureGenerator() *AnonymousFuncSignatureGenerator {
return &AnonymousFuncSignatureGenerator{}
// NewAnonymousFuncSignature returns a new `AnonymousFuncSignature`.
func NewAnonymousFuncSignature() *AnonymousFuncSignature {
return &AnonymousFuncSignature{}
}

// AddFuncParameters adds parameters of function to `AnonymousFuncSignatureGenerator`.
// This method returns a *new* `AnonymousFuncSignatureGenerator`; it means this method acts as immutable.
func (f *AnonymousFuncSignatureGenerator) AddFuncParameters(funcParameters ...*FuncParameter) *AnonymousFuncSignatureGenerator {
return &AnonymousFuncSignatureGenerator{
// AddFuncParameters adds parameters of function to `AnonymousFuncSignature`.
// This method returns a *new* `AnonymousFuncSignature`; it means this method acts as immutable.
func (f *AnonymousFuncSignature) AddFuncParameters(funcParameters ...*FuncParameter) *AnonymousFuncSignature {
return &AnonymousFuncSignature{
FuncParameters: append(f.FuncParameters, funcParameters...),
ReturnTypes: f.ReturnTypes,
}
}

// AddReturnTypes adds return types of the function to `AnonymousFuncSignatureGenerator`.
// This method returns a *new* `AnonymousFuncSignatureGenerator`; it means this method acts as immutable.
func (f *AnonymousFuncSignatureGenerator) AddReturnTypes(returnTypes ...string) *AnonymousFuncSignatureGenerator {
return &AnonymousFuncSignatureGenerator{
// AddReturnTypes adds return types of the function to `AnonymousFuncSignature`.
// This method returns a *new* `AnonymousFuncSignature`; it means this method acts as immutable.
func (f *AnonymousFuncSignature) AddReturnTypes(returnTypes ...string) *AnonymousFuncSignature {
return &AnonymousFuncSignature{
FuncParameters: f.FuncParameters,
ReturnTypes: append(f.ReturnTypes, returnTypes...),
}
}

// Generate generates a signature of the anonymous func as golang code.
func (f *AnonymousFuncSignatureGenerator) Generate(indentLevel int) (string, error) {
func (f *AnonymousFuncSignature) Generate(indentLevel int) (string, error) {
stmt := "("

typeExisted := true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"log"
)

func ExampleAnonymousFuncSignatureGenerator_Generate() {
generator := NewAnonymousFuncSignatureGenerator().
func ExampleAnonymousFuncSignature_Generate() {
generator := NewAnonymousFuncSignature().
AddFuncParameters(
NewFuncParameter("foo", "string"),
NewFuncParameter("bar", "int64"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestShouldGenerateAnonymousFuncSignatureCode(t *testing.T) {
generator := NewAnonymousFuncSignatureGenerator()
generator := NewAnonymousFuncSignature()
gen, err := generator.Generate(0)
assert.NoError(t, err)
assert.Equal(t, "()", gen)
Expand All @@ -21,7 +21,7 @@ func TestShouldGenerateAnonymousFuncSignatureCode(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "(foo string) string", gen)

generator = NewAnonymousFuncSignatureGenerator().
generator = NewAnonymousFuncSignature().
AddFuncParameters(
NewFuncParameter("foo", "string"),
NewFuncParameter("bar", "int64"),
Expand All @@ -33,15 +33,15 @@ func TestShouldGenerateAnonymousFuncSignatureCode(t *testing.T) {
}

func TestShouldGenerateAnonymousFuncSignatureRaisesErrorWhenParamNameIsEmpty(t *testing.T) {
generator := NewAnonymousFuncSignatureGenerator().AddFuncParameters(
generator := NewAnonymousFuncSignature().AddFuncParameters(
NewFuncParameter("", "string"),
)
_, err := generator.Generate(0)
assert.EqualError(t, err, errmsg.FuncParameterNameIsEmptyErr().Error())
}

func TestShouldGenerateAnonymousFuncSignatureRaisesErrorWhenParamTypeIsEmpty(t *testing.T) {
generator := NewAnonymousFuncSignatureGenerator().AddFuncParameters(
generator := NewAnonymousFuncSignature().AddFuncParameters(
NewFuncParameter("foo", ""),
)
_, err := generator.Generate(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
)

func TestShouldGenerateAnonymousFunc(t *testing.T) {
generator := NewAnonymousFuncGenerator(
generator := NewAnonymousFunc(
false,
NewAnonymousFuncSignatureGenerator(),
NewAnonymousFuncSignature(),
)

{
Expand All @@ -30,8 +30,8 @@ func TestShouldGenerateAnonymousFunc(t *testing.T) {
}
`
generator = generator.AddStatements(
NewCommentGenerator(" do something"),
NewRawStatementGenerator(`fmt.Printf("%d", i)`, true),
NewComment(" do something"),
NewRawStatement(`fmt.Printf("%d", i)`, true),
)
gen, err := generator.Generate(0)
assert.NoError(t, err)
Expand All @@ -40,16 +40,16 @@ func TestShouldGenerateAnonymousFunc(t *testing.T) {
}

func TestShouldGenerateAnonymousFuncWithSignature(t *testing.T) {
generator := NewAnonymousFuncGenerator(
generator := NewAnonymousFunc(
false,
NewAnonymousFuncSignatureGenerator().
NewAnonymousFuncSignature().
AddFuncParameters(
NewFuncParameter("foo", "string"),
NewFuncParameter("bar", "int64"),
).
AddReturnTypes("string", "error"),
NewCommentGenerator(" do something"),
NewRawStatementGenerator(`fmt.Printf("%d", i)`, true),
NewComment(" do something"),
NewRawStatement(`fmt.Printf("%d", i)`, true),
)

expected := `func(foo string, bar int64) (string, error) {
Expand All @@ -63,17 +63,17 @@ func TestShouldGenerateAnonymousFuncWithSignature(t *testing.T) {
}

func TestShouldGenerateAnonymousGoFuncWithInvocation(t *testing.T) {
generator := NewAnonymousFuncGenerator(
generator := NewAnonymousFunc(
true,
NewAnonymousFuncSignatureGenerator().
NewAnonymousFuncSignature().
AddFuncParameters(
NewFuncParameter("foo", "string"),
NewFuncParameter("bar", "int64"),
).
AddReturnTypes("string", "error"),
NewCommentGenerator(" do something"),
NewRawStatementGenerator(`fmt.Printf("%d", i)`, true),
).SetFuncInvocation(NewFuncInvocationGenerator("foo", "bar"))
NewComment(" do something"),
NewRawStatement(`fmt.Printf("%d", i)`, true),
).SetFuncInvocation(NewFuncInvocation("foo", "bar"))

expected := `go func(foo string, bar int64) (string, error) {
// do something
Expand All @@ -86,18 +86,18 @@ func TestShouldGenerateAnonymousGoFuncWithInvocation(t *testing.T) {
}

func TestShouldGenerateAnonymousFuncRaisesErrorWhenAnonymousFuncSignatureIsNil(t *testing.T) {
generator := NewAnonymousFuncGenerator(
generator := NewAnonymousFunc(
false,
nil,
)
_, err := generator.Generate(0)
assert.EqualError(t, err, errmsg.AnonymousFuncSignatureIsNilError().Error())
}

func TestShouldGenerateAnonymousFuncRaisesErrorWhenAnonymousFuncSignatureGeneratorRaisesError(t *testing.T) {
generator := NewAnonymousFuncGenerator(
func TestShouldGenerateAnonymousFuncRaisesErrorWhenAnonymousFuncSignatureRaisesError(t *testing.T) {
generator := NewAnonymousFunc(
false,
NewAnonymousFuncSignatureGenerator().AddFuncParameters(
NewAnonymousFuncSignature().AddFuncParameters(
NewFuncParameter("", "string"),
),
)
Expand All @@ -106,21 +106,21 @@ func TestShouldGenerateAnonymousFuncRaisesErrorWhenAnonymousFuncSignatureGenerat
}

func TestShouldGenerateAnonymousFuncRaisesErrorWhenStatementRaisesError(t *testing.T) {
generator := NewAnonymousFuncGenerator(
generator := NewAnonymousFunc(
false,
NewAnonymousFuncSignatureGenerator(),
NewFuncGenerator(nil, NewFuncSignatureGenerator("")),
NewAnonymousFuncSignature(),
NewFunc(nil, NewFuncSignature("")),
)

_, err := generator.Generate(0)
assert.EqualError(t, err, errmsg.FuncNameIsEmptyError().Error())
}

func TestShouldGenerateAnonymousFuncRaisesErrorWhenFuncInvocationGeneratorRaisesError(t *testing.T) {
generator := NewAnonymousFuncGenerator(
func TestShouldGenerateAnonymousFuncRaisesErrorWhenFuncInvocationRaisesError(t *testing.T) {
generator := NewAnonymousFunc(
false,
NewAnonymousFuncSignatureGenerator(),
).SetFuncInvocation(NewFuncInvocationGenerator(""))
NewAnonymousFuncSignature(),
).SetFuncInvocation(NewFuncInvocation(""))
_, err := generator.Generate(0)
assert.EqualError(t, err, errmsg.FuncInvocationParameterIsEmptyError().Error())
}
22 changes: 11 additions & 11 deletions generator/case_generator.go → generator/case.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,32 @@ import (
"github.com/moznion/gowrtr/internal/errmsg"
)

// CaseGenerator represents a code generator for `case` statement.
// Case represents a code generator for `case` statement.
// See also: https://tour.golang.org/flowcontrol/9
type CaseGenerator struct {
type Case struct {
Condition string
Statements []StatementGenerator
Statements []Statement
}

// NewCaseGenerator creates a new `CaseGenerator`.
func NewCaseGenerator(condition string, statements ...StatementGenerator) *CaseGenerator {
return &CaseGenerator{
// NewCase creates a new `Case`.
func NewCase(condition string, statements ...Statement) *Case {
return &Case{
Condition: condition,
Statements: statements,
}
}

// AddStatements adds statements to `CaseGenerator`.
// This method returns a *new* `CaseGenerator`; it means this method acts as immutable.
func (c *CaseGenerator) AddStatements(statements ...StatementGenerator) *CaseGenerator {
return &CaseGenerator{
// AddStatements adds statements to `Case`.
// This method returns a *new* `Case`; it means this method acts as immutable.
func (c *Case) AddStatements(statements ...Statement) *Case {
return &Case{
Condition: c.Condition,
Statements: append(c.Statements, statements...),
}
}

// Generate generates `case` statement as golang code.
func (c *CaseGenerator) Generate(indentLevel int) (string, error) {
func (c *Case) Generate(indentLevel int) (string, error) {
condition := c.Condition
if condition == "" {
return "", errmsg.CaseConditionIsEmptyError()
Expand Down
Loading

0 comments on commit 34e3b5d

Please sign in to comment.