sunboyy / repogen Goto Github PK
View Code? Open in Web Editor NEWA code generator for MongoDB repository in Golang
License: MIT License
A code generator for MongoDB repository in Golang
License: MIT License
Nice generator package!
Is your feature request related to a problem? Please describe.
With dependency injection pattern, it's common that controller package defines the repository interface and accept it as input. However current implementation requires that repository interface and model type located within the same package.
Describe the solution you'd like
It would be nice if it could support the setup like this.
examples/ref_exp
├── ctrl
│ └── ctrl.go
├── repo
│ └── user_repo.go // generated
└── user.go
// ctrl.go
package ctrl
//go:generate repogen -pkg=.. -dest=../repo/user_repo.go -model=UserModel -repo=UserRepository
// UserRepository is an interface that describes the specification of querying
// user data in the database.
type UserRepository interface {
// InsertOne stores userModel into the database and returns inserted ID
// if insertion succeeds and returns error if insertion fails.
InsertOne(ctx context.Context, userModel *ref_exp.UserModel) (interface{}, error)
// FindByUsername queries user by username. If a user with specified
// username exists, the user will be returned. Otherwise, error will be
// returned.
FindByUsername(ctx context.Context, username string) (*ref_exp.UserModel, error)
...
}
func NewController(userRepo UserRepository ... )...
Additional context
Just play around the code base. For this feature these might be related:
Model
located. (to be used in RepositoryGenerator.Imports()
)interfaceMethodParser
there are some operation mode checking. And about the type comparison. Not sure how to allow imported types like *ref_exp.UserModel
in args and return. (Currently it will check with p.StructModel
).Is your feature request related to a problem? Please describe.
generated repository should return concrete type instead of interface
currently NewUserRepository
in example is returning interface.
However as suggestion in go codereview
Go interfaces generally belong in the package that uses values of the interface type, not the package that implements those values. The implementing package should return concrete (usually pointer or struct) types 1
*UserRepositoryMongo
should be returned.
btw you can assert the implementation in test file
// user_repo_test.go
var _ UserRepository = (*UserRepositoryMongo)(nil)
Describe the solution you'd like
return concrete type
Just found this great generator but missing one thing to use it in our project with data model generated by other tool (OpenAPI spec generator)
Is your feature request related to a problem? Please describe.
I'm using model from pkg auto-generated from OpenAPI spec so would not want to copy it everytime into file where interface is.
Describe the solution you'd like
Ignore -model
flag, just detect it in from the interface methods definitions (copy the same?) during generation.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.