diff --git a/go.mod b/go.mod index 7504f120..31995449 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/pkg/fixtures/MapToInterface.go b/pkg/fixtures/map_to_interface.go similarity index 100% rename from pkg/fixtures/MapToInterface.go rename to pkg/fixtures/map_to_interface.go diff --git a/pkg/fixtures/struct_with_tag.go b/pkg/fixtures/struct_with_tag.go new file mode 100644 index 00000000..5da88314 --- /dev/null +++ b/pkg/fixtures/struct_with_tag.go @@ -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"` + } +} diff --git a/pkg/generator.go b/pkg/generator.go index 356ed60e..bff07333 100644 --- a/pkg/generator.go +++ b/pkg/generator.go @@ -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) } } diff --git a/pkg/generator_test.go b/pkg/generator_test.go index f2cd1a4f..629e0e67 100644 --- a/pkg/generator_test.go +++ b/pkg/generator_test.go @@ -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" ) @@ -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()) } @@ -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() { @@ -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 {