英文:
How to refactor repeated Golang codebase usage of pointer
问题
重构重复的代码的最佳方法是什么?我查看了一些关于Golang的不同方法,但没有快速找到有用的内容。我还在使用go-restful
包和Swagger。
我的主要关注点是所有的ws
都存在重复。如果我在ApiResource
的接收器上使用解引用运算符和指针,是否会更好?
func (api *ApiResource) registerLostLogin(container *restful.Container) {
ws := new(restful.WebService)
ws.
Path("/lostlogin").
Doc("Lost login").
Consumes(restful.MIME_JSON, restful.MIME_XML).
Produces(restful.MIME_JSON, restful.MIME_JSON) // you can specify this per route as well
ws.Route(ws.POST("").To(api.authenticate).
Doc("Performs lost login actions").
Operation("lostlogin").
Reads(LostLogin{})) // from the request
container.Add(ws)
}
func (api *ApiResource) registerAccount(container *restful.Container) {
ws := new(restful.WebService)
ws.
Path("/account").
Doc("Account calls").
Consumes(restful.MIME_JSON, restful.MIME_XML).
Produces(restful.MIME_JSON, restful.MIME_JSON) // you can specify this per route as well
ws.Route(ws.POST("").To(api.authenticate).
Doc("Register calls").
Operation("register").
Reads(Account{}))
ws.Route(ws.PUT("").To(api.authenticate).
Doc("Modify user details").
Operation("modifyUserDetails").
Reads(Account{}))
ws.Route(ws.PUT("security").To(api.authenticate).
Doc("Modify security question").
Operation("modifySeucirtyQuestion").
Reads(Account{}))
ws.Route(ws.PUT("limit").To(api.authenticate).
Doc("Modify limit").
Operation("modifyLimit").
Reads(Account{}))
ws.Route(ws.PUT("password").To(api.authenticate).
Doc("Modify password").
Operation("modifyPassword").
Reads(Account{}))
container.Add(ws)
}
func main() {
// to see what happens in the package, uncomment the following
restful.TraceLogger(log.New(os.Stdout, "[restful] ", log.LstdFlags|log.Lshortfile))
wsContainer := restful.NewContainer()
api := &ApiResource{map[string]intapi.OxiResp{}}
api.registerLogin(wsContainer)
api.registerAccount(wsContainer)
api.registerLostLogin(wsContainer)
api.registerWallet(wsContainer)
config := swagger.Config{
WebServices: wsContainer.RegisteredWebServices(), // you control what services are visible
WebServicesUrl: "http://localhost:8080",
ApiPath: "/apidocs.json",
SwaggerPath: "/apidocs/",
SwaggerFilePath: "/Users/aa/IdeaProjects/go_projects/src/test1/src/swagger-ui/dist",
DisableCORS: false}
swagger.RegisterSwaggerService(config, wsContainer)
log.Printf("start listening on localhost:8080")
server := &http.Server{Addr: ":8080", Handler: wsContainer}
log.Fatal(server.ListenAndServe())
}
通过在ApiResource
的接收器上使用指针,可以避免重复创建ws
对象,并且可以在方法中修改ApiResource
的状态。这样可以减少代码重复并提高代码的可维护性。
英文:
What is the best way to refactor repeated code base like below? I looked at a few different methods on Golang but wasn't able to find something useful quickly. I'm also using go-restful
package with Swagger
func (api ApiResource) registerLostLogin(container *restful.Container) {
ws := new(restful.WebService)
ws.
Path("/lostlogin").
Doc("Lost login").
Consumes(restful.MIME_JSON, restful.MIME_XML).
Produces(restful.MIME_JSON, restful.MIME_JSON) // you can specify this per route as well
ws.Route(ws.POST("").To(api.authenticate).
Doc("Performs lost login actions").
Operation("lostlogin").
Reads(LostLogin{})) // from the request
container.Add(ws)
}
func (api ApiResource) registerAccount(container *restful.Container) {
ws := new(restful.WebService)
ws.
Path("/account").
Doc("Account calls").
Consumes(restful.MIME_JSON, restful.MIME_XML).
Produces(restful.MIME_JSON, restful.MIME_JSON) // you can specify this per route as well
ws.Route(ws.POST("").To(api.authenticate).
Doc("Register calls").
Operation("register").
Reads(Account{}))
ws.Route(ws.PUT("").To(api.authenticate).
Doc("Modify user details").
Operation("modifyUserDetails").
Reads(Account{}))
ws.Route(ws.PUT("security").To(api.authenticate).
Doc("Modify security question").
Operation("modifySeucirtyQuestion").
Reads(Account{}))
ws.Route(ws.PUT("limit").To(api.authenticate).
Doc("Modify limit").
Operation("modifyLimit").
Reads(Account{}))
ws.Route(ws.PUT("password").To(api.authenticate).
Doc("Modify password").
Operation("modifyPassword").
Reads(Account{}))
container.Add(ws)
}
func main() {
// to see what happens in the package, uncomment the following
restful.TraceLogger(log.New(os.Stdout, "[restful] ", log.LstdFlags|log.Lshortfile))
wsContainer := restful.NewContainer()
api := ApiResource{map[string]intapi.OxiResp{}}
api.registerLogin(wsContainer)
api.registerAccount(wsContainer)
api.registerLostLogin(wsContainer)
api.registerWallet(wsContainer)
config := swagger.Config{
WebServices: wsContainer.RegisteredWebServices(), // you control what services are visible
WebServicesUrl: "http://localhost:8080",
ApiPath: "/apidocs.json",
SwaggerPath: "/apidocs/",
SwaggerFilePath: "/Users/aa/IdeaProjects/go_projects/src/test1/src/swagger-ui/dist",
DisableCORS: false}
swagger.RegisterSwaggerService(config, wsContainer)
log.Printf("start listening on localhost:8080")
server := &http.Server{Addr: ":8080", Handler: wsContainer}
log.Fatal(server.ListenAndServe())
}
My main concern is duplication around all ws being duplicated across. Is it better if I use deference operator and pointer for the receiver with ApiResource?
答案1
得分: 1
你可以将它们封装起来,可能像这样:
type registration func(container *restful.Container, ws *restul.WebService)
func registerHandlers(handlers ...registration) {
c := restful.NewContainer()
ws := new(restful.WebService)
for _, h := range handlers {
h(c, ws)
}
}
然后调用它,传入所有的方法:
api := ApiResource{map[string]intapi.OxiResp{}}
registerHandlers(api.registerLostLogin,
api.registerAccount)
然后你只需要在你的方法中接受 WebService
实例:
func (api ApiResource) registerAccount(container *restful.Container, ws *restful.WebService)
这样至少可以在方法调用之间重用 WebService
实例。虽然需要多写一点代码。
英文:
You can certainly wrap them up.. perhaps something like this:
type registration func(container *restful.Container, ws *restul.WebService)
func registerHandlers(handlers ...registration) {
c := restful.NewContainer()
ws := new(restful.WebService)
for _, h := range handlers {
h(c, ws)
}
}
Then call it, passing in all of your methods:
api := ApiResource{map[string]intapi.OxiResp{}}
registerHandlers(api.registerLostLogin,
api.registerAccount)
Then you just need to accept the WebService
instance in your methods:
func (api ApiResource) registerAccount(container *restful.Container, ws *restful.WebService)
That at least re-uses the WebService
instance across method calls. A little bit more code though.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论