Skip to content

Commit

Permalink
Merge pull request #6 from moznion/embedding
Browse files Browse the repository at this point in the history
Add support for embedding
  • Loading branch information
moznion authored Feb 4, 2021
2 parents 8085fac + 103c8b0 commit 5bed476
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/moznion/gonstructor

go 1.14
go 1.15

require (
github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334
Expand Down
18 changes: 16 additions & 2 deletions internal/constructor/field_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"go/ast"
"go/types"
"reflect"
"strings"
)

const gonstructorTag = "gonstructor"
Expand Down Expand Up @@ -51,9 +52,22 @@ func convertStructFieldsToConstructorOnes(fields []*ast.Field) []*Field {
shouldIgnore = customTag.Get(gonstructorTag) == "-"
}

fieldType := types.ExprString(field.Type)

var fieldName string
if len(field.Names) > 0 {
fieldName = field.Names[0].Name
} else {
// split 'mypackage.MyType'
chunks := strings.Split(fieldType, ".")

// it could be a pointer: '*mypackage.MyType' or '*MyType'
fieldName = strings.TrimPrefix(chunks[len(chunks)-1], "*")
}

fs[i] = &Field{
FieldName: field.Names[0].Name,
FieldType: types.ExprString(field.Type),
FieldName: fieldName,
FieldType: fieldType,
ShouldIgnore: shouldIgnore,
}
}
Expand Down
16 changes: 16 additions & 0 deletions internal/test/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,19 @@ type StructureWithInit struct {
func (structure *StructureWithInit) initialize() {
structure.status = "ok"
}

type Embedded struct {
Bar string
}

//go:generate sh -c "$(cd ./\"$(git rev-parse --show-cdup)\" || exit; pwd)/dist/gonstructor_test --type=StructureWithEmbedding --constructorTypes=allArgs,builder --withGetter"
type StructureWithEmbedding struct {
Embedded
foo string
}

//go:generate sh -c "$(cd ./\"$(git rev-parse --show-cdup)\" || exit; pwd)/dist/gonstructor_test --type=StructureWithPointerEmbedding --constructorTypes=allArgs,builder --withGetter"
type StructureWithPointerEmbedding struct {
*Embedded
foo string
}
46 changes: 46 additions & 0 deletions internal/test/structure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,49 @@ func TestStructureWithInitBuilder(t *testing.T) {
assert.EqualValues(t, "ok", got.status)
assert.EqualValues(t, nil, got.qux)
}

func TestStructureWithEmbeddingAllArgsConstructor(t *testing.T) {
got := NewStructureWithEmbedding(Embedded{Bar: "bar"}, "foo")
assert.IsType(t, &StructureWithEmbedding{}, got)

assert.EqualValues(t, "foo", got.foo)
assert.EqualValues(t, "bar", got.Bar)
assert.EqualValues(t, "bar", got.Embedded.Bar)

// test for getters
assert.EqualValues(t, "foo", got.GetFoo())
assert.EqualValues(t, "bar", got.GetEmbedded().Bar)
}

func TestStructureWithEmbeddingBuilder(t *testing.T) {
b := NewStructureWithEmbeddingBuilder()
got := b.Foo("foo").Embedded(Embedded{Bar: "bar"}).Build()
assert.IsType(t, &StructureWithEmbedding{}, got)

assert.EqualValues(t, "foo", got.foo)
assert.EqualValues(t, "bar", got.Bar)
assert.EqualValues(t, "bar", got.Embedded.Bar)
}

func TestStructureWithPointerEmbeddingAllArgsConstructor(t *testing.T) {
got := NewStructureWithPointerEmbedding(&Embedded{Bar: "bar"}, "foo")
assert.IsType(t, &StructureWithPointerEmbedding{}, got)

assert.EqualValues(t, "foo", got.foo)
assert.EqualValues(t, "bar", got.Bar)
assert.EqualValues(t, "bar", got.Embedded.Bar)

// test for getters
assert.EqualValues(t, "foo", got.GetFoo())
assert.EqualValues(t, "bar", got.GetEmbedded().Bar)
}

func TestStructureWithPointerEmbeddingBuilder(t *testing.T) {
b := NewStructureWithPointerEmbeddingBuilder()
got := b.Foo("foo").Embedded(&Embedded{Bar: "bar"}).Build()
assert.IsType(t, &StructureWithPointerEmbedding{}, got)

assert.EqualValues(t, "foo", got.foo)
assert.EqualValues(t, "bar", got.Bar)
assert.EqualValues(t, "bar", got.Embedded.Bar)
}

0 comments on commit 5bed476

Please sign in to comment.