binder

package module
v0.0.0-...-72314a1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 23, 2021 License: GPL-3.0 Imports: 12 Imported by: 1

README

Binder

Golang http Request body binder

Installation

Golang version 1.11 + required

go get github.com/eatMoreApple/binder

Quick Start

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"github.com/eatMoreApple/binder"
	"net/url"
	"time"
)

type Address struct {
	Province string `json:"province"`
	City     string `json:"city"`
}

type User struct {
	Name     string `form:"name"`
	Age      int
	Married  bool      `form:"-"` // ignore this field
	Male     bool      `form:"male"`
	Hobbies  []string  `form:"hobbies"`
	Address  Address   `form:"address"`
	Birthday time.Time `bind:"time" form:"birthday"` // add custom method
}

func main() {
	// Bind Request Form
	bind := binder.DefaultFormBinder()
	// add custom method on this binder
	bind.AddCustomBindFormMethod("time", binder.FormatTimeBinder("2006-01-02 15:04:05"))
	var form = url.Values{
		"name":     {"eatMoreApple"},
		"Age":      {"25"},
		"Married":  {"1"},
		"male":     {"1"},
		"hobbies":  {"coding", "Playing games"},
		"address":  {`{"province": "浙江", "city": "杭州"}`},
		"birthday": {"1996-11-03 00:00:00"},
	}
	var user User

	bind.Bind(form, &user)

	fmt.Printf("%+v\n", user)
	// {Name:eatMoreApple Age:25 Married:false Male:true Hobbies:[coding Playing games] Address:{Province:浙江 City:杭州} Birthday:1996-11-03 00:00:00 +0000 UTC}

	// Bind Request Body
	data, _ := json.Marshal(user)
	var buffer = bytes.NewBuffer(data)
	var user2 User
	binder.JsonBodyBinder.Bind(buffer, &user2)
	fmt.Printf("%+v\n", user2)
	// {Name:eatMoreApple Age:25 Married:false Male:true Hobbies:[coding Playing games] Address:{Province:浙江 City:杭州} Birthday:1996-11-03 00:00:00 +0000 UTC}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// may be used outside
	EmptyMultipartFormError = errors.New("nil *multipart.Form got")
)
View Source
var JsonBodyBinder = NewBodyBinder(json.Unmarshal)
View Source
var XmlBodyBinder = NewBodyBinder(xml.Unmarshal)

Functions

func AddCustomBindFormMethod

func AddCustomBindFormMethod(name string, method CustomBindMethod) error

func BindForm

func BindForm(form url.Values, v interface{}) error

func BindMultipartForm

func BindMultipartForm(form *multipart.Form, v interface{}) error

Types

type BodyBinder

type BodyBinder struct {
	Unmarshaler func([]byte, interface{}) error
}

func NewBodyBinder

func NewBodyBinder(unmarshaler func([]byte, interface{}) error) *BodyBinder

func (BodyBinder) Bind

func (d BodyBinder) Bind(reader io.Reader, v interface{}) error

type CustomBindMethod

type CustomBindMethod func(reflect.Value, []string) error

func FormatTimeBinder

func FormatTimeBinder(format string) CustomBindMethod

func Int64TimeBinder

func Int64TimeBinder() CustomBindMethod

type CustomFormBinder

type CustomFormBinder interface {
	BindForm(values url.Values) error
}

type CustomMultipartFormBinder

type CustomMultipartFormBinder interface {
	BindMultipartForm(form *multipart.Form) error
}

type FormBinder

type FormBinder struct {
	TagName           string
	CustomBindTagName string
	BindMethods       map[string]CustomBindMethod
}

func DefaultFormBinder

func DefaultFormBinder() *FormBinder

func NewFormBinder

func NewFormBinder(tagName string) *FormBinder

func (*FormBinder) AddCustomBindFormMethod

func (f *FormBinder) AddCustomBindFormMethod(name string, method CustomBindMethod) error

func (*FormBinder) Bind

func (f *FormBinder) Bind(form url.Values, v interface{}) error

type MultipartFormBinder

type MultipartFormBinder struct {
	*FormBinder
	FieldTag string
}

func (*MultipartFormBinder) Bind

func (m *MultipartFormBinder) Bind(form *multipart.Form, v interface{}) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL