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

Export the mockgen API as a library for use by other projects in generating mock codes. #124

Open
sysulq opened this issue Nov 15, 2023 · 6 comments · May be fixed by #184
Open

Export the mockgen API as a library for use by other projects in generating mock codes. #124

sysulq opened this issue Nov 15, 2023 · 6 comments · May be fixed by #184
Labels

Comments

@sysulq
Copy link

sysulq commented Nov 15, 2023

I have a project here, github.com/sysulq/struct2interface, which converts structs into interfaces. I'm looking to take it a step further—being able to automatically generate mock structures based on interfaces. Currently, I can only generate them directly using mockgen binary. It would be perfect if it could support a library format.

Thanks in advance~

@sywhang sywhang added good first issue Good for newcomers wishlist and removed good first issue Good for newcomers labels Nov 21, 2023
@sysulq sysulq linked a pull request Jun 18, 2024 that will close this issue
@r-hang
Copy link
Contributor

r-hang commented Jun 18, 2024

Hey @sysulq,

From my experience, it's unusual to use a code generator as a library. In your program, could you shell out to mockgen to produce your mocks?

@sysulq
Copy link
Author

sysulq commented Jun 19, 2024

@r-hang Thanks for your reply. :-)

What I did is just call mockgen through shell cmd, but I think it's more natural to call as a library in my situation, without the need to install mockgen.

I created a tool to auto create interface base on struct, and I want to go further, continue to create the mocks based on the interfaces auto created.

And here is the code in below link:

@sysulq
Copy link
Author

sysulq commented Jun 19, 2024

A typical example is the air project, which provide a runner pkg to be imported by thirdparty project directly as a library.

@r-hang
Copy link
Contributor

r-hang commented Jun 25, 2024

Hey @sysulq

I spent some time thinking about this PR.

From what i've seen, code generators don't generally take library form because the authors don't intend code generators to be used with other code. The only exception I can think of are code generation plugins used within tightly coupled ecosystems (e.g. protoc, thriftrw). Here, there is a strong contract within the ecosystem.

I'm hesitant to expose the code generator CLI as a library because the API for a code generator is bigger than that of a library. For example, we can't know whether or not a change to generated code won't conflict with or break other generator outputs. From my perspective, a code generator as an importable API creates an API surface that is hard to maintain.

@sysulq
Copy link
Author

sysulq commented Jun 26, 2024

@r-hang Thank you for taking the time to respond.

In fact we are only exposing a single API, api.Generate, for code generation. This is a very concise, streamlined, and extensible approach, and it actually has very low maintenance costs for the code.

Moreover, for uber-go/mock, it is equivalent to expanding the scope of usage scenarios, which I believe can significantly increase Mock's long-term utilization rate. I sincerely suggest that you support this feature.

Additionally, Mockery's code generation API can be accessed via this link.

@sysulq
Copy link
Author

sysulq commented Jul 3, 2024

Another typical example can be found in gorm/gen, which also can be imported as a library directly :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants