-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistry.go
68 lines (58 loc) · 2.07 KB
/
registry.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
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
package negotiator
import (
"errors"
"mime"
"reflect"
)
var (
// ErrNoContentType is the error returned if an accept header cannot be matched
// in the current registry
ErrNoContentType = errors.New("No Acceptable Content Type")
)
// ContentTypeParams is a type alias for a map of string to strings,
// representing any parameters passed to the Content-Type header
type ContentTypeParams map[string]string
// Registry is a content type registry used for managing a mapping of media
// ranges to the interfaces that represent those resources
type Registry map[string]interface{}
// NewRegistry returns an empty Registry
func NewRegistry() *Registry {
return &Registry{}
}
// Register registers the default struct value for a content type in the
// registry. when requested, a copy of the default value will be provided as
// the result of a call to Negotiate
func (r Registry) Register(contentType string, defaultValue interface{}) {
if reflect.TypeOf(defaultValue).Kind() == reflect.Ptr {
r[contentType] = reflect.ValueOf(defaultValue).Elem().Interface()
return
}
r[contentType] = defaultValue
}
// Negotiate attempts to negotiate the proper interface for the provided accept
// header. Negotiate returns a copy of the default interface that best matches
// the provided accept header, if a match is found
func (r Registry) Negotiate(header string) (interface{}, *Accept, error) {
acceptHeader, err := ParseHeader(header)
if err != nil {
return nil, nil, err
}
for _, hdr := range acceptHeader {
if val, ok := r[string(hdr.MediaRange)]; ok {
return reflect.ValueOf(val).Interface(), hdr, nil
}
}
return nil, nil, ErrNoContentType
}
// ContentType parses the provided Content-Type header and attempts to find an
// interface which implements the specified content type
func (r Registry) ContentType(header string) (interface{}, ContentTypeParams, error) {
mediaType, params, err := mime.ParseMediaType(header)
if err != nil {
return nil, nil, err
}
if val, ok := r[mediaType]; ok {
return reflect.ValueOf(val).Interface(), params, nil
}
return nil, nil, ErrNoContentType
}