form

package
v61.21.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2019 License: MIT Imports: 7 Imported by: 4,471

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Strict = false

Strict enables strict mode wherein the package will panic on an AppendTo function if it finds that a tag string was malformed.

Functions

func AppendTo

func AppendTo(values *Values, i interface{})

AppendTo uses reflection to form encode into the given values collection based off the form tags that it defines.

func AppendToPrefixed

func AppendToPrefixed(values *Values, i interface{}, keyParts []string)

AppendToPrefixed is the same as AppendTo, but it allows a slice of key parts to be specified to prefix the form values.

I was hoping not to have to expose this function, but I ended up needing it for recipients. Recipients is going away, and when it does, we can probably remove it again.

func FormatKey

func FormatKey(parts []string) string

FormatKey takes a series of key parts that may be parameter keyParts, map keys, or array indices and unifies them into a single key suitable for Stripe's style of form encoding.

Types

type Appender

type Appender interface {
	// AppendTo is invoked by the form package on any types found to implement
	// Appender so that they have a chance to encode themselves. Note that
	// AppendTo is called in addition to normal encoding, so other form tags on
	// the struct are still fair game.
	AppendTo(values *Values, keyParts []string)
}

Appender is the interface implemented by types that can append themselves to a collection of form values.

This is usually something that shouldn't be used, but is needed in a few places where authors deviated from norms while implementing various parameters.

type Values

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

Values is a collection of values that can be submitted along with a request that specifically allows for duplicate keys and encodes its entries in the same order that they were added.

func (*Values) Add

func (f *Values) Add(key, val string)

Add adds a key/value tuple to the form.

func (*Values) Empty

func (f *Values) Empty() bool

Empty returns true if no parameters have been set.

func (*Values) Encode

func (f *Values) Encode() string

Encode encodes the values into “URL encoded” form ("bar=baz&foo=quux").

func (*Values) Get

func (f *Values) Get(key string) []string

Get retrieves the list of values for the given key. If no values exist for the key, nil will be returned.

Note that Get is O(n) and may be quite slow for a very large parameter list.

func (*Values) Set

func (f *Values) Set(key, val string)

Set sets the first instance of a parameter for the given key to the given value. If no parameters exist with the key, a new one is added.

Note that Set is O(n) and may be quite slow for a very large parameter list.

func (*Values) ToValues

func (f *Values) ToValues() url.Values

ToValues converts an instance of Values into an instance of url.Values. This can be useful in cases where it's useful to make an unordered comparison of two sets of request values.

Note that url.Values is incapable of representing certain Rack form types in a cohesive way. For example, an array of maps in Rack is encoded with a string like:

arr[][foo]=foo0&arr[][bar]=bar0&arr[][foo]=foo1&arr[][bar]=bar1

Because url.Values is a map, values will be handled in a way that's grouped by their key instead of in the order they were added. Therefore the above may by encoded to something like (maps are unordered so the actual result is somewhat non-deterministic):

arr[][foo]=foo0&arr[][foo]=foo1&arr[][bar]=bar0&arr[][bar]=bar1

And thus result in an incorrect request to Stripe.

Jump to

Keyboard shortcuts

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