-
Notifications
You must be signed in to change notification settings - Fork 33
/
reference.go
140 lines (124 loc) · 3.11 KB
/
reference.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package schematic
import (
"fmt"
"net/url"
"reflect"
"regexp"
"strings"
)
const (
fragment = "#"
separator = "/"
)
var href = regexp.MustCompile(`({\([^\)]+)\)}`)
// Reference represents a JSON Reference.
type Reference string
// NewReference creates a new Reference based on a reference value.
func NewReference(ref string) *Reference {
r := new(Reference)
*r = Reference(ref)
return r
}
// Resolve resolves reference inside a Schema.
func (rf Reference) Resolve(r *Schema) *Schema {
if !strings.HasPrefix(string(rf), fragment) {
panic(fmt.Sprintf("non-fragment reference are not supported : %s", rf))
}
var node interface{}
node = r
for _, t := range strings.Split(string(rf), separator)[1:] {
t = decode(t)
v := reflect.Indirect(reflect.ValueOf(node))
switch v.Kind() {
case reflect.Struct:
var f reflect.Value
for i := 0; i < v.NumField(); i++ {
f = v.Field(i)
ft := v.Type().Field(i)
tag := ft.Tag.Get("json")
if tag == "-" {
continue
}
name := parseTag(tag)
if name == "" {
name = ft.Name
}
if name == t {
break
}
}
if !f.IsValid() {
panic(fmt.Sprintf("can't find '%s' field in %s", t, rf))
}
node = f.Interface()
case reflect.Map:
kv := v.MapIndex(reflect.ValueOf(t))
if !kv.IsValid() {
panic(fmt.Sprintf("can't find '%s' key in %s", t, rf))
}
node = kv.Interface()
default:
panic(fmt.Sprintf("can't follow pointer : %s", rf))
}
}
return node.(*Schema)
}
func encode(t string) (encoded string) {
encoded = strings.Replace(t, "/", "~1", -1)
return strings.Replace(encoded, "~", "~0", -1)
}
func decode(t string) (decoded string) {
decoded = strings.Replace(t, "~1", "/", -1)
return strings.Replace(decoded, "~0", "~", -1)
}
func parseTag(tag string) string {
if i := strings.Index(tag, ","); i != -1 {
return tag[:i]
}
return tag
}
// HRef represents a Link href.
type HRef struct {
href string
Order []string
Schemas map[string]*Schema
}
// NewHRef creates a new HRef struct based on a href value.
func NewHRef(href string) *HRef {
return &HRef{
href: href,
}
}
// Resolve resolves a href inside a Schema.
func (h *HRef) Resolve(r *Schema, rs ResolvedSet) {
h.Order = make([]string, 0)
h.Schemas = make(map[string]*Schema)
for _, v := range href.FindAllString(string(h.href), -1) {
u, err := url.QueryUnescape(v[2 : len(v)-2])
if err != nil {
panic(err)
}
parts := strings.Split(u, "/")
name := initialLow(fmt.Sprintf("%s-%s", parts[len(parts)-3], parts[len(parts)-1]))
h.Order = append(h.Order, name)
h.Schemas[name] = Reference(u).Resolve(r).Resolve(r, rs)
}
}
// UnmarshalJSON sets *h to a copy of data.
func (h *HRef) UnmarshalJSON(data []byte) error {
h.href = string(data[1 : len(data)-1])
return nil
}
// MarshalJSON returns *h as the JSON encoding of h.
func (h *HRef) MarshalJSON() ([]byte, error) {
return []byte(h.href), nil
}
// URL returns a usable URL for the href.
func (h *HRef) URL() (*url.URL, error) {
return url.Parse(string(h.href))
}
func (h *HRef) String() string {
return href.ReplaceAllStringFunc(string(h.href), func(v string) string {
return "%v"
})
}