Documentation ¶
Index ¶
- Constants
- func Set(target, source interface{}) error
- func SetDefault(target interface{}) error
- type Array
- type Context
- type Converter
- type CookieProvider
- type Decoder
- type DefaultProvider
- type Field
- type HeaderProvider
- type Map
- type PathProvider
- type QueryProvider
- type Struct
- type Tag
- type ValueConverter
- type ValueProvider
Examples ¶
Constants ¶
const ( // OptionSimple is the simple opt OptionSimple = "simple" // OptionForm is the form opt OptionForm = "form" // OptionLabel is the simple opt OptionLabel = "label" // OptionMatrix is the simple opt OptionMatrix = "matrix" // OptionExplode is the simple opt OptionExplode = "explode" // OptionDeepObject is the simple opt OptionDeepObject = "deep-object" // OptionSpaceDelimited is the space-delimited opt OptionSpaceDelimited = "space-delimited" // OptionPipeDelimited is the pipe-delimited opt OptionPipeDelimited = "pipe-delimited" )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Array ¶
Array works with slices and arrays
func MakeArrayOf ¶
MakeArrayOf returns an array / slice
type CookieProvider ¶
CookieProvider represents a parameter provider that fetches values from incoming request's cookies
func (*CookieProvider) Value ¶
func (p *CookieProvider) Value(ctx *Context) (interface{}, error)
Value returns a primitive value
type Decoder ¶
type Decoder struct { TagName string Provider ValueProvider Converter ValueConverter }
Decoder decodes the values from given source
Example (Cookie) ¶
package main import ( "fmt" "net/http" "github.com/phogolabs/inflate" ) func main() { type Session struct { Token string `cookie:"token"` } cookies := []*http.Cookie{ {Name: "token", Value: "123456"}, } session := &Session{} if err := inflate.NewCookieDecoder(cookies).Decode(session); err != nil { panic(err) } fmt.Printf("%+v", session) }
Output: &{Token:123456}
Example (Default) ¶
package main import ( "fmt" "github.com/phogolabs/inflate" ) func main() { type Address struct { City string `json:"city"` Country string `json:"country"` } type Profile struct { Name string `default:"John"` Address Address `default:"{\"city\":\"London\",\"country\":\"UK\"}"` } profile := &Profile{} if err := inflate.SetDefault(profile); err != nil { panic(err) } fmt.Printf("%+v", profile) }
Output: &{Name:John Address:{City:London Country:UK}}
Example (Header) ¶
package main import ( "fmt" "net/http" "github.com/phogolabs/inflate" ) func main() { type Tag struct { RequestID string `header:"X-Request-ID"` } header := http.Header{} header.Set("X-Request-ID", "123456") tag := &Tag{} if err := inflate.NewHeaderDecoder(header).Decode(tag); err != nil { panic(err) } fmt.Printf("%+v", tag) }
Output: &{RequestID:123456}
Example (Path) ¶
package main import ( "fmt" "github.com/go-chi/chi" "github.com/phogolabs/inflate" ) func main() { type Member struct { ID string `path:"id"` } param := &chi.RouteParams{} param.Keys = append(param.Keys, "id") param.Values = append(param.Values, "123456") member := &Member{} if err := inflate.NewPathDecoder(param).Decode(member); err != nil { panic(err) } fmt.Printf("%+v", member) }
Output: &{ID:123456}
Example (Query) ¶
package main import ( "fmt" "net/url" "github.com/phogolabs/inflate" ) func main() { type User struct { ID string `query:"id"` Name string `query:"name"` } query, err := url.ParseQuery("id=1&name=Jack") if err != nil { panic(err) } user := &User{} if err := inflate.NewQueryDecoder(query).Decode(user); err != nil { panic(err) } fmt.Printf("%+v", user) }
Output: &{ID:1 Name:Jack}
Example (Set) ¶
package main import ( "fmt" "github.com/phogolabs/inflate" ) func main() { type Order struct { ID string `field:"order_id"` } type OrderItem struct { OrderID string `field:"order_id"` } source := &Order{ID: "0000123"} target := &OrderItem{} if err := inflate.Set(target, source); err != nil { panic(err) } fmt.Printf("%+v", target) }
Output: &{OrderID:0000123}
func NewCookieDecoder ¶
NewCookieDecoder creates a cookie decoder
func NewFormDecoder ¶
NewFormDecoder creates a path decoder
func NewHeaderDecoder ¶
NewHeaderDecoder creates a header decoder
func NewPathDecoder ¶
func NewPathDecoder(r *chi.RouteParams) *Decoder
NewPathDecoder creates a path decoder
func NewQueryDecoder ¶
NewQueryDecoder creates a path decoder
type DefaultProvider ¶
type DefaultProvider struct{}
DefaultProvider returns the default for given field
func (*DefaultProvider) Value ¶
func (p *DefaultProvider) Value(ctx *Context) (interface{}, error)
Value returns the default value if specified
type HeaderProvider ¶
HeaderProvider represents a parameter provider that fetches values from incoming request's header
func (*HeaderProvider) Value ¶
func (p *HeaderProvider) Value(ctx *Context) (interface{}, error)
Value returns a primitive value
type PathProvider ¶
type PathProvider struct {
Param *chi.RouteParams
}
PathProvider represents a parameter provider that fetches values from incoming request's header
func (*PathProvider) Value ¶
func (p *PathProvider) Value(ctx *Context) (interface{}, error)
Value returns a primitive value
type QueryProvider ¶
QueryProvider represents a parameter provider that fetches values from incoming request's cookies
func (*QueryProvider) Value ¶
func (p *QueryProvider) Value(ctx *Context) (interface{}, error)
Value returns a primitive value
type ValueConverter ¶
type ValueConverter interface {
Convert(source, target interface{}) error
}
ValueConverter converts source to target
type ValueProvider ¶
ValueProvider provides a value