value

package
v0.0.0-...-ed9fe1d Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2018 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Overview

Package value provides lazy.Value for web context.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewQueryIntInList

func NewQueryIntInList(key string, values []int, or int) lazy.Value

NewQueryIntInList is like NewQueryIntOr but it also limits the possible values in the `list“

func NewQueryIntInRange

func NewQueryIntInRange(key string, min int, max int, or int) lazy.Value

NewQueryIntInRange is like NewQueryIntOr but it also limits the min and max for the value

func NewQueryIntOr

func NewQueryIntOr(key string, or int) lazy.Value

NewQueryIntOr returns a new *lazy.Value to get the query value by `key` or `or` value if the value is empty or invalid.

Example
val := NewQueryIntOr("foo", 10)
r := web.NewRouter(nil)
r.Get("/", web.HandlerFunc(func(req *web.Request, next web.NextHandler) *response.Response {
	v, _ := val.Eval(req.Context())
	return response.NewText(v)
}))
recorder := httptest.NewRecorder(r)
fmt.Println(
	recorder.TestGet("/?foo=5").Body,
	recorder.TestGet("/?foo=bar").Body,
	recorder.TestGet("/").Body,
)
Output:

5 10 10

func NewQueryStringInList

func NewQueryStringInList(key string, values []string, or string) lazy.Value

NewQueryStringInList is like NewQueryStringOr but it also limits the possible values in the `list“

Example
val := NewQueryStringInList("foo", []string{"bar"}, "default")
r := web.NewRouter(nil)
r.Get("/", web.HandlerFunc(func(req *web.Request, next web.NextHandler) *response.Response {
	v, _ := val.Eval(req.Context())
	return response.NewText(v)
}))
recorder := httptest.NewRecorder(r)
fmt.Println(
	recorder.TestGet("/?foo=aaa").Body,
	recorder.TestGet("/?foo=bar").Body,
)
Output:

default bar

func NewQueryStringOr

func NewQueryStringOr(key string, or string) lazy.Value

NewQueryStringOr returns a new *lazy.Value to get the query value by `key` or `or` value if the value is empty.

Example
val := NewQueryStringOr("foo", "bar")
r := web.NewRouter(nil)
r.Get("/", web.HandlerFunc(func(req *web.Request, next web.NextHandler) *response.Response {
	v, _ := val.Eval(req.Context())
	return response.NewText(v)
}))
recorder := httptest.NewRecorder(r)
fmt.Println(
	recorder.TestGet("/?foo=aaa").Body,
	recorder.TestGet("/").Body,
)
Output:

aaa bar

func NewRequestValue

func NewRequestValue(f func(*web.Request) (interface{}, error)) lazy.Value

NewRequestValue returns a lazy.Value in a request context

Example
val := NewRequestValue(func(req *web.Request) (interface{}, error) {
	return req.URL.Query().Encode(), nil
})
r := web.NewRouter(nil)
r.Get("/", web.HandlerFunc(func(req *web.Request, next web.NextHandler) *response.Response {
	v, _ := val.Eval(req.Context())
	return response.NewText(v)
}))
recorder := httptest.NewRecorder(r)
fmt.Println(recorder.TestGet("/?foo=bar").Body)
Output:

foo=bar

Types

This section is empty.

Jump to

Keyboard shortcuts

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