Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Add new string template functions #705

Merged
merged 5 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,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 <br>`#!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`. <b>Deprecated</b>: use `{{ .InterfaceName \| camelcase }}` instead |
| InterfaceNameLowerCamel | Converts `InterfaceName` to `interfaceName` . <b>Deprecated</b>: use `{{ .InterfaceName \| camelcase \| firstLower }}` instead |
| InterfaceNameSnake | Converts `InterfaceName` to `interface_name` . <b>Deprecated</b>: use `{{ .InterfaceName \| snakecase }}` instead |
| InterfaceNameLower | Converts `InterfaceName` to `interfacename` . <b>Deprecated</b>: 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: <br>`#!yaml mockname: "{{.Mock}}{{.InterfaceName}}"`<br> 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 |
Expand Down Expand Up @@ -231,6 +231,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)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.19
require (
github.com/chigopher/pathlib v0.15.0
github.com/davecgh/go-spew v1.1.1
github.com/iancoleman/strcase v0.2.0
github.com/huandu/xstrings v1.4.0
github.com/jinzhu/copier v0.3.5
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/mapstructure v1.5.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +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/iancoleman/strcase v0.2.0 h1:05I4QRnGpI0m37iZQRuskXh+w77mr6Z41lwQzuHLwW0=
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
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/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
Expand Down
1 change: 1 addition & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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/huandu/xstrings v1.4.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
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=
Expand Down
15 changes: 11 additions & 4 deletions pkg/outputter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"text/template"

"github.com/chigopher/pathlib"
"github.com/iancoleman/strcase"
"github.com/huandu/xstrings"
"github.com/rs/zerolog"

"github.com/vektra/mockery/v2/pkg/config"
Expand Down Expand Up @@ -46,6 +46,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,
Expand Down Expand Up @@ -215,9 +222,9 @@ func parseConfigTemplates(ctx context.Context, c *config.Config, iface *Interfac
InterfaceDir: filepath.Dir(iface.FileName),
InterfaceDirRelative: interfaceDirRelative,
InterfaceName: iface.Name,
InterfaceNameCamel: strcase.ToCamel(iface.Name),
InterfaceNameLowerCamel: strcase.ToLowerCamel(iface.Name),
InterfaceNameSnake: strcase.ToSnake(iface.Name),
InterfaceNameCamel: xstrings.ToCamelCase(iface.Name),
InterfaceNameLowerCamel: xstrings.FirstRuneToLower(xstrings.ToCamelCase(iface.Name)),
InterfaceNameSnake: xstrings.ToSnakeCase(iface.Name),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this have any behavior differences or are they strictly equivalent? I'm inclined to keep the old package populating these variables and just remove them in v3.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as far as I see, they behave absolutely the same.
But since they are deprecated I guess it's okay to keep code unchanged.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

InterfaceNameLower: strings.ToLower(iface.Name),
Mock: mock,
MockName: c.MockName,
Expand Down
23 changes: 23 additions & 0 deletions pkg/outputter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Loading