rbbind

package module
v0.0.0-...-dbdc49b Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2021 License: MIT Imports: 9 Imported by: 0

README

Backlog

  • SHOULD add xml decoder

  • COULD add debug logging for binding, but that should be request scoped, which requires inclusion of other package

  • COULD add a way to decode/bind data from the session into a request/page struct. But that might allow accidental writing of page fields from other request inputs if the fields have the same name. Instead, maybe add a rbbind.Target type that configures what (form, query, sess) will be decoded with what. Or maybe look at the content type

Binding targets

  • Body: Based on content-type
  • Query: Always form
  • Session: implemented by rbsess
  • Headers

Bind v2

Features:

  • Zero-alloc if the encoder supports zero allocations (such as form query decoding). This prevents us from using the option pattern. Maybe flags, or pair providing
  • Should support decoders that decode the: body, query, session or headers
  • Should support taking into account content-type

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FormDecoder = fx.Annotated{
	Target: NewForm,
	Group:  "rb.decoder",
}

FormDecoder provides an form decoder annotated with the correct group

View Source
var JSONDecoder = fx.Annotated{
	Target: NewJSON,
	Group:  "rb.decoder",
}

JSONDecoder annotates the json decoder constructor as the correct group

Functions

This section is empty.

Types

type B

type B interface {
	Bind(r *http.Request, vs ...interface{}) error
}

B is the bind interface

func New

func New(logs *zap.Logger, cfg Conf, p Params) (B, error)

New inits the binder

type Binder

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

Binder provides the ability to bind requests to values

func (*Binder) Bind

func (b *Binder) Bind(r *http.Request, vs ...interface{}) (err error)

Bind will bind the request to all the provided values

type Conf

type Conf struct {
	DecoderOrder []string `env:"RB_BIND_DECODER_ORDER" envSeparator:","`
}

Conf configures the binder

func ParseConf

func ParseConf() (cfg Conf, err error)

ParseConf parses the env

type Decoder

type Decoder interface {
	Name() string
	Decode(r *http.Request, v interface{}, mt string, mtParams map[string]string) error
}

Decoder can be implemented to decode http requests

func NewForm

func NewForm() Decoder

NewForm inits the form decoder for binding

func NewJSON

func NewJSON() Decoder

NewJSON creates the json request decoder

type Form

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

Form decoder supports decoding of url encoded values

func (*Form) Decode

func (d *Form) Decode(r *http.Request, v interface{}, _ string, _ map[string]string) (err error)

Decode performs the actual decoder

func (*Form) Name

func (d *Form) Name() string

Name returns the name of the decoder

type JSON

type JSON struct{}

JSON decoder

func (*JSON) Decode

func (d *JSON) Decode(r *http.Request, v interface{}, mt string, _ map[string]string) (err error)

Decode performs the request decoding

func (*JSON) Name

func (d *JSON) Name() string

Name returns the name of the json decoder

type Params

type Params struct {
	fx.In
	Decoders []Decoder `group:"rb.decoder"`
}

Params configures dependencies binding dependencies

Jump to

Keyboard shortcuts

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