Skip to content

Commit

Permalink
Merge pull request #569 from hikyaru-suzuki/feature/add_struct_tag
Browse files Browse the repository at this point in the history
Generate struct tag
  • Loading branch information
LandonTClipp authored Mar 9, 2023
2 parents 284fce2 + dd639a3 commit b61e29c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ require (
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/afero v1.9.3 // indirect
Expand Down
File renamed without changes.
11 changes: 11 additions & 0 deletions pkg/fixtures/struct_with_tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package test

type StructWithTag interface {
MethodA(v *struct {
FieldA int `json:"field_a"`
FieldB int `json:"field_b" xml:"field_b"`
}) *struct {
FieldC int `json:"field_c"`
FieldD int `json:"field_d" xml:"field_d"`
}
}
7 changes: 6 additions & 1 deletion pkg/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,12 @@ func (g *Generator) renderType(ctx context.Context, typ types.Type) string {
if f.Anonymous() {
fields = append(fields, g.renderType(ctx, f.Type()))
} else {
fields = append(fields, fmt.Sprintf("%s %s", f.Name(), g.renderType(ctx, f.Type())))
field := fmt.Sprintf("%s %s", f.Name(), g.renderType(ctx, f.Type()))
tag := t.Tag(i)
if tag != "" {
field += " `" + tag + "`"
}
fields = append(fields, field)
}
}

Expand Down
22 changes: 18 additions & 4 deletions pkg/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
mocks "github.com/vektra/mockery/v2/mocks/github.com/vektra/mockery/v2/pkg/fixtures"
)
Expand Down Expand Up @@ -904,7 +903,7 @@ func (s *GeneratorSuite) TestVersionOnCorrectLine() {
gen.GeneratePrologue(s.ctx, pkg)
err := gen.Generate(s.ctx)

require.NoError(s.T(), err)
s.NoError(err)
scan := bufio.NewScanner(&gen.buf)
s.Contains("Code generated by", scan.Text())
}
Expand Down Expand Up @@ -2098,8 +2097,7 @@ func NewMapToInterface(t mockConstructorTestingTNewMapToInterface) *MapToInterfa
return mock
}
`
s.checkGeneration("MapToInterface.go", "MapToInterface", false, "", expected)

s.checkGeneration("map_to_interface.go", "MapToInterface", false, "", expected)
}

func (s *GeneratorSuite) TestGeneratorFunctionArgsNamesCollision() {
Expand Down Expand Up @@ -2306,6 +2304,22 @@ func NewA(t mockConstructorTestingTNewA) *A {
s.checkGeneration("struct_value.go", "A", false, "", expected)
}

func (s *GeneratorSuite) TestGeneratorForStructWithTag() {
// StructTag has back-quote, So can't use raw string literals in this test case.
var expected string
expected += "*struct {"
expected += "FieldC int `json:\"field_c\"`"
expected += "FieldD int `json:\"field_d\" xml:\"field_d\"`"
expected += "}"

gen := s.getGeneratorWithConfig("struct_with_tag.go", "StructWithTag", GeneratorConfig{})
err := gen.Generate(s.ctx)
s.NoError(err)

actual := bufio.NewScanner(&gen.buf).Text()
s.Contains(expected, actual)
}

func (s *GeneratorSuite) TestStructNameOverride() {
expected := `// Requester2OverrideName is an autogenerated mock type for the Requester2 type
type Requester2OverrideName struct {
Expand Down

0 comments on commit b61e29c

Please sign in to comment.