Search code examples
gomiddlewarego-gin

Creating a variable of type from interface in 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?


Solution

  • 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.