Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid generating long lines #4

Merged
merged 1 commit into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module github.com/moznion/gonstructor

go 1.13
go 1.14

require (
github.com/iancoleman/strcase v0.0.0-20180726023541-3605ed457bf7
github.com/moznion/gowrtr v1.4.0
github.com/stretchr/testify v1.3.0
golang.org/x/tools v0.0.0-20191206204035-259af5ff87bd
github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334
github.com/moznion/gowrtr v1.5.0
github.com/stretchr/testify v1.6.0
golang.org/x/tools v0.0.0-20200530233709-52effbd89c51
)
52 changes: 50 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,27 +1,75 @@
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/moznion/gowrtr v1.5.0 h1:qIBySxYUwtSFVq5dXfxXPAr2IMLvfCMbPpwRHDDTMDM=
github.com/moznion/gowrtr v1.5.0/go.mod h1:6JVftynefKr+F7IDQLqdx7xgb2lxsm6w/GeM/Hv1AaM=
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=
github.com/stretchr/testify v1.6.0 h1:jlIyCplCJFULU/01vCkhKuTyc3OorI3bJFuw6obfgho=
github.com/stretchr/testify v1.6.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
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/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
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/net v0.0.0-20200226121028-0de0cce0169b/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/sync v0.0.0-20190911185100-cd5d95a43a6e/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-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
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/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.0.0-20200530233709-52effbd89c51 h1:Wec8/IO8hAraBf0it7/dPQYOslIrgM938wZYNkLnOYc=
golang.org/x/tools v0.0.0-20200530233709-52effbd89c51/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
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=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
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,
)
}