diff --git a/docs/configuration.md b/docs/configuration.md index f07174f1..f7c09eaa 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -197,10 +197,10 @@ Variables that are marked as being templated are capable of using mockery-provid | InterfaceDir | The directory path of the original interface being mocked. This can be used as
`#!yaml dir: "{{.InterfaceDir}}"` to place your mocks adjacent to the original interface. This should not be used for external interfaces. | | InterfaceDirRelative | The directory path of the original interface being mocked, relative to the current working directory. If the path cannot be made relative to the current working directory, this variable will be set equal to `PackagePath` | | InterfaceName | The name of the original interface being mocked | -| InterfaceNameCamel | Converts a string `interface_name` to `InterfaceName` | -| InterfaceNameLowerCamel | Converts `InterfaceName` to `interfaceName` | -| InterfaceNameSnake | Converts `InterfaceName` to `interface_name` | -| InterfaceNameLower | Converts `InterfaceName` to `interfacename` | +| InterfaceNameCamel | Converts a string `interface_name` to `InterfaceName`. Deprecated: use `{{ .InterfaceName \| camelcase }}` instead | +| InterfaceNameLowerCamel | Converts `InterfaceName` to `interfaceName` . Deprecated: use `{{ .InterfaceName \| camelcase \| firstLower }}` instead | +| InterfaceNameSnake | Converts `InterfaceName` to `interface_name` . Deprecated: use `{{ .InterfaceName \| snakecase }}` instead | +| InterfaceNameLower | Converts `InterfaceName` to `interfacename` . Deprecated: use `{{ .InterfaceName \| lower }}` instead | | Mock | A string that is `Mock` if the interface is exported, or `mock` if it is not exported. Useful when setting the name of your mock to something like:
`#!yaml mockname: "{{.Mock}}{{.InterfaceName}}"`
This way, the mock name will retain the exported-ness of the original interface. | | MockName | The name of the mock that will be generated. Note that this is simply the `mockname` configuration variable | | PackageName | The name of the package from the original interface | @@ -232,6 +232,13 @@ To learn more about the templating syntax, please [see the Go `text/template` do * [`trimRight` string cutset](https://pkg.go.dev/strings#TrimRight) * [`trimSpace` string](https://pkg.go.dev/strings#TrimSpace) * [`trimSuffix` string suffix](https://pkg.go.dev/strings#TrimSuffix) +* [`lower` string](https://pkg.go.dev/strings#ToLower) +* [`upper` string](https://pkg.go.dev/strings#ToUpper) +* [`camelcase` string](https://pkg.go.dev/github.com/huandu/xstrings#ToCamelCase) +* [`snakecase` string](https://godoc.org/github.com/huandu/xstrings#ToSnakeCase) +* [`kebabcase` string](https://godoc.org/github.com/huandu/xstrings#ToKebabCase) +* [`firstLower` string](https://godoc.org/github.com/huandu/xstrings#FirstRuneToLower) +* [`firstUpper` string](https://godoc.org/github.com/huandu/xstrings#FirstRuneToUpper) * [`matchString` pattern](https://pkg.go.dev/regexp#MatchString) * [`quoteMeta` string](https://pkg.go.dev/regexp#QuoteMeta) * [`base` string](https://pkg.go.dev/path/filepath#Base) diff --git a/go.mod b/go.mod index 3d127ffb..0a52d015 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.19 require ( github.com/chigopher/pathlib v0.15.0 github.com/davecgh/go-spew v1.1.1 + github.com/huandu/xstrings v1.4.0 github.com/iancoleman/strcase v0.2.0 github.com/jinzhu/copier v0.3.5 github.com/mitchellh/go-homedir v1.1.0 diff --git a/go.sum b/go.sum index 641fc632..e6962530 100644 --- a/go.sum +++ b/go.sum @@ -128,6 +128,8 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/huandu/xstrings v1.4.0 h1:D17IlohoQq4UcpqD7fDk80P7l+lwAmlFaBHgOipl2FU= +github.com/huandu/xstrings v1.4.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= github.com/iancoleman/strcase v0.2.0 h1:05I4QRnGpI0m37iZQRuskXh+w77mr6Z41lwQzuHLwW0= github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= diff --git a/go.work.sum b/go.work.sum index fb0cae71..2a9b8253 100644 --- a/go.work.sum +++ b/go.work.sum @@ -443,6 +443,7 @@ github.com/hashicorp/memberlist v0.5.0 h1:EtYPN8DpAURiapus508I4n9CzHs2W+8NZGbmmR github.com/hashicorp/memberlist v0.5.0/go.mod h1:yvyXLpo0QaGE59Y7hDTsTzDD25JYBZ4mHgHUZ8lrOI0= github.com/hashicorp/serf v0.10.1 h1:Z1H2J60yRKvfDYAOZLd2MU0ND4AH/WDz7xYHDWQsIPY= github.com/hashicorp/serf v0.10.1/go.mod h1:yL2t6BqATOLGc5HF7qbFkTfXoPIY0WZdWHfEvMqbG+4= +github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639 h1:mV02weKRL81bEnm8A0HT1/CAelMQDBuQIfLw8n+d6xI= github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g= github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ= diff --git a/pkg/outputter.go b/pkg/outputter.go index 10fb8025..2f1a8192 100644 --- a/pkg/outputter.go +++ b/pkg/outputter.go @@ -14,6 +14,7 @@ import ( "text/template" "github.com/chigopher/pathlib" + "github.com/huandu/xstrings" "github.com/iancoleman/strcase" "github.com/rs/zerolog" @@ -46,6 +47,13 @@ var templateFuncMap = template.FuncMap{ "trimRight": strings.TrimRight, "trimSpace": strings.TrimSpace, "trimSuffix": strings.TrimSuffix, + "lower": strings.ToLower, + "upper": strings.ToUpper, + "camelcase": xstrings.ToCamelCase, + "snakecase": xstrings.ToSnakeCase, + "kebabcase": xstrings.ToKebabCase, + "firstLower": xstrings.FirstRuneToLower, + "firstUpper": xstrings.FirstRuneToUpper, // Regular expression matching "matchString": regexp.MatchString, @@ -212,9 +220,11 @@ func parseConfigTemplates(ctx context.Context, c *config.Config, iface *Interfac PackageName string PackagePath string }{ - InterfaceDir: filepath.Dir(iface.FileName), - InterfaceDirRelative: interfaceDirRelative, - InterfaceName: iface.Name, + InterfaceDir: filepath.Dir(iface.FileName), + InterfaceDirRelative: interfaceDirRelative, + InterfaceName: iface.Name, + // Deprecated: All custom case variables of InterfaceName will be removed in the next major version + // Use the template functions instead InterfaceNameCamel: strcase.ToCamel(iface.Name), InterfaceNameLowerCamel: strcase.ToLowerCamel(iface.Name), InterfaceNameSnake: strcase.ToSnake(iface.Name), diff --git a/pkg/outputter_test.go b/pkg/outputter_test.go index 013140f9..8c7b860c 100644 --- a/pkg/outputter_test.go +++ b/pkg/outputter_test.go @@ -115,6 +115,29 @@ func Test_parseConfigTemplates(t *testing.T) { Outpkg: "packageName", }, }, + { + name: "template funcs cases", + args: args{ + c: &config.Config{ + Dir: "{{.InterfaceDir}}/{{.PackagePath}}", + FileName: "{{.InterfaceName | kebabcase }}.go", + MockName: "{{.InterfaceName | camelcase }}", + Outpkg: "{{.PackageName | snakecase }}", + }, + + iface: &Interface{ + Name: "FooBar", + FileName: "path/to/foobar.go", + }, + }, + pkg: mockPkg, + want: &config.Config{ + Dir: "path/to/github.com/user/project/package", + FileName: "foo-bar.go", + MockName: "FooBar", + Outpkg: "package_name", + }, + }, { name: "InterfaceDirRelative in current working directory", args: args{