-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
route-descriptor.go
48 lines (41 loc) · 1.23 KB
/
route-descriptor.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
package navaros
import (
"encoding/json"
)
// RouteDescriptor is a struct that is generated by the router when the public
// variants of the handler binding methods (named after their respective
// http method) are called. The router has a RouteDescriptors method which
// returns these objects, and can be used to build a api map, or pre-filter
// requests before they are passed to the router. This is most useful for
// libraries that wish to extend the functionality of Navaros.
type RouteDescriptor struct {
Method HTTPMethod
Pattern *Pattern
}
// MarshalJSON returns the JSON representation of the route descriptor.
func (r *RouteDescriptor) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Method HTTPMethod
Pattern string
}{
Method: r.Method,
Pattern: r.Pattern.String(),
})
}
// UnmarshalJSON parses the JSON representation of the route descriptor.
func (r *RouteDescriptor) UnmarshalJSON(data []byte) error {
fromJSONStruct := struct {
Method HTTPMethod
Pattern string
}{}
if err := json.Unmarshal(data, &fromJSONStruct); err != nil {
return err
}
pattern, err := NewPattern(fromJSONStruct.Pattern)
if err != nil {
return err
}
r.Method = fromJSONStruct.Method
r.Pattern = pattern
return nil
}