Skip to content

Commit

Permalink
Add resolve-type-alias parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
LandonTClipp committed Nov 16, 2024
1 parent f6ecb44 commit 102e89c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
28 changes: 15 additions & 13 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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.
Expand All @@ -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")
Expand All @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions pkg/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type GeneratorConfig struct {
UnrollVariadic bool
WithExpecter bool
ReplaceType []string
ResolveTypeAlias bool
}

// Generator is responsible for generating the string containing
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 102e89c

Please sign in to comment.