Skip to content

alikarimii/binder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Binder

binder is a form/param/query/header converter of http request to struct.this package is custom version of bind in echo framework for mux router.

install

go get github.com/alikarimii/binder

How it's work

package main

import (
  "github.com/alikarimii/binder"
  "github.com/gorilla/mux"
)

type body struct {
    Name string `form:"name"`
    Age int `form:"age"`
}

type param struct {
    User string `param:"user"`
    ID   int `param:"id"`
}
type query struct {
    IsAdmin bool `query:"is_admin"`
    Test int `query:"test"` 
}

func main() {
    b := binder.NewBinder()
    r := mux.NewRouter()
    r.Methods("POST").Path("/api").HandlerFunc(func(w http.ResponseWriter, r *http.Request){
       var mystruct body
       err := b.Bind(&mystruct, r)
       ...
      })
    r.Methods("GET").Path("/api/{user}/{id}").HandlerFunc(func(w http.ResponseWriter, r *http.Request){

       var mystruct param
       err := b.Bind(&mystruct, r)
       ...

      }
    r.Methods("GET").Path("/api?is_admin=true&test=2").HandlerFunc(func(w http.ResponseWriter, r *http.Request){

       var mystruct query
       err := b.Bind(&mystruct, r)
       ...
      }

      ...
  }

About

body,param,query,header of request to struct

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages