Go implementation for RFC 8941 Structured Field Values for HTTP (SFV). It also supports Dates and Display Strings defined by draft-ietf-httpbis-sfbis-03.
h := make(http.Header)
// Decoding Items
item, err := sfv.DecodeItem(h.Values("Example-Hdr"))
switch val := item.Value.(type) {
case int64: // Integers
case float64: // Decimals
case string: // Strings
case sfv.Token: // Tokens
case bool: // Booleans
case time.Time: // Dates
case sfv.DisplayString: // Display Strings
}
// Decoding Lists
list, err := sfv.DecodeList(h.Values("Example-Hdr"))
// Decoding Dictionaries
dict, err := sfv.DecodeDictionary(h.Values("Example-Hdr"))
// Encoding Items
val, err := sfv.EncodeItem(item)
// Encoding Lists
val, err := sfv.EncodeList(list)
// Encoding Dictionaries
val, err := sfv.EncodeDictionary(dict)
SFV types are mapped to Go types as described in this section.
Note that only Lists(sfv.List
), Dictionaries(sfv.Dictionary
), and Items (sfv.Item
) can be in a top-level.
The sfv.Value
is defined as the following:
type Value interface{}
The actual type might be one of them:
Type of SFV | Example of SFV | Type in Go | Example in Go |
---|---|---|---|
Integer | 10 |
int64 |
int64(10) |
Decimal | 3.14 |
float64 |
float64(3.14) |
String | "hello" |
string |
"hello" |
Token | x |
sfv.Token |
sfv.Token("x") |
Byte Seq | :AQID: |
[]byte |
[]byte{1, 2, 3} |
Boolean | ?1 |
bool |
true |
Date | @1659578233 |
time.Time |
time.Unix(1659578233, 0) |
DisplayString | %"f%c3%bc%c3%bc" |
sfv.DisplayString |
sfv.DisplayString("füü") |
Inner List | (1 2) |
sfv.InnerList |
sfv.InnerList{} |
Parameters are ordered map of key-value pairs, however Go's map
types are unordered.
So sfv.Parameters
is defined by a slice of sfv.Parameter
that is a key-value pair.
type Parameter struct {
Key string
Value Value
}
type Parameters []Parameter
Lists are decoded to sfv.List
.
type List []Item
Inner Lists are decoded to sfv.InnerList
.
type InnerList []Item
Note that sfv.InnerList
can't contain sfv.InnerList
itself.
// Encoding this will fail.
innerList := sfv.InnerList{
{
Value: sfv.InnerList{},
},
}
Dictionaries are ordered maps of key-value pairs, however Go's map
types are unordered.
So sfv.Dictionary
is defined by a slice of sfv.DictMember
that is a key-value pair.
type DictMember struct {
Key string
Item Item
}
type Dictionary []DictMember