-
Notifications
You must be signed in to change notification settings - Fork 2
/
location.go
78 lines (66 loc) · 1.47 KB
/
location.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
package openapi
import (
"github.com/chanced/jsonpointer"
"github.com/chanced/uri"
)
// TODO: relToRes needs to be a slice
func NewLocation(uri uri.URI) (Location, error) {
ptr, err := jsonpointer.Parse(uri.Fragment)
if err != nil {
return Location{}, err
}
loc := Location{
absolute: uri,
relative: ptr,
}
return loc, nil
}
type Location struct {
absolute uri.URI
relative jsonpointer.Pointer
}
func (l Location) String() string {
return l.absolute.String()
}
func (l Location) AbsoluteLocation() uri.URI {
return l.absolute
}
// RelativeLocation returns a jsonpointer.Pointer of the path from the
// containing resource file.
func (l Location) RelativeLocation() jsonpointer.Pointer {
return l.relative
}
func (l Location) AppendLocation(p string) Location {
l.relative = l.relative.AppendString(p)
l.absolute.Fragment = l.relative.String()
l.absolute.RawFragment = l.relative.String()
return l
}
func (l Location) withURI(uri *uri.URI) (Location, error) {
l.absolute = *uri
if len(l.absolute.Fragment) > 0 {
var err error
l.relative, err = jsonpointer.Parse(l.absolute.Fragment)
if err != nil {
return l, err
}
}
// we dont know what this is yet
l.relative = ""
return l, nil
}
func (l Location) location() Location {
return l
}
func (l Location) IsRelativeTo(uri *uri.URI) bool {
if uri == nil {
return false
}
a := l.absolute
a.Fragment = ""
a.RawFragment = ""
u := *uri
u.Fragment = ""
u.RawFragment = ""
return a.String() == u.String()
}