scanner

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2024 License: MIT Imports: 12 Imported by: 0

README

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 := scanner.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.

  • scanner.JsonScanner: Scans json data from an io.Reader
  • scanner.HeaderScanner: Scans header data from an *http.Header
  • scanner.QueryScanner: Scans url query values from a *url.Values
  • scanner.FormScanner: Scans form data from a *url.Values
  • scanner.CookieScanner: Scans cookies from a http.CookieJar
  • scanner.MultipartScanner: Scans multipart form data from a scanner.MultipartValues
  • scanner.ImageScanner: Scans multipart images from a scanner.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 scanner.Scanner

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

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

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

Or, alternatively, you can use a scanner.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 := scanner.NewPipeScanner(
  scanner.NewQueryScanner(/* url.Values */),
  scanner.NewHeaderScanner(/* http.Header */),
  scanner.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 Cookie struct {
	http.CookieJar
	// contains filtered or unexported fields
}

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

func NewCookie added in v0.0.2

func NewCookie(jar http.CookieJar, url *url.URL) *Cookie

func (Cookie) Get added in v0.0.2

func (v Cookie) Get(key string) any

func (*Cookie) Scan added in v0.0.2

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

Scans the cookie values onto v

type Directory added in v0.0.2

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

A scanner to scan os file's content to a struct

func NewDirectory added in v0.0.2

func NewDirectory(fsys fs.FS) (*Directory, error)

func (*Directory) Cast added in v0.0.2

func (s *Directory) Cast(from any, to reflect.Type) (any, error)

func (*Directory) Get added in v0.0.2

func (s *Directory) Get(key string) any

func (*Directory) Scan added in v0.0.2

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

type Form added in v0.0.2

type Form struct {
	*url.Values
}

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

func NewForm added in v0.0.2

func NewForm(v *url.Values) *Form

func (Form) Cast added in v0.0.2

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

func (Form) Get added in v0.0.2

func (v Form) Get(key string) any

func (*Form) Scan added in v0.0.2

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

Scans the form data onto v

type Header struct {
	*http.Header
}

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

func NewHeader added in v0.0.2

func NewHeader(h *http.Header) *Header

func (*Header) Get

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

func (*Header) Scan added in v0.0.2

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

Scans the headers onto v

type Image added in v0.0.2

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

func NewImage added in v0.0.2

func NewImage(v *MultipartValues) *Image

func (Image) Get added in v0.0.2

func (v Image) Get(key string) any

func (*Image) Scan added in v0.0.2

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

Scans the multipart form data and turns them into image.Image and sets v

type JSON added in v0.0.2

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

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

func NewJSON added in v0.0.2

func NewJSON(r io.Reader) *JSON

func NewJSONBytes added in v0.0.2

func NewJSONBytes(b []byte) *JSON

func (*JSON) Scan added in v0.0.2

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

Scans the json onto v

type Multipart added in v0.0.2

type Multipart 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 NewMultipart added in v0.0.2

func NewMultipart(v *MultipartValues) *Multipart

func (*Multipart) Scan added in v0.0.2

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

Scans the multipart form data onto v

type MultipartParser

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

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 Pipe added in v0.0.2

type Pipe []Scanner

func NewPipe added in v0.0.2

func NewPipe(scanners ...Scanner) *Pipe

func (*Pipe) Scan added in v0.0.2

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

Runs given scanners in sequence

type Query added in v0.0.2

type Query struct {
	*url.Values
}

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

func NewQuery added in v0.0.2

func NewQuery(v *url.Values) *Query

func (Query) Cast added in v0.0.2

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

func (Query) Get added in v0.0.2

func (v Query) Get(key string) any

func (*Query) Scan added in v0.0.2

func (s *Query) 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