From 344a291f8e53302f10768739f2b1c9226a01c32e Mon Sep 17 00:00:00 2001 From: SirSova Date: Mon, 18 Sep 2023 13:30:50 +0300 Subject: [PATCH 1/5] add new template funcs list of funcs: - lower - upper - title - camelcase - snakecase - kebabcase - firstLower - firstUpper change the dependency for strings ops "iancoleman/strcase" -> "huandu/xstrings" --- docs/configuration.md | 8 ++++++++ go.mod | 2 +- go.sum | 4 ++-- go.work.sum | 1 + pkg/outputter.go | 16 ++++++++++++---- pkg/outputter_test.go | 23 +++++++++++++++++++++++ 6 files changed, 47 insertions(+), 7 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 21810355..efed6ac3 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -231,6 +231,14 @@ 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 suffix](https://pkg.go.dev/strings#ToLower) +* [`upper` string suffix](https://pkg.go.dev/strings#ToUpper) +* [`title` string suffix](https://pkg.go.dev/strings#ToTitle) +* [`camelcase` string suffix](https://pkg.go.dev/github.com/huandu/xstrings#ToCamelCase) +* [`snakecase` string suffix](https://godoc.org/github.com/huandu/xstrings#ToSnakeCase) +* [`kebabcase` string suffix](https://godoc.org/github.com/huandu/xstrings#ToKebabCase) +* [`firstLower` string suffix](https://godoc.org/github.com/huandu/xstrings#FirstRuneToLower) +* [`firstUpper` string suffix](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..42c15b78 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 641fc632..0a72f05c 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/go.work.sum b/go.work.sum index fb0cae71..1f74b819 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/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= diff --git a/pkg/outputter.go b/pkg/outputter.go index 10fb8025..de38b6a6 100644 --- a/pkg/outputter.go +++ b/pkg/outputter.go @@ -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" @@ -46,6 +46,14 @@ var templateFuncMap = template.FuncMap{ "trimRight": strings.TrimRight, "trimSpace": strings.TrimSpace, "trimSuffix": strings.TrimSuffix, + "lower": strings.ToLower, + "upper": strings.ToUpper, + "title": strings.ToTitle, + "camelcase": xstrings.ToCamelCase, + "snakecase": xstrings.ToSnakeCase, + "kebabcase": xstrings.ToKebabCase, + "firstLower": xstrings.FirstRuneToLower, + "firstUpper": xstrings.FirstRuneToUpper, // Regular expression matching "matchString": regexp.MatchString, @@ -215,9 +223,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), InterfaceNameLower: strings.ToLower(iface.Name), Mock: mock, MockName: c.MockName, 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{ From 7e620221bd544c3fc581af736c728b783cf184d6 Mon Sep 17 00:00:00 2001 From: SirSova Date: Mon, 18 Sep 2023 13:39:21 +0300 Subject: [PATCH 2/5] add deprecations for custom InterfaceName template variables --- docs/configuration.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index efed6ac3..ab3e25f4 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -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
`#!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 | From d09466479889f9fa46fd9200dcccc403ed55399f Mon Sep 17 00:00:00 2001 From: SirSova Date: Tue, 19 Sep 2023 13:53:21 +0300 Subject: [PATCH 3/5] remove "title" func --- docs/configuration.md | 1 - pkg/outputter.go | 1 - 2 files changed, 2 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index ab3e25f4..37e26de5 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -233,7 +233,6 @@ To learn more about the templating syntax, please [see the Go `text/template` do * [`trimSuffix` string suffix](https://pkg.go.dev/strings#TrimSuffix) * [`lower` string suffix](https://pkg.go.dev/strings#ToLower) * [`upper` string suffix](https://pkg.go.dev/strings#ToUpper) -* [`title` string suffix](https://pkg.go.dev/strings#ToTitle) * [`camelcase` string suffix](https://pkg.go.dev/github.com/huandu/xstrings#ToCamelCase) * [`snakecase` string suffix](https://godoc.org/github.com/huandu/xstrings#ToSnakeCase) * [`kebabcase` string suffix](https://godoc.org/github.com/huandu/xstrings#ToKebabCase) diff --git a/pkg/outputter.go b/pkg/outputter.go index de38b6a6..70c31223 100644 --- a/pkg/outputter.go +++ b/pkg/outputter.go @@ -48,7 +48,6 @@ var templateFuncMap = template.FuncMap{ "trimSuffix": strings.TrimSuffix, "lower": strings.ToLower, "upper": strings.ToUpper, - "title": strings.ToTitle, "camelcase": xstrings.ToCamelCase, "snakecase": xstrings.ToSnakeCase, "kebabcase": xstrings.ToKebabCase, From 2835751b77da2729f3be209f71e025a08a06210f Mon Sep 17 00:00:00 2001 From: SirSova Date: Tue, 19 Sep 2023 13:55:19 +0300 Subject: [PATCH 4/5] fix doc syntax of new functions --- docs/configuration.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 37e26de5..d6c9dd47 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -231,13 +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 suffix](https://pkg.go.dev/strings#ToLower) -* [`upper` string suffix](https://pkg.go.dev/strings#ToUpper) -* [`camelcase` string suffix](https://pkg.go.dev/github.com/huandu/xstrings#ToCamelCase) -* [`snakecase` string suffix](https://godoc.org/github.com/huandu/xstrings#ToSnakeCase) -* [`kebabcase` string suffix](https://godoc.org/github.com/huandu/xstrings#ToKebabCase) -* [`firstLower` string suffix](https://godoc.org/github.com/huandu/xstrings#FirstRuneToLower) -* [`firstUpper` string suffix](https://godoc.org/github.com/huandu/xstrings#FirstRuneToUpper) +* [`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) From 8b0b6a318c7d5b44b8db34626733abf218873535 Mon Sep 17 00:00:00 2001 From: SirSova Date: Tue, 26 Sep 2023 13:50:00 +0300 Subject: [PATCH 5/5] return back "strcase" functions for InterfaceName add deprecation comment --- go.mod | 1 + go.sum | 2 ++ go.work.sum | 2 +- pkg/outputter.go | 15 +++++++++------ 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index 42c15b78..0a52d015 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ 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 github.com/mitchellh/mapstructure v1.5.0 diff --git a/go.sum b/go.sum index 0a72f05c..e6962530 100644 --- a/go.sum +++ b/go.sum @@ -130,6 +130,8 @@ 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= 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= diff --git a/go.work.sum b/go.work.sum index 1f74b819..2a9b8253 100644 --- a/go.work.sum +++ b/go.work.sum @@ -443,7 +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/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 70c31223..2f1a8192 100644 --- a/pkg/outputter.go +++ b/pkg/outputter.go @@ -15,6 +15,7 @@ import ( "github.com/chigopher/pathlib" "github.com/huandu/xstrings" + "github.com/iancoleman/strcase" "github.com/rs/zerolog" "github.com/vektra/mockery/v2/pkg/config" @@ -219,12 +220,14 @@ func parseConfigTemplates(ctx context.Context, c *config.Config, iface *Interfac PackageName string PackagePath string }{ - InterfaceDir: filepath.Dir(iface.FileName), - InterfaceDirRelative: interfaceDirRelative, - InterfaceName: iface.Name, - InterfaceNameCamel: xstrings.ToCamelCase(iface.Name), - InterfaceNameLowerCamel: xstrings.FirstRuneToLower(xstrings.ToCamelCase(iface.Name)), - InterfaceNameSnake: xstrings.ToSnakeCase(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), InterfaceNameLower: strings.ToLower(iface.Name), Mock: mock, MockName: c.MockName,