-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
encode.go
42 lines (37 loc) · 1.21 KB
/
encode.go
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
// Package httpsfv implements serializing and parsing
// of Structured Field Values for HTTP as defined in RFC 8941.
//
// Structured Field Values are either lists, dictionaries or items. Dedicated types are provided for all of them.
// Dedicated types are also used for tokens, parameters and inner lists.
// Other values are stored in native types:
//
// int64, for integers
// float64, for decimals
// string, for strings
// byte[], for byte sequences
// bool, for booleans
//
// The specification is available at https://httpwg.org/specs/rfc8941.html.
package httpsfv
import (
"strings"
)
// marshaler is the interface implemented by types that can marshal themselves into valid SFV.
type marshaler interface {
marshalSFV(b *strings.Builder) error
}
// StructuredFieldValue represents a List, a Dictionary or an Item.
type StructuredFieldValue interface {
marshaler
}
// Marshal returns the HTTP Structured Value serialization of v
// as defined in https://httpwg.org/specs/rfc8941.html#text-serialize.
//
// v must be a List, a Dictionary, an Item or an InnerList.
func Marshal(v StructuredFieldValue) (string, error) {
var b strings.Builder
if err := v.marshalSFV(&b); err != nil {
return "", err
}
return b.String(), nil
}