-
Notifications
You must be signed in to change notification settings - Fork 426
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
Using mockery V3 to generate github.com/hexdigest/gowrap style interface wrappers #936
Comments
That being said, in regards to performance, the approach using |
One more thing: |
This is a really interesting perspective. I knew of the From what I'm understanding, you are essentially wanting to run mockery with multiple separate templates, some for mocks, others for middlewares. The core problem with that idea is that each template will accept different templates:
- name: "file://path/to/template.txt"
data: #template-data
- name: matryer
- name: moq I'm just bouncing random ideas around, I'm interested to hear your thoughts. |
For me, it was always clear, that mockery could become a more generic tool for generating code based on interfaces, if it will be extended such that it operates on Go templates. I already mentioned this 2 years back in #715 (comment) (at the bottom). That being said, back to your questions:
Example ---
all: False
template: matryer
force-file-write: true
include-regex: "some regex to match the interfaces we would like to generate mocks for"
dir: >-
{{- $targetDir := "mock" -}}
{{- if and (.InterfaceName | hasSuffix "SomeSuffix") -}}
{{- $targetDir = "some/mock" -}}
{{- end -}}
{{- if .InterfaceName | hasSuffix "OtherSuffix" -}}
{{- $targetDir = "other/mock" -}}
{{- end -}}
{{- .InterfaceDir -}}/{{- $targetDir -}}
filename: "{{ .InterfaceName | snakecase }}_mock_gen.go"
mockname: "{{ .InterfaceName }}Mock"
pkgname: mock
packages:
github.com/org/one:
github.com/org/other: and the an example for the wrappers: ---
all: False
template: file://./path/to/prometheus.gotmpl
force-file-write: true
include-regex: "some regex to match the interfaces we would like to generate wrappers for"
exclude-regex: "maybe even an exclude regex, since we do not need wrappers for these interfaces"
dir: >-
{{- $targetDir := "middleware" -}}
{{- if and (.InterfaceName | hasSuffix "SomeSuffix") -}}
{{- $targetDir = "some/middleware" -}}
{{- end -}}
{{- if .InterfaceName | hasSuffix "OtherSuffix" -}}
{{- $targetDir = "other/middleware" -}}
{{- end -}}
{{- .InterfaceDir -}}/{{- $targetDir -}}
filename: >-
{{ .InterfaceName | snakecase }}_prometheus_gen.go
mockname: "{{ .InterfaceName }}WithPrometheus"
pkgname: middleware
packages:
github.com/org/one:
github.com/org/other: After putting this together, I start to think, that maybe it is simpler to just add an additional (optional) top level key to the config file, which allows to combine the 3 config files into a single config file but allows all other settings to be distinct, similar to what you already proposed, but then not limited to Something like: templates:
- name: "file://path/to/template.txt"
config:
include-regex:
dir:
filename:
...
- name: matryer
include-regex:
dir:
filename:
...
- name: moq
include-regex:
dir:
filename:
... One side note: with the "extended" use of |
I tried to use mockery V3 in the new matryer/moq mode to generate interface wrappers in the style of https://github.com/hexdigest/gowrap and I made it work, but I found some rough edges, that I would like to mention here:
gowrap
offers some helpers for usage in templates, that come in handy when generating interface wrappers, namely:.HasParams
and.HasResults
onMethodData
. Withmockery
I needed to write the condition{{ if gt ($method.Returns | len) 0 }}
where ingowrap
it is simply{{ if $method.HasResults }}
.AcceptsContext
and.ReturnsError
onMethodData
. Withmockery
I needed create helper variables like this{{- if $returnsError }}
, which is cumbersome.What made this additionally cumbersome is, that Go templates do no provide basic arithmetic in order to get the last return value with
($method.Returns | len) - 1
(this would be possible with e.g. the math functions provided by sprig). Therefore I needed to range though the list only to get the last item.$method.Pass
, which accepts a prefix (basically the selector of the attribute of the wrapped instance). It returns the complete call to to the wrapped method including the arguments as well as thereturn
statement, if the method does return anything. My workaround withmockery
looks like this:gowrap
there is a helper{{$method.Declaration}}
and withmockery
I needed to do{{.Name}}({{.ArgList}}) ({{.ReturnArgList}})
.gowrap
, the name of the template is added as a comment to the generated file. I was not able to find a template variable, which contains the name of the Go template, that has been used to generate the source file. (example: https://github.com/hexdigest/gowrap/blob/master/templates_tests/interface_with_log.go#L2)The text was updated successfully, but these errors were encountered: