binder

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2024 License: Apache-2.0 Imports: 11 Imported by: 1

README

binder

HTTP request data binder.

GitHub tag (latest SemVer) Tests CodeQL Analysis GolangCI Lint Go Report Card Go Reference License

Features

  • Bind query string parameters to struct fields
  • Bind form values to struct fields
  • Bind JSON body to struct fields
  • Get file from multipart form
  • Bind multipart form values to struct fields (limited support, see supported types)
  • Binder interface implementation
Supported types

Supported types for binding from multipart form:

  • string
  • int, int8, int16, int32, int64
  • uint, uint8, uint16, uint32, uint64
  • float32, float64
  • bool
  • map[string]interface{}
  • *binder.File & binder.File
  • time.Time
  • []string
  • []int, []int8, []int16, []int32, []int64
  • []uint, []uint8, []uint16, []uint32, []uint64
  • []float32, []float64
  • []bool
  • []*binder.File & []binder.File
  • []time.Time

Installation

go get -u github.com/dmitrymomot/binder

Usage

Comming soon...

Documentation

Index

Constants

View Source
const (
	// JSON struct tag name for binding
	TagJSON = "json"
	// Form struct tag name for binding
	TagForm = "form"
	// Query struct tag name for binding
	TagQuery = "query"
	// Header struct tag name for binding
	TagHeader = "header"
)

Default tag names for binding

Variables

View Source
var (
	ErrInvalidMethod        = errors.New("invalid http method for binding")
	ErrInvalidContentType   = errors.New("invalid content type for binding")
	ErrEmptyBody            = errors.New("empty request body")
	ErrParseForm            = errors.New("failed to parse form")
	ErrDecodeForm           = errors.New("failed to decode form")
	ErrGetFile              = errors.New("failed to get file from request")
	ErrReadFile             = errors.New("failed to read file from request")
	ErrGetFileMimeType      = errors.New("failed to get file mime type")
	ErrEmptyQuery           = errors.New("empty query string")
	ErrDecodeQuery          = errors.New("failed to decode request query")
	ErrInvalidInput         = errors.New("invalid input")
	ErrUnsupportedType      = errors.New("unsupported type")
	ErrTargetMustBeAPointer = errors.New("target must be a pointer")
	ErrTargetMustBeAStruct  = errors.New("target must be a struct")
	ErrInputIsNil           = errors.New("input is nil")
	ErrDecodeJSON           = errors.New("failed to decode json")
)

Predefined errors

View Source
var MultiPartFormMaxMemory int64 = 32 << 20

MultiPartFormMaxMemory is the maximum amount of memory to use when parsing a multipart form. It is passed to http.Request.ParseMultipartForm. Default value is 32 << 20 (32 MB).

Functions

func BindForm

func BindForm(r *http.Request, v interface{}) error

BindForm binds the passed v pointer to the request. It uses the application/x-www-form-urlencoded content type for binding. `v` param should be a pointer to a struct with `form“ tags. Implements the binder.BinderFunc interface.

func BindFormMultipart

func BindFormMultipart(r *http.Request, v interface{}) error

BindFormMultipart binds the passed v pointer to the request. It uses the multipart/form-data content type for binding. `v` param should be a pointer to a struct with `form“ tags. Implements the binder.BinderFunc interface.

func BindFunc added in v0.2.0

func BindFunc(r *http.Request, v interface{}) error

BindFunc is the function type that implements the BinderFunc interface. It returns an error if the binding fails. Binding depends on the request method and the content type. If the request method is GET, HEAD, DELETE, or OPTIONS, then the binding is done from the query. If the request method is POST, PUT, or PATCH, then the binding is done from the request body. If the content type is JSON, then the binding is done from the request body. If the content type is form, then the binding is done from the request body.

func BindJSON

func BindJSON(r *http.Request, v interface{}) error

BindJSON binds the passed v pointer to the request. It uses the JSON content type for binding. `v` param should be a pointer to a struct with `json` tags. Implements the binder.BinderFunc interface.

func BindQuery

func BindQuery(r *http.Request, v interface{}) error

BindQuery binds the passed v pointer to the request. It uses the query string for binding. `v` param should be a pointer to a struct with `query“ tags. Implements the binder.BinderFunc interface.

func FileDataConverter

func FileDataConverter(v string) reflect.Value

FileDataConverter is the function type that converts a string to a reflect.Value. The string is the file data.

func FileDataConverterPtr

func FileDataConverterPtr(v string) reflect.Value

FileDataConverterPtr is the function type that converts a string to a reflect.Value. The string is the file data.

func GetFileMimeType

func GetFileMimeType(input []byte) (string, error)

GetFileMimeType returns the MIME type of a file using the github.com/gabriel-vasile/mimetype package.

Types

type Binder

type Binder interface {
	Bind(r *http.Request, v interface{}) error
}

Binder is the interface that wraps the Bind method.

Bind should bind the passed v pointer to the request. For example, the implementation could bind the request body to the v pointer.

type BinderFunc

type BinderFunc func(*http.Request, interface{}) error

BinderFunc is the function type that implements the Binder interface.

type DefaultBinder added in v0.2.0

type DefaultBinder struct{}

DefaultBinder is the default implementation of the Binder interface.

func (*DefaultBinder) Bind added in v0.2.0

func (b *DefaultBinder) Bind(r *http.Request, v interface{}) error

Bind binds the passed v pointer to the request. For example, the implementation could bind the request body to the v pointer. Bind implements the Binder interface. It returns an error if the binding fails. Binding depends on the request method and the content type. If the request method is GET, HEAD, DELETE, or OPTIONS, then the binding is done from the query. If the request method is POST, PUT, or PATCH, then the binding is done from the request body. If the content type is JSON, then the binding is done from the request body. If the content type is form, then the binding is done from the request body.

type File

type File struct {
	// FileName stores the name of the file
	FileName string
	// FileSize stores the size of the file in bytes
	FileSize int64
	// ContentType stores the MIME type of the file
	ContentType string
	// Data is a byte slice that holds the contents of the file
	Data []byte
}

File represents a file that was uploaded via a multipart form.

type FileData

type FileData struct {
	// Name is the filename.
	Name string
	// Size is the size of the file in bytes.
	Size int64
	// MimeType is the MIME type of the file, as determined by its contents or extension.
	MimeType string
	// Data contains the raw bytes of the file.
	Data []byte
}

FileData represents the data contained in a file, including metadata and the file contents.

func GetFileData

func GetFileData(req *http.Request, fieldName string) (*FileData, error)

GetFileData extracts file data from a multipart request.

func GetFileDataFromMultipartFileHeader

func GetFileDataFromMultipartFileHeader(header *multipart.FileHeader) (*FileData, error)

GetFileDataFromMultipartFileHeader extracts file data from a multipart file header.

Jump to

Keyboard shortcuts

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