1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
// Package httpheader implements encoding of structs into http.Header fields.
//
// As a simple example:
//
// type Options struct {
// ContentType string `header:"Content-Type"`
// Length int
// }
//
// opt := Options{"application/json", 2}
// h, _ := httpheader.Header(opt)
// fmt.Printf("%#v", h)
// // will output:
// // http.Header{"Content-Type":[]string{"application/json"},"Length":[]string{"2"}}
//
// The exact mapping between Go values and http.Header is described in the
// documentation for the Header() function.
package httpheader
import (
"fmt"
"net/http"
"reflect"
"strconv"
"strings"
"time"
)
const tagName = "header"
// Version ...
const Version = "0.3.1"
var timeType = reflect.TypeOf(time.Time{})
var headerType = reflect.TypeOf(http.Header{})
var encoderType = reflect.TypeOf(new(Encoder)).Elem()
// Encoder is an interface implemented by any type that wishes to encode
// itself into Header fields in a non-standard way.
type Encoder interface {
EncodeHeader(key string, v *http.Header) error
}
// Header returns the http.Header encoding of v.
//
// Header expects to be passed a struct, and traverses it recursively using the
// following encoding rules.
//
// Each exported struct field is encoded as a Header field unless
//
// - the field's tag is "-", or
// - the field is empty and its tag specifies the "omitempty" option
//
// The empty values are false, 0, any nil pointer or interface value, any array
// slice, map, or string of length zero, and any time.Time that returns true
// for IsZero().
//
// The Header field name defaults to the struct field name but can be
// specified in the struct field's tag value. The "header" key in the struct
// field's tag value is the key name, followed by an optional comma and
// options. For example:
//
// // Field is ignored by this package.
// Field int `header:"-"`
//
// // Field appears as Header field "X-Name".
// Field int `header:"X-Name"`
//
// // Field appears as Header field "X-Name" and the field is omitted if
// // its value is empty
// Field int `header:"X-Name,omitempty"`
//
// // Field appears as Header field "Field" (the default), but the field
// // is skipped if empty. Note the leading comma.
// Field int `header:",omitempty"`
//
// For encoding individual field values, the following type-dependent rules
// apply:
//
// Boolean values default to encoding as the strings "true" or "false".
// Including the "int" option signals that the field should be encoded as the
// strings "1" or "0".
//
// time.Time values default to encoding as RFC1123("Mon, 02 Jan 2006 15:04:05 GMT")
// timestamps. Including the "unix" option signals that the field should be
// encoded as a Unix time (see time.Unix())
//
// Slice and Array values default to encoding as multiple Header values of the
// same name. example:
// X-Name: []string{"Tom", "Jim"}, etc.
//
// http.Header values will be used to extend the Header fields.
//
// Anonymous struct fields are usually encoded as if their inner exported
// fields were fields in the outer struct, subject to the standard Go
// visibility rules. An anonymous struct field with a name given in its Header
// tag is treated as having that name, rather than being anonymous.
//
// Non-nil pointer values are encoded as the value pointed to.
//
// All other values are encoded using their default string representation.
//
// Multiple fields that encode to the same Header filed name will be included
// as multiple Header values of the same name.
func Header(v interface{}) (http.Header, error) {
h := make(http.Header)
val := reflect.ValueOf(v)
for val.Kind() == reflect.Ptr {
if val.IsNil() {
return h, nil
}
val = val.Elem()
}
if v == nil {
return h, nil
}
if val.Kind() != reflect.Struct {
return nil, fmt.Errorf("httpheader: Header() expects struct input. Got %v", val.Kind())
}
err := reflectValue(h, val)
return h, err
}
// reflectValue populates the header fields from the struct fields in val.
// Embedded structs are followed recursively (using the rules defined in the
// Values function documentation) breadth-first.
func reflectValue(header http.Header, val reflect.Value) error {
var embedded []reflect.Value
typ := val.Type()
for i := 0; i < typ.NumField(); i++ {
sf := typ.Field(i)
if sf.PkgPath != "" && !sf.Anonymous { // unexported
continue
}
sv := val.Field(i)
tag := sf.Tag.Get(tagName)
if tag == "-" {
continue
}
name, opts := parseTag(tag)
if name == "" {
if sf.Anonymous && sv.Kind() == reflect.Struct {
// save embedded struct for later processing
embedded = append(embedded, sv)
continue
}
name = sf.Name
}
if opts.Contains("omitempty") && isEmptyValue(sv) {
continue
}
if sv.Type().Implements(encoderType) {
if !reflect.Indirect(sv).IsValid() {
sv = reflect.New(sv.Type().Elem())
}
m := sv.Interface().(Encoder)
if err := m.EncodeHeader(name, &header); err != nil {
return err
}
continue
}
if sv.Kind() == reflect.Slice || sv.Kind() == reflect.Array {
for i := 0; i < sv.Len(); i++ {
k := name
header.Add(k, valueString(sv.Index(i), opts))
}
continue
}
for sv.Kind() == reflect.Ptr {
if sv.IsNil() {
break
}
sv = sv.Elem()
}
if sv.Type() == timeType {
header.Add(name, valueString(sv, opts))
continue
}
if sv.Type() == headerType {
h := sv.Interface().(http.Header)
for k, vs := range h {
for _, v := range vs {
header.Add(k, v)
}
}
continue
}
if sv.Kind() == reflect.Struct {
if err := reflectValue(header, sv); err != nil {
return err
}
continue
}
header.Add(name, valueString(sv, opts))
}
for _, f := range embedded {
if err := reflectValue(header, f); err != nil {
return err
}
}
return nil
}
// valueString returns the string representation of a value.
func valueString(v reflect.Value, opts tagOptions) string {
for v.Kind() == reflect.Ptr {
if v.IsNil() {
return ""
}
v = v.Elem()
}
if v.Kind() == reflect.Bool && opts.Contains("int") {
if v.Bool() {
return "1"
}
return "0"
}
if v.Type() == timeType {
t := v.Interface().(time.Time)
if opts.Contains("unix") {
return strconv.FormatInt(t.Unix(), 10)
}
return t.Format(http.TimeFormat)
}
return fmt.Sprint(v.Interface())
}
// isEmptyValue checks if a value should be considered empty for the purposes
// of omitting fields with the "omitempty" option.
func isEmptyValue(v reflect.Value) bool {
switch v.Kind() {
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
return v.Len() == 0
case reflect.Bool:
return !v.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return v.Uint() == 0
case reflect.Float32, reflect.Float64:
return v.Float() == 0
case reflect.Interface, reflect.Ptr:
return v.IsNil()
}
if v.Type() == timeType {
return v.Interface().(time.Time).IsZero()
}
return false
}
// tagOptions is the string following a comma in a struct field's "header" tag, or
// the empty string. It does not include the leading comma.
type tagOptions []string
// parseTag splits a struct field's header tag into its name and comma-separated
// options.
func parseTag(tag string) (string, tagOptions) {
s := strings.Split(tag, ",")
return s[0], s[1:]
}
// Contains checks whether the tagOptions contains the specified option.
func (o tagOptions) Contains(option string) bool {
for _, s := range o {
if s == option {
return true
}
}
return false
}
// Encode is an alias of Header function
func Encode(v interface{}) (http.Header, error) {
return Header(v)
}
|