header

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: May 3, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package header implements utility functions for encoding Go structs into standard HTTP Headers.

The primary utility provided is an API for encoding a (nearly) arbitrary Go struct into an http.Header, as well as the converse of filling in the fields of a (nearly) arbitrary Go struct from an http.Header. This API also includes a struct tag, to allow for customized encoding of struct fields as keys when encoding into an http.Header.

There are limitations on the exact nature of what can be encoded and decoded by this package, based primarily on the limitations of an http.Header just being a map[string][]string at its core.

The following types are fully supported:

int, []int, and all bit-specified types
float, []float, and all bit-specified types
string, []string
bool, []bool
struct

Arrays of structs are not supported, and issues may arise if multiple structs are encoded where the Field Names are duplicated. Nested structs are supported, but with the restriction on Field Name uniqueness.

This package works by converting the Field Name (or struct tag, if present) into the map key, and "stringifying" the Field Value to append to the map value array. When decoding, the reverse process is used, where the fields of the struct to decode into are used to find corresponding values in the map, and the map values are parsed back into the corresponding types.

Index

Constants

View Source
const EasyTLSStructTag = `easytls`

EasyTLSStructTag represents the Struct Tag key used by this package. Structs can customize the key used when encoding into an http.Header by using this struct tag analogous to how the json tags work.

Variables

This section is empty.

Functions

func DefaultDecode

func DefaultDecode(H http.Header, v interface{}) error

DefaultDecode will allow using the default decoder, decoding the header into the value pointed to by v.

func DefaultEncode

func DefaultEncode(v interface{}) (http.Header, error)

DefaultEncode will allow using a default encoding, returning an HTTP Header to be merged with the one to be sent.

func Merge

func Merge(Base *http.Header, Insert *http.Header)

Merge will merge two http.Headers together, adding "Insert" into "Base".

Types

type Decoder

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

Decoder will implement the necessary functionality for parsing an http.Header into a Go struct.

func NewDecoder

func NewDecoder(v interface{}) *Decoder

NewDecoder will create and initialize a decoder, ready to write an http.Header into a Go struct. This will decode into the value pointed to by v.

func (*Decoder) Decode

func (D *Decoder) Decode(H http.Header) (err error)

Decode will actually decode the Header H into the interface used to initialize the Decoder used.

func (*Decoder) Out

func (D *Decoder) Out() interface{}

Out will return a copy of the struct being filled in by this Decoder, exactly as it sees it at the time of calling.

type Encoder

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

Encoder will implement the necessary functionality for parsing a Go struct into a valid http.Header.

func NewEncoder

func NewEncoder(H *http.Header) *Encoder

NewEncoder will create and initialize an encoder, ready to write a Go struct into an HTTP Header.

func (*Encoder) Encode

func (E *Encoder) Encode(v interface{}) (err error)

Encode will actually encode the struct v into the http.Header. This process must flatten any nested structs into a single layer, to match the underlying structure of the HTTP Headers.

func (*Encoder) Header

func (E *Encoder) Header() http.Header

Header returns a copy of the underlying HTTP Header, as the Encoder currently sees it.

Jump to

Keyboard shortcuts

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