From 35b4e06fa67fce643c366267071ef74068038119 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Mon, 31 Aug 2020 21:14:38 +0300 Subject: [PATCH] add DecodeHeaders --- cache.go | 2 +- schema.go | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/cache.go b/cache.go index e361bfe..adcc326 100644 --- a/cache.go +++ b/cache.go @@ -19,7 +19,7 @@ func newCache() *cache { c := cache{ m: make(map[reflect.Type]*structInfo), regconv: make(map[reflect.Type]Converter), - tags: []string{"form", "url", "param", "schema"}, + tags: []string{"form", "url", "header", "param", "schema"}, } return &c } diff --git a/schema.go b/schema.go index 6ac0fa2..fcbe6fb 100644 --- a/schema.go +++ b/schema.go @@ -1,13 +1,15 @@ package schema var ( - defaultDecoder = NewDecoder() // form, url, param, schema. + defaultDecoder = NewDecoder() // form, url, header, param, schema. // Form Decoder. The default instance for DecodeForm function. Form = NewDecoder().SetAliasTag("form") // Query Decoder. The default instance for DecodeQuery function. Query = NewDecoder().SetAliasTag("url").IgnoreUnknownKeys(true) // allow unknown url queries + // Headers Decoder. The default instance for DecodeHeaders function. + Headers = NewDecoder().SetAliasTag("header").IgnoreUnknownKeys(true) // Params Decoder. The default instance for DecodeParams function. - Params = NewDecoder().SetAliasTag("param").IgnoreUnknownKeys(true) // and dynamic path parameters. + Params = NewDecoder().SetAliasTag("param").IgnoreUnknownKeys(true) ) // Decode maps "values" to "ptr". @@ -28,6 +30,12 @@ func DecodeQuery(values map[string][]string, ptr interface{}) error { return Query.Decode(ptr, values) } +// DecodeHeaders maps "values" to "ptr". +// With "header" tag for fields. +func DecodeHeaders(values map[string][]string, ptr interface{}) error { + return Headers.Decode(ptr, values) +} + // DecodeParams maps "values" to "ptr". // With "param" tag for fields. func DecodeParams(values map[string][]string, ptr interface{}) error {