Documentation ¶
Overview ¶
Package sfv provides a parser and a serializer for Structured Field Values (SFV).
Index ¶
- Constants
- func EncodeDictionary(dict Dictionary) (string, error)
- func EncodeItem(item Item) (string, error)
- func EncodeList(list List) (string, error)
- func IsValidString(s string) bool
- type DictMember
- type Dictionary
- type DisplayString
- type InnerList
- type Item
- type List
- type Parameter
- type Parameters
- type Token
- type Value
Examples ¶
Constants ¶
const ( // The range of Integers MaxInteger = 999_999_999_999_999 MinInteger = -999_999_999_999_999 // The range of Decimals MaxDecimal = 0x1.d1a94a1fffffbp+39 // = 999999999999.9993896484375 MinDecimal = -0x1.d1a94a1fffffbp+39 // = -999999999999.9993896484375 )
Variables ¶
This section is empty.
Functions ¶
func EncodeDictionary ¶
func EncodeDictionary(dict Dictionary) (string, error)
EncodeDictionary encodes the given dictionary to Structured Field Values.
Example ¶
dict := sfv.Dictionary{ { Key: "foo", Item: sfv.Item{ Value: int64(1), }, }, { Key: "bar", Item: sfv.Item{ Value: int64(2), }, }, } val, err := sfv.EncodeDictionary(dict) if err != nil { panic(err) } fmt.Println(val)
Output: foo=1, bar=2
func EncodeItem ¶
EncodeItem encodes the given item to Structured Field Values.
Example ¶
item := sfv.Item{ Value: int64(2), Parameters: sfv.Parameters{ { Key: "foourl", Value: "https://foo.example.com/", }, }, } val, err := sfv.EncodeItem(item) if err != nil { panic(err) } fmt.Println(val)
Output: 2;foourl="https://foo.example.com/"
func EncodeList ¶
EncodeList encodes the given list to Structured Field Values.
Example ¶
list := sfv.List{ { Value: sfv.Token("foo"), }, { Value: sfv.Token("bar"), }, } val, err := sfv.EncodeList(list) if err != nil { panic(err) } fmt.Println(val)
Output: foo, bar
func IsValidString ¶
IsValidString returns whether the s has valid form.
Types ¶
type DictMember ¶
DictMember is a key-value pair of Dictionary.
type Dictionary ¶
type Dictionary []DictMember
Dictionary is an ordered map of key-value pairs defined in RFC 8941 Section 3.2. Dictionaries.
func DecodeDictionary ¶
func DecodeDictionary(fields []string) (Dictionary, error)
DecodeDictionary decodes fields as Structured Field Values, and returns the result as a Dictionary.
Example ¶
h := make(http.Header) h.Add("Example-Hdr", "foo=1") h.Add("Example-Hdr", "bar=2") dict, err := sfv.DecodeDictionary(h.Values("Example-Hdr")) if err != nil { panic(err) } for _, member := range dict { fmt.Println(member.Key, member.Item.Value) }
Output: foo 1 bar 2
func (Dictionary) Get ¶
func (dict Dictionary) Get(key string) Item
Get returns the last item associated with the given key. If there are no items associated with the key, Get returns the zero value of Item.
Example ¶
dict := sfv.Dictionary{ { Key: "foo", Item: sfv.Item{ Value: int64(1), }, }, { Key: "bar", Item: sfv.Item{ Value: int64(2), }, }, } fmt.Println(dict.Get("foo").Value) fmt.Println(dict.Get("bar").Value) fmt.Println(dict.Get("baz").Value)
Output: 1 2 <nil>
func (Dictionary) Len ¶
func (dict Dictionary) Len() int
Len returns the number of items in the dict.
Example ¶
dict := sfv.Dictionary{ { Key: "foo", Item: sfv.Item{ Value: int64(1), }, }, { Key: "bar", Item: sfv.Item{ Value: int64(2), }, }, } fmt.Println(dict.Len())
Output: 2
type InnerList ¶
type InnerList []Item
InnerList is an array defined in RFC 8941 Section 3.1.1. Inner Lists.
type Item ¶
type Item struct { Value Value Parameters Parameters }
Item is an item defined RFC 8941 Section 3.3. Items.
func DecodeItem ¶
DecodeList decodes fields as Structured Field Values, and returns the result as an Item.
Example ¶
item, err := sfv.DecodeItem([]string{`2; foourl="https://foo.example.com/"`}) if err != nil { panic(err) } fmt.Println(item.Value) fmt.Println(item.Parameters.Get("foourl"))
Output: 2 https://foo.example.com/
type List ¶
type List []Item
List is an array defined in RFC 8941 Section 3.1. Lists.
func DecodeList ¶
DecodeList decodes fields as Structured Field Values, and returns the result as a List.
Example ¶
h := make(http.Header) h.Add("Example-Hdr", "foo") h.Add("Example-Hdr", "bar") list, err := sfv.DecodeList(h.Values("Example-Hdr")) if err != nil { panic(err) } for _, item := range list { fmt.Println(item.Value) }
Output: foo bar
type Parameter ¶
type Parameter struct { // Key must match the following regular expression: // // [a-z*][a-z0-9_.*-] Key string // Value is a bare item. Value Value }
Parameter is a key-value pair of Parameters.
type Parameters ¶
type Parameters []Parameter
Parameters are an ordered map of key-value pairs defined in RFC 8941 Section 3.1.2. Parameters.
func (Parameters) Get ¶
func (param Parameters) Get(key string) Value
Get returns the last value associated with the given key. If there are no values associated with the key, Get returns Value(nil).
Example ¶
params := sfv.Parameters{ { Key: "foo", Value: "bar", }, { Key: "baz", Value: "qux", }, } fmt.Println(params.Get("foo")) fmt.Println(params.Get("baz")) fmt.Println(params.Get("quux"))
Output: bar qux <nil>
func (Parameters) Len ¶
func (param Parameters) Len() int
Len returns the number of items in the param.
Example ¶
params := sfv.Parameters{ { Key: "foo", Value: "bar", }, { Key: "baz", Value: "qux", }, } fmt.Println(params.Len())
Output: 2
type Token ¶
type Token string
Token is a token defined in RFC 8941 Section 3.3.4. Tokens. The token must match the following regular expression:
[a-zA-Z*][a-zA-Z0-9:/!#$%&'*+_.^_`|~-]*
type Value ¶
type Value interface{}
Value is a bare item. It might be Integers, Decimals, Strings, Tokens, Byte Sequences, Booleans or Inner Lists. It's type is one of these:
int64 for Integers float64 for Decimals string for Strings Token for Tokens []byte for Byte Sequences bool for Booleans time.Time for Date DisplayString for Display Strings InnerList for Inner Lists