Skip to content

Commit

Permalink
avoid generating long lines
Browse files Browse the repository at this point in the history
  • Loading branch information
kovetskiy committed May 14, 2020
1 parent c15c70b commit 23c8f02
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@

/internal/test/*_gen.go
/dist/

/gonstructor
43 changes: 38 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,44 @@ type Structure struct {
e.g.

```go
type StructureBuilder struct {
foo string
bar io.Reader
buz chan interface{}
bufferSize int
}

func NewStructureBuilder() *StructureBuilder {
return &StructureBuilder{}
}

func (b *StructureBuilder) Foo(foo string) *StructureBuilder {
b.foo = foo
return b
}

func (b *StructureBuilder) Bar(bar io.Reader) *StructureBuilder {
b.bar = bar
return b
}

func (b *StructureBuilder) Buz(buz chan interface{}) *StructureBuilder {
b.buz = buz
return b
}

func (b *StructureBuilder) BufferSize(bufferSize int) *StructureBuilder {
b.bufferSize = bufferSize
return b
}

func (b *StructureBuilder) Build() *Structure {
return &Structure{foo: b.foo, bar: b.bar, Buz: b.buz}
return &Structure{
foo: b.foo,
bar: b.bar,
Buz: b.buz,
bufferSize: b.bufferSize,
}
}
```

Expand Down Expand Up @@ -138,10 +159,22 @@ func (structure *Structure) construct() {
e.g.

```go
func NewStructure(foo string, bar io.Reader, buz chan interface{}, bufferSize int) *Structure {
r := &Structure{foo: foo, bar: bar, Buz: buz, bufferSize: bufferSize}
r.construct()
return r
func NewStructure(
foo string,
bar io.Reader,
buz chan interface{},
bufferSize int,
) *Structure {
r := &Structure{
foo: foo,
bar: bar,
Buz: buz,
bufferSize: bufferSize,
}

r.construct()

return r
}
```

Expand Down
5 changes: 4 additions & 1 deletion cmd/gonstructor/gonstructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,20 @@ func main() {
Fields: fields,
InitFunc: *initFunc,
}

case builderConstructorType:
constructorGenerator = &constructor.BuilderGenerator{
TypeName: *typeName,
Fields: fields,
InitFunc: *initFunc,
}

default:
// unreachable, just in case
log.Fatalf("[error] unexpected constructor type has come [given=%s]", constructorType)
}
rootStmt = rootStmt.AddStatements(constructorGenerator.Generate())

rootStmt = rootStmt.AddStatements(constructorGenerator.Generate(0))
}

if *withGetter {
Expand Down
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ module github.com/moznion/gonstructor
go 1.13

require (
github.com/iancoleman/strcase v0.0.0-20180726023541-3605ed457bf7
github.com/alecthomas/chroma v0.7.3 // indirect
github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334
github.com/moznion/gowrtr v1.4.0
github.com/stretchr/testify v1.3.0
golang.org/x/tools v0.0.0-20191206204035-259af5ff87bd
golang.org/x/tools v0.0.0-20200117220505-0cba7a3a9ee9
)

replace github.com/moznion/gowrtr => /home/operator/go/src/github.com/moznion/gowrtr
32 changes: 32 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,27 +1,59 @@
github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38/go.mod h1:r7bzyVFMNntcxPZXK3/+KdruV1H5KSlyVY0gc+NgInI=
github.com/alecthomas/chroma v0.7.3 h1:NfdAERMy+esYQs8OXk0I868/qDxxCEo7FMz1WIqMAeI=
github.com/alecthomas/chroma v0.7.3/go.mod h1:sko8vR34/90zvl5QdcUdvzL3J8NKjAUx9va9jPuFNoM=
github.com/alecthomas/colour v0.0.0-20160524082231-60882d9e2721/go.mod h1:QO9JBoKquHd+jz9nshCh40fOfO+JzsoXy8qTHF68zU0=
github.com/alecthomas/kong v0.2.4/go.mod h1:kQOmtJgV+Lb4aj+I2LEn40cbtawdWJ9Y8QLq+lElKxE=
github.com/alecthomas/repr v0.0.0-20180818092828-117648cd9897/go.mod h1:xTS7Pm1pD1mvyM075QCDSRqH6qRLXylzS24ZTpRiSzQ=
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 h1:y5HC9v93H5EPKqaS1UYVg1uYah5Xf51mBfIoWehClUQ=
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964/go.mod h1:Xd9hchkHSWYkEqJwUGisez3G1QY8Ryz0sdWrLPMGjLk=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlclark/regexp2 v1.2.0 h1:8sAhBGEM0dRWogWqWyQeIJnxjWO6oIjl8FKqREDsGfk=
github.com/dlclark/regexp2 v1.2.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/iancoleman/strcase v0.0.0-20180726023541-3605ed457bf7 h1:ux/56T2xqZO/3cP1I2F86qpeoYPCOzk+KF/UH/Ar+lk=
github.com/iancoleman/strcase v0.0.0-20180726023541-3605ed457bf7/go.mod h1:SK73tn/9oHe+/Y0h39VT4UCxmurVJkR5NA7kMEAOgSE=
github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334 h1:VHgatEHNcBFEB7inlalqfNqw65aNkM1lGX2yt3NmbS8=
github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334/go.mod h1:SK73tn/9oHe+/Y0h39VT4UCxmurVJkR5NA7kMEAOgSE=
github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/moznion/go-errgen v1.3.1/go.mod h1:+BkSRLNc98WWAJmdtxYcxxucQ7sl8m4sroepmHio33Y=
github.com/moznion/go-errgen v1.3.2/go.mod h1:dROwlwGrLPlSudszQRjX56OlWxWnG5tPGel0It/5LrI=
github.com/moznion/go-errgen v1.8.1/go.mod h1:3mE7v+d7czyIqbfd3jOK/wLgRn5lTOtNRNEeCHXHaCk=
github.com/moznion/gowrtr v0.0.0-20190121085203-08e30cf60446/go.mod h1:u2ZFWAHT1c56nmSP3w0C4N9MZtI2UE7YwbkItJAsF78=
github.com/moznion/gowrtr v1.4.0 h1:vA2S5GyKGZq/CXEoFg/+8UrYbow9Rh2iQUUyNbH34IA=
github.com/moznion/gowrtr v1.4.0/go.mod h1:5/DxTrHHNshUFR1aFL2q3kb+VRerTDKYcZbWu8bwa6s=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/lint v0.0.0-20181217174547-8f45f776aaf1/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f/go.mod h1:5qLYkcX4OjUUV8bRuDixDT3tpyyb+LUpUlRWLxfhWrs=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200413165638-669c56c373c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190111214448-fc1d57b08d7b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190121143147-24cd39ecf745/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191125144606-a911d9008d1f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191206204035-259af5ff87bd h1:Zc7EU2PqpsNeIfOoVA7hvQX4cS3YDJEs5KlfatT3hLo=
golang.org/x/tools v0.0.0-20191206204035-259af5ff87bd/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200117220505-0cba7a3a9ee9 h1:KOkk4e2xd5OeCDJGwacvr75ICCbCsShrHiqPEdsA9hg=
golang.org/x/tools v0.0.0-20200117220505-0cba7a3a9ee9/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
20 changes: 14 additions & 6 deletions internal/constructor/all_args_constructor_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,48 @@ package constructor

import (
"fmt"
"strings"

"github.com/iancoleman/strcase"
g "github.com/moznion/gowrtr/generator"
)

// AllArgsConstructorGenerator is a struct type that has the responsibility to generate a statement of a constructor with all of arguments.
// AllArgsConstructorGenerator is a struct type that has the responsibility to
// generate a statement of a constructor with all of arguments.
type AllArgsConstructorGenerator struct {
TypeName string
Fields []*Field
InitFunc string
}

// Generate generates a constructor statement with all of arguments.
func (cg *AllArgsConstructorGenerator) Generate() g.Statement {
func (cg *AllArgsConstructorGenerator) Generate(indentLevel int) g.Statement {
funcSignature := g.NewFuncSignature(fmt.Sprintf("New%s", strcase.ToCamel(cg.TypeName)))

retStructureKeyValues := make([]string, 0)
for _, field := range cg.Fields {
if field.ShouldIgnore {
continue
}
funcSignature = funcSignature.AddParameters(g.NewFuncParameter(toLowerCamel(field.FieldName), field.FieldType))
retStructureKeyValues = append(retStructureKeyValues, fmt.Sprintf("%s: %s", field.FieldName, toLowerCamel(field.FieldName)))
funcSignature = funcSignature.AddParameters(
g.NewFuncParameter(toLowerCamel(field.FieldName), field.FieldType),
)
retStructureKeyValues = append(
retStructureKeyValues,
fmt.Sprintf("%s: %s", field.FieldName, toLowerCamel(field.FieldName)),
)
}

funcSignature = funcSignature.AddReturnTypes("*" + cg.TypeName)

retStructure := fmt.Sprintf("&%s{%s}", cg.TypeName, strings.Join(retStructureKeyValues, ","))
retStructure := generateStructure(cg.TypeName, retStructureKeyValues, indentLevel+1)

var stmts []g.Statement
if cg.InitFunc != "" {
stmts = []g.Statement{
g.NewRawStatementf("r := %s", retStructure),
g.NewNewline(),
g.NewRawStatementf("r.%s()", cg.InitFunc),
g.NewNewline(),
g.NewReturnStatement("r"),
}
} else {
Expand All @@ -50,5 +57,6 @@ func (cg *AllArgsConstructorGenerator) Generate() g.Statement {
funcSignature,
stmts...,
)

return fn
}
9 changes: 4 additions & 5 deletions internal/constructor/builder_constructor_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package constructor

import (
"fmt"
"strings"

"github.com/iancoleman/strcase"
g "github.com/moznion/gowrtr/generator"
Expand All @@ -16,7 +15,7 @@ type BuilderGenerator struct {
}

// Generate generates a builder statement.
func (cg *BuilderGenerator) Generate() g.Statement {
func (cg *BuilderGenerator) Generate(indentLevel int) g.Statement {
builderConstructorName := fmt.Sprintf("New%sBuilder", strcase.ToCamel(cg.TypeName))
builderType := fmt.Sprintf("%sBuilder", strcase.ToCamel(cg.TypeName))

Expand Down Expand Up @@ -52,7 +51,7 @@ func (cg *BuilderGenerator) Generate() g.Statement {
retStructureKeyValues = append(retStructureKeyValues, fmt.Sprintf("%s: b.%s", field.FieldName, toLowerCamel(field.FieldName)))
}

buildResult := fmt.Sprintf("&%s{%s}", cg.TypeName, strings.Join(retStructureKeyValues, ","))
buildResult := generateStructure(cg.TypeName, retStructureKeyValues, indentLevel+1)

var buildStmts []g.Statement
if cg.InitFunc != "" {
Expand All @@ -75,7 +74,7 @@ func (cg *BuilderGenerator) Generate() g.Statement {

stmt := g.NewRoot(builderStruct, builderConstructorFunc)
for _, f := range fieldRegistererFunctions {
stmt = stmt.AddStatements(f)
stmt = stmt.AddStatements(g.NewNewline(), f)
}
return stmt.AddStatements(buildFunc)
return stmt.AddStatements(g.NewNewline(), buildFunc)
}
2 changes: 1 addition & 1 deletion internal/constructor/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import g "github.com/moznion/gowrtr/generator"
// Generator is an interface that has the responsibility to generate a constructor code.
type Generator interface {
// Generate generates a constructor statement.
Generate() g.Statement
Generate(indentLevel int) g.Statement
}
21 changes: 21 additions & 0 deletions internal/constructor/struct.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package constructor

import (
"fmt"
"strings"

g "github.com/moznion/gowrtr/generator"
)

func generateStructure(typeName string, keyValues []string, indentLevel int) string {
indent := g.BuildIndent(indentLevel)
nextIndent := g.BuildIndent(indentLevel + 1)

return fmt.Sprintf(
"&%s{\n%s%s,\n%s}",
typeName,
nextIndent,
strings.Join(keyValues, ",\n"+nextIndent),
indent,
)
}

0 comments on commit 23c8f02

Please sign in to comment.