Skip to content

Commit 6c20c91

Browse files
authored
Merge pull request #705 from SirSova/feature/702
[Feature] Add new string template functions
2 parents 1ffe649 + 8b0b6a3 commit 6c20c91

File tree

6 files changed

+51
-7
lines changed

6 files changed

+51
-7
lines changed

docs/configuration.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,10 @@ Variables that are marked as being templated are capable of using mockery-provid
197197
| 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. |
198198
| 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` |
199199
| InterfaceName | The name of the original interface being mocked |
200-
| InterfaceNameCamel | Converts a string `interface_name` to `InterfaceName` |
201-
| InterfaceNameLowerCamel | Converts `InterfaceName` to `interfaceName` |
202-
| InterfaceNameSnake | Converts `InterfaceName` to `interface_name` |
203-
| InterfaceNameLower | Converts `InterfaceName` to `interfacename` |
200+
| InterfaceNameCamel | Converts a string `interface_name` to `InterfaceName`. <b>Deprecated</b>: use `{{ .InterfaceName \| camelcase }}` instead |
201+
| InterfaceNameLowerCamel | Converts `InterfaceName` to `interfaceName` . <b>Deprecated</b>: use `{{ .InterfaceName \| camelcase \| firstLower }}` instead |
202+
| InterfaceNameSnake | Converts `InterfaceName` to `interface_name` . <b>Deprecated</b>: use `{{ .InterfaceName \| snakecase }}` instead |
203+
| InterfaceNameLower | Converts `InterfaceName` to `interfacename` . <b>Deprecated</b>: use `{{ .InterfaceName \| lower }}` instead |
204204
| 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. |
205205
| MockName | The name of the mock that will be generated. Note that this is simply the `mockname` configuration variable |
206206
| 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
232232
* [`trimRight` string cutset](https://pkg.go.dev/strings#TrimRight)
233233
* [`trimSpace` string](https://pkg.go.dev/strings#TrimSpace)
234234
* [`trimSuffix` string suffix](https://pkg.go.dev/strings#TrimSuffix)
235+
* [`lower` string](https://pkg.go.dev/strings#ToLower)
236+
* [`upper` string](https://pkg.go.dev/strings#ToUpper)
237+
* [`camelcase` string](https://pkg.go.dev/github.com/huandu/xstrings#ToCamelCase)
238+
* [`snakecase` string](https://godoc.org/github.com/huandu/xstrings#ToSnakeCase)
239+
* [`kebabcase` string](https://godoc.org/github.com/huandu/xstrings#ToKebabCase)
240+
* [`firstLower` string](https://godoc.org/github.com/huandu/xstrings#FirstRuneToLower)
241+
* [`firstUpper` string](https://godoc.org/github.com/huandu/xstrings#FirstRuneToUpper)
235242
* [`matchString` pattern](https://pkg.go.dev/regexp#MatchString)
236243
* [`quoteMeta` string](https://pkg.go.dev/regexp#QuoteMeta)
237244
* [`base` string](https://pkg.go.dev/path/filepath#Base)

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.19
55
require (
66
github.com/chigopher/pathlib v0.15.0
77
github.com/davecgh/go-spew v1.1.1
8+
github.com/huandu/xstrings v1.4.0
89
github.com/iancoleman/strcase v0.2.0
910
github.com/jinzhu/copier v0.3.5
1011
github.com/mitchellh/go-homedir v1.1.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
128128
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
129129
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
130130
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
131+
github.com/huandu/xstrings v1.4.0 h1:D17IlohoQq4UcpqD7fDk80P7l+lwAmlFaBHgOipl2FU=
132+
github.com/huandu/xstrings v1.4.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
131133
github.com/iancoleman/strcase v0.2.0 h1:05I4QRnGpI0m37iZQRuskXh+w77mr6Z41lwQzuHLwW0=
132134
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
133135
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=

go.work.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ github.com/hashicorp/memberlist v0.5.0 h1:EtYPN8DpAURiapus508I4n9CzHs2W+8NZGbmmR
443443
github.com/hashicorp/memberlist v0.5.0/go.mod h1:yvyXLpo0QaGE59Y7hDTsTzDD25JYBZ4mHgHUZ8lrOI0=
444444
github.com/hashicorp/serf v0.10.1 h1:Z1H2J60yRKvfDYAOZLd2MU0ND4AH/WDz7xYHDWQsIPY=
445445
github.com/hashicorp/serf v0.10.1/go.mod h1:yL2t6BqATOLGc5HF7qbFkTfXoPIY0WZdWHfEvMqbG+4=
446+
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
446447
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639 h1:mV02weKRL81bEnm8A0HT1/CAelMQDBuQIfLw8n+d6xI=
447448
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
448449
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=

pkg/outputter.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"text/template"
1515

1616
"github.com/chigopher/pathlib"
17+
"github.com/huandu/xstrings"
1718
"github.com/iancoleman/strcase"
1819
"github.com/rs/zerolog"
1920

@@ -46,6 +47,13 @@ var templateFuncMap = template.FuncMap{
4647
"trimRight": strings.TrimRight,
4748
"trimSpace": strings.TrimSpace,
4849
"trimSuffix": strings.TrimSuffix,
50+
"lower": strings.ToLower,
51+
"upper": strings.ToUpper,
52+
"camelcase": xstrings.ToCamelCase,
53+
"snakecase": xstrings.ToSnakeCase,
54+
"kebabcase": xstrings.ToKebabCase,
55+
"firstLower": xstrings.FirstRuneToLower,
56+
"firstUpper": xstrings.FirstRuneToUpper,
4957

5058
// Regular expression matching
5159
"matchString": regexp.MatchString,
@@ -212,9 +220,11 @@ func parseConfigTemplates(ctx context.Context, c *config.Config, iface *Interfac
212220
PackageName string
213221
PackagePath string
214222
}{
215-
InterfaceDir: filepath.Dir(iface.FileName),
216-
InterfaceDirRelative: interfaceDirRelative,
217-
InterfaceName: iface.Name,
223+
InterfaceDir: filepath.Dir(iface.FileName),
224+
InterfaceDirRelative: interfaceDirRelative,
225+
InterfaceName: iface.Name,
226+
// Deprecated: All custom case variables of InterfaceName will be removed in the next major version
227+
// Use the template functions instead
218228
InterfaceNameCamel: strcase.ToCamel(iface.Name),
219229
InterfaceNameLowerCamel: strcase.ToLowerCamel(iface.Name),
220230
InterfaceNameSnake: strcase.ToSnake(iface.Name),

pkg/outputter_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,29 @@ func Test_parseConfigTemplates(t *testing.T) {
115115
Outpkg: "packageName",
116116
},
117117
},
118+
{
119+
name: "template funcs cases",
120+
args: args{
121+
c: &config.Config{
122+
Dir: "{{.InterfaceDir}}/{{.PackagePath}}",
123+
FileName: "{{.InterfaceName | kebabcase }}.go",
124+
MockName: "{{.InterfaceName | camelcase }}",
125+
Outpkg: "{{.PackageName | snakecase }}",
126+
},
127+
128+
iface: &Interface{
129+
Name: "FooBar",
130+
FileName: "path/to/foobar.go",
131+
},
132+
},
133+
pkg: mockPkg,
134+
want: &config.Config{
135+
Dir: "path/to/github.com/user/project/package",
136+
FileName: "foo-bar.go",
137+
MockName: "FooBar",
138+
Outpkg: "package_name",
139+
},
140+
},
118141
{
119142
name: "InterfaceDirRelative in current working directory",
120143
args: args{

0 commit comments

Comments
 (0)