httppayload

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2025 License: MIT Imports: 9 Imported by: 1

README

Install

go get github.com/canpacis/http-payload

Scanner

Scanner is a utility package to extract certain values and cast them to usable values in the context of http servers. It can extract request bodies, form values, url queries, cookies and headers. Scanner defines a Scanner interface for you to extend it to your own needs.

type Scanner interface {
  Scan(any) error
}

Example

An example that extracts header values from an http.Header object.

type Params struct {
  // provide the header name in the field tag
  Language string `header:"accept-language"`
}

// ...

// Create your instance
p := &Params{}
// Provide the request headers
s := payload.NewHeaderScanner(r.Headers())
if err := s.Scan(p); err != nil {
  // handle error
}
// p.Language -> r.Headers().Get("Accept-Language")

You can compose your scanners. There are a handful of pre-built scanners for the most common of use cases.

  • payload.JSONScanner: Scans json data from an io.Reader
  • payload.HeaderScanner: Scans header data from an *http.Header
  • payload.QueryScanner: Scans url query values from a *url.Values
  • payload.FormScanner: Scans form data from a *url.Values
  • payload.CookieScanner: Scans cookies from a http.CookieJar
  • payload.MultipartScanner: Scans multipart form data from a payload.MultipartValues
  • payload.ImageScanner: Scans multipart images from a payload.MultipartValues

eg.:

type Params struct {
  IDs      []string `query:"ids"`
  Language string   `header:"accept-language"`
  Token    string   `cookie:"token"`
}

// ...

// Create your instance
p := &Params{}
var s payload.Scanner

s = payload.NewQueryScanner(/* url.Values */)
s.Scan(p) // Don't forget to handle errors

s = payload.NewHeaderScanner(/* http.Header */)
s.Scan(p) // Don't forget to handle errors

s = payload.NewCookieScanner(/* http.CookieJar */)
s.Scan(p) // Don't forget to handle errors

Or, alternatively, you can use a payload.PipeScanner to streamline the process.

type Params struct {
  IDs      []string `query:"ids"`
  Language string   `header:"accept-language"`
  Token    string   `cookie:"token"`
}

// ...

// Create your instance
p := &Params{}
s := payload.NewPipeScanner(
  payload.NewQueryScanner(/* url.Values */),
  payload.NewHeaderScanner(/* http.Header */),
  payload.NewCookieScanner(/* http.CookieJar */),
)
s.Scan(p) // Don't forget to handle errors

This will populate your struct's fields with available values.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CookiePrinter

type CookiePrinter struct {
	// contains filtered or unexported fields
}

func NewCookiePrinter

func NewCookiePrinter(w http.ResponseWriter) *CookiePrinter

func (*CookiePrinter) Print

func (p *CookiePrinter) Print(v any) error

func (*CookiePrinter) Set

func (p *CookiePrinter) Set(key string, value any)

func (*CookiePrinter) SetField

func (p *CookiePrinter) SetField(key string, value any, field reflect.StructField)

type CookieScanner

type CookieScanner struct {
	// contains filtered or unexported fields
}

A scanner to scan http cookies for a url from a `http.CookieJar` to a struct

func NewCookieScanner

func NewCookieScanner(cookies []*http.Cookie) *CookieScanner

func (CookieScanner) Get

func (v CookieScanner) Get(key string) any

func (*CookieScanner) Scan

func (s *CookieScanner) Scan(v any) error

Scans the cookie values onto v

type FormScanner

type FormScanner struct {
	*url.Values
}

A scanner to scan form values from a `*url.Values` to a struct

func NewFormScanner

func NewFormScanner(v *url.Values) *FormScanner

func (FormScanner) Cast

func (v FormScanner) Cast(from any, to reflect.Type) (any, error)

func (FormScanner) Get

func (v FormScanner) Get(key string) any

func (*FormScanner) Scan

func (s *FormScanner) Scan(v any) error

Scans the form data onto v

type HeaderPrinter

type HeaderPrinter struct {
	// contains filtered or unexported fields
}

func NewHeaderPrinter

func NewHeaderPrinter(w http.ResponseWriter) *HeaderPrinter

func (*HeaderPrinter) Print

func (p *HeaderPrinter) Print(v any) error

func (*HeaderPrinter) Set

func (p *HeaderPrinter) Set(key string, value any)

type HeaderScanner

type HeaderScanner struct {
	*http.Header
}

A scanner to scan header values from an `http.Header` to a struct

func NewHeaderScanner

func NewHeaderScanner(h *http.Header) *HeaderScanner

func (*HeaderScanner) Get

func (h *HeaderScanner) Get(key string) any

func (*HeaderScanner) Scan

func (s *HeaderScanner) Scan(v any) error

Scans the headers onto v

type JSONPrinter added in v0.2.1

type JSONPrinter struct {
	// contains filtered or unexported fields
}

func NewJSONPrinter added in v0.2.1

func NewJSONPrinter(w http.ResponseWriter) *JSONPrinter

func (*JSONPrinter) Print added in v0.2.1

func (p *JSONPrinter) Print(v any) error

type JSONScanner

type JSONScanner struct {
	// contains filtered or unexported fields
}

A scanner to scan json value from an `io.Reader` to a struct

func NewJSONScanner

func NewJSONScanner(r io.Reader) *JSONScanner

func NewJSONScannerFromBytes

func NewJSONScannerFromBytes(b []byte) *JSONScanner

func (*JSONScanner) Scan

func (s *JSONScanner) Scan(v any) error

Scans the json onto v

type MultipartParser

type MultipartParser interface {
	ParseMultipartForm(int64) error
	FormFile(string) (multipart.File, *multipart.FileHeader, error)
}

type MultipartScanner

type MultipartScanner struct {
	// contains filtered or unexported fields
}

A scanner to scan multipart form values, files, from a `*scanner.MultipartValues` to a struct You can create a `*scanner.MultipartValues` instance with the `scanner.MultipartValuesFromParser` function.

func NewMultipartScanner

func NewMultipartScanner(v *MultipartValues) *MultipartScanner

func (*MultipartScanner) Scan

func (s *MultipartScanner) Scan(v any) error

Scans the multipart form data onto v

type MultipartValues

type MultipartValues struct {
	Files map[string]multipart.File
}

func MultipartValuesFromParser

func MultipartValuesFromParser(p MultipartParser, size int64, names ...string) (*MultipartValues, error)

MultipartValuesFromParser takes a generic parser that is usually an `*http.Request` and returns `*scanner.MultipartValues` to use it with a `scanner.MultipartScanner` or `scanner.ImageScanner`

func (MultipartValues) Get

func (v MultipartValues) Get(key string) any

type PathScanner

type PathScanner struct {
	*http.Request
}

A scanner to scan path parameters from a `*http.Request` to a struct

func NewPathScanner

func NewPathScanner(req *http.Request) *PathScanner

func (PathScanner) Cast

func (v PathScanner) Cast(from any, to reflect.Type) (any, error)

func (PathScanner) Get

func (v PathScanner) Get(key string) any

func (*PathScanner) Scan

func (s *PathScanner) Scan(v any) error

Scans the path parameters onto v

type PipePrinter

type PipePrinter []Printer

func NewPipePrinter

func NewPipePrinter(printers ...Printer) *PipePrinter

func (*PipePrinter) Print

func (s *PipePrinter) Print(v any) error

Runs given printers in sequence

type PipeScanner

type PipeScanner []Scanner

func NewPipeScanner

func NewPipeScanner(scanners ...Scanner) *PipeScanner

func (*PipeScanner) Scan

func (s *PipeScanner) Scan(v any) error

Runs given scanners in sequence

type Printer

type Printer interface {
	Print(any) error
}

type QueryScanner

type QueryScanner struct {
	*url.Values
}

A scanner to scan url query values from a `*url.Values` to a struct

func NewQueryScanner

func NewQueryScanner(v url.Values) *QueryScanner

func (QueryScanner) Cast

func (v QueryScanner) Cast(from any, to reflect.Type) (any, error)

func (QueryScanner) Get

func (v QueryScanner) Get(key string) any

func (*QueryScanner) Scan

func (s *QueryScanner) Scan(v any) error

Scans the query values onto v

type Scanner

type Scanner interface {
	Scan(any) error
}

Scanner interface resembles a json parser, it populates the given struct with available values based on its field tags. It should return an error when v is not a struct.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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