doupai6875
2016-10-31 18:51
浏览 128
已采纳

从Golang中的接口创建类型的变量

I am trying to create validator/binder middleware in go using gin framework.

This is the model

type LoginForm struct{
    Email string `json:"email" form:"email" binding:"email,required"`
    Password string `json:"password" form:"password" binding:"required"`
}

Router

router.POST("/login",middlewares.Validator(LoginForm{}) ,controllers.Login)

Middleware

func Validator(v interface{}) gin.HandlerFunc{
    return func(c *gin.Context){
        a := reflect.New(reflect.TypeOf(v))
        err:=c.Bind(&a)
        if(err!=nil){
            respondWithError(401, "Login Error", c)
            return
        }
        c.Set("LoginForm",a)
        c.Next()
    }
}

I am very new to golang. I understand the problem is with the binding to the wrong variable. Is there any other way of solving this?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • drpzr64329 2016-10-31 19:37
    已采纳

    Clarify my comment,

    Instead of having the signature func Validator(v interface{}) gin.HandlerFunc for the MW, use func Validator(f Viewfactory) gin.HandlerFunc

    Where ViewFactory if a function type such as type ViewFactory func() interface{}

    the MW can be changed so

    type ViewFactory func() interface{}
    
    func Validator(f ViewFactory) gin.HandlerFunc{
        return func(c *gin.Context){
            a := f()
            err:=c.Bind(a) // I don t think you need to send by ref here, to check by yourself
            if(err!=nil){
                respondWithError(401, "Login Error", c)
                return
            }
            c.Set("LoginForm",a)
            c.Next()
        }
    }
    

    You can write the router like this

    type LoginForm struct{
        Email string `json:"email" form:"email" binding:"email,required"`
        Password string `json:"password" form:"password" binding:"required"`
    }
    func NewLoginForm() interface{} {
       return &LoginForm{}
    }
    router.POST("/login",middlewares.Validator(NewLoginForm) ,controllers.Login)
    

    Going further, i think you may have to find out about this later, once you have an interface{} value, you can make it back a LoginForm like this v := some.(*LoginForm).

    Or like this for more security

    if v, ok := some.(*LoginForm); ok {
     // v is a *LoginForm
    }
    

    See golang type assertions for more in depth information.

    打赏 评论

相关推荐 更多相似问题