@@ -40,13 +40,23 @@ const (
40
40
41
41
var ErrChildURL = errors .New ("child url is nil" )
42
42
43
+ // Ref represents a json reference object
44
+ type Ref struct {
45
+ referenceURL * url.URL
46
+ referencePointer jsonpointer.Pointer
47
+
48
+ HasFullURL bool
49
+ HasURLPathOnly bool
50
+ HasFragmentOnly bool
51
+ HasFileScheme bool
52
+ HasFullFilePath bool
53
+ }
54
+
43
55
// New creates a new reference for the given string
44
56
func New (jsonReferenceString string ) (Ref , error ) {
45
-
46
57
var r Ref
47
58
err := r .parse (jsonReferenceString )
48
59
return r , err
49
-
50
60
}
51
61
52
62
// MustCreateRef parses the ref string and panics when it's invalid.
@@ -56,19 +66,8 @@ func MustCreateRef(ref string) Ref {
56
66
if err != nil {
57
67
panic (err )
58
68
}
59
- return r
60
- }
61
-
62
- // Ref represents a json reference object
63
- type Ref struct {
64
- referenceURL * url.URL
65
- referencePointer jsonpointer.Pointer
66
69
67
- HasFullURL bool
68
- HasURLPathOnly bool
69
- HasFragmentOnly bool
70
- HasFileScheme bool
71
- HasFullFilePath bool
70
+ return r
72
71
}
73
72
74
73
// GetURL gets the URL for this reference
@@ -83,7 +82,6 @@ func (r *Ref) GetPointer() *jsonpointer.Pointer {
83
82
84
83
// String returns the best version of the url for this reference
85
84
func (r * Ref ) String () string {
86
-
87
85
if r .referenceURL != nil {
88
86
return r .referenceURL .String ()
89
87
}
@@ -108,9 +106,27 @@ func (r *Ref) IsCanonical() bool {
108
106
return (r .HasFileScheme && r .HasFullFilePath ) || (! r .HasFileScheme && r .HasFullURL )
109
107
}
110
108
109
+ // Inherits creates a new reference from a parent and a child
110
+ // If the child cannot inherit from the parent, an error is returned
111
+ func (r * Ref ) Inherits (child Ref ) (* Ref , error ) {
112
+ childURL := child .GetURL ()
113
+ parentURL := r .GetURL ()
114
+ if childURL == nil {
115
+ return nil , ErrChildURL
116
+ }
117
+ if parentURL == nil {
118
+ return & child , nil
119
+ }
120
+
121
+ ref , err := New (parentURL .ResolveReference (childURL ).String ())
122
+ if err != nil {
123
+ return nil , err
124
+ }
125
+ return & ref , nil
126
+ }
127
+
111
128
// "Constructor", parses the given string JSON reference
112
129
func (r * Ref ) parse (jsonReferenceString string ) error {
113
-
114
130
parsed , err := url .Parse (jsonReferenceString )
115
131
if err != nil {
116
132
return err
@@ -139,22 +155,3 @@ func (r *Ref) parse(jsonReferenceString string) error {
139
155
140
156
return nil
141
157
}
142
-
143
- // Inherits creates a new reference from a parent and a child
144
- // If the child cannot inherit from the parent, an error is returned
145
- func (r * Ref ) Inherits (child Ref ) (* Ref , error ) {
146
- childURL := child .GetURL ()
147
- parentURL := r .GetURL ()
148
- if childURL == nil {
149
- return nil , ErrChildURL
150
- }
151
- if parentURL == nil {
152
- return & child , nil
153
- }
154
-
155
- ref , err := New (parentURL .ResolveReference (childURL ).String ())
156
- if err != nil {
157
- return nil , err
158
- }
159
- return & ref , nil
160
- }
0 commit comments