From 102e89c56bb7db90d537328ade82d58c198a4891 Mon Sep 17 00:00:00 2001 From: LandonTClipp <11232769+LandonTClipp@users.noreply.github.com> Date: Fri, 15 Nov 2024 19:04:25 -0600 Subject: [PATCH] Add `resolve-type-alias` parameter --- pkg/config/config.go | 28 +++++++++++++++------------- pkg/generator.go | 4 ++++ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index d3928e6ab..97b6a7e90 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -33,7 +33,7 @@ type Interface struct { type Config struct { All bool `mapstructure:"all"` - MockBuildTags string `mapstructure:"mock-build-tags"` + BoilerplateFile string `mapstructure:"boilerplate-file"` BuildTags string `mapstructure:"tags"` Case string `mapstructure:"case"` Config string `mapstructure:"config"` @@ -43,38 +43,38 @@ type Config struct { DisableFuncMocks bool `mapstructure:"disable-func-mocks"` DisableVersionString bool `mapstructure:"disable-version-string"` DryRun bool `mapstructure:"dry-run"` + Exclude []string `mapstructure:"exclude"` ExcludeRegex string `mapstructure:"exclude-regex"` Exported bool `mapstructure:"exported"` FileName string `mapstructure:"filename"` - IncludeAutoGenerated bool `mapstructure:"include-auto-generated"` - IncludeRegex string `mapstructure:"include-regex"` InPackage bool `mapstructure:"inpackage"` InPackageSuffix bool `mapstructure:"inpackage-suffix"` + IncludeAutoGenerated bool `mapstructure:"include-auto-generated"` + IncludeRegex string `mapstructure:"include-regex"` KeepTree bool `mapstructure:"keeptree"` LogLevel string `mapstructure:"log-level"` + MockBuildTags string `mapstructure:"mock-build-tags"` MockName string `mapstructure:"mockname"` Name string `mapstructure:"name"` Note string `mapstructure:"note"` Outpkg string `mapstructure:"outpkg"` Output string `mapstructure:"output"` - Packages map[string]interface{} `mapstructure:"packages"` Packageprefix string `mapstructure:"packageprefix"` + Packages map[string]interface{} `mapstructure:"packages"` Print bool `mapstructure:"print"` Profile string `mapstructure:"profile"` Quiet bool `mapstructure:"quiet"` Recursive bool `mapstructure:"recursive"` - Exclude []string `mapstructure:"exclude"` + ReplaceType []string `mapstructure:"replace-type"` + ResolveTypeAlias bool `mapstructure:"resolve-type-alias"` SrcPkg string `mapstructure:"srcpkg"` - BoilerplateFile string `mapstructure:"boilerplate-file"` // StructName overrides the name given to the mock struct and should only be nonempty // when generating for an exact match (non regex expression in -name). - StructName string `mapstructure:"structname"` - TestOnly bool `mapstructure:"testonly"` - UnrollVariadic bool `mapstructure:"unroll-variadic"` - Version bool `mapstructure:"version"` - WithExpecter bool `mapstructure:"with-expecter"` - ReplaceType []string `mapstructure:"replace-type"` - + StructName string `mapstructure:"structname"` + TestOnly bool `mapstructure:"testonly"` + UnrollVariadic bool `mapstructure:"unroll-variadic"` + Version bool `mapstructure:"version"` + WithExpecter bool `mapstructure:"with-expecter"` // Viper throws away case-sensitivity when it marshals into this struct. This // destroys necessary information we need, specifically around interface names. // So, we re-read the config into this map outside of viper. @@ -98,6 +98,7 @@ func NewConfigFromViper(v *viper.Viper) (*Config, error) { v.SetDefault("case", "camel") v.SetDefault("dir", ".") v.SetDefault("output", "./mocks") + v.SetDefault("resolve-type-alias", true) } else { v.SetDefault("dir", "mocks/{{.PackagePath}}") v.SetDefault("filename", "mock_{{.InterfaceName}}.go") @@ -107,6 +108,7 @@ func NewConfigFromViper(v *viper.Viper) (*Config, error) { v.SetDefault("with-expecter", true) v.SetDefault("dry-run", false) v.SetDefault("log-level", "info") + v.SetDefault("resolve-type-alias", false) } if err := v.UnmarshalExact(c); err != nil { diff --git a/pkg/generator.go b/pkg/generator.go index 4f600c78b..91a8ed305 100644 --- a/pkg/generator.go +++ b/pkg/generator.go @@ -64,6 +64,7 @@ type GeneratorConfig struct { UnrollVariadic bool WithExpecter bool ReplaceType []string + ResolveTypeAlias bool } // Generator is responsible for generating the string containing @@ -521,6 +522,9 @@ func (g *Generator) renderType(ctx context.Context, typ types.Type) string { case *types.Named: return g.renderNamedType(ctx, t) case *types.Alias: + if g.config.ResolveTypeAlias { + return g.renderType(ctx, t.Underlying()) + } return g.renderNamedType(ctx, t) case *types.TypeParam: if t.Constraint() != nil {