dps43378 2014-09-21 10:30 采纳率: 0%
浏览 40
已采纳

如何重构指针的重复Golang代码库用法

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条回答 默认 最新

  • doudang2817 2014-09-21 12:06
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波