1 // Copyright 2013 sigu-399 ( https://github.com/sigu-399 )
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
16 // author-github https://github.com/sigu-399
17 // author-mail sigu.399@gmail.com
19 // repository-name jsonreference
20 // repository-desc An implementation of JSON Reference - Go language
22 // description Main and unique file.
33 "github.com/PuerkitoBio/purell"
34 "github.com/go-openapi/jsonpointer"
41 // New creates a new reference for the given string
42 func New(jsonReferenceString string) (Ref, error) {
45 err := r.parse(jsonReferenceString)
50 // MustCreateRef parses the ref string and panics when it's invalid.
51 // Use the New method for a version that returns an error
52 func MustCreateRef(ref string) Ref {
60 // Ref represents a json reference object
63 referencePointer jsonpointer.Pointer
72 // GetURL gets the URL for this reference
73 func (r *Ref) GetURL() *url.URL {
77 // GetPointer gets the json pointer for this reference
78 func (r *Ref) GetPointer() *jsonpointer.Pointer {
79 return &r.referencePointer
82 // String returns the best version of the url for this reference
83 func (r *Ref) String() string {
85 if r.referenceURL != nil {
86 return r.referenceURL.String()
89 if r.HasFragmentOnly {
90 return fragmentRune + r.referencePointer.String()
93 return r.referencePointer.String()
96 // IsRoot returns true if this reference is a root document
97 func (r *Ref) IsRoot() bool {
98 return r.referenceURL != nil &&
101 r.referenceURL.Fragment == ""
104 // IsCanonical returns true when this pointer starts with http(s):// or file://
105 func (r *Ref) IsCanonical() bool {
106 return (r.HasFileScheme && r.HasFullFilePath) || (!r.HasFileScheme && r.HasFullURL)
109 // "Constructor", parses the given string JSON reference
110 func (r *Ref) parse(jsonReferenceString string) error {
112 parsed, err := url.Parse(jsonReferenceString)
117 r.referenceURL, _ = url.Parse(purell.NormalizeURL(parsed, purell.FlagsSafe|purell.FlagRemoveDuplicateSlashes))
118 refURL := r.referenceURL
120 if refURL.Scheme != "" && refURL.Host != "" {
123 if refURL.Path != "" {
124 r.HasURLPathOnly = true
125 } else if refURL.RawQuery == "" && refURL.Fragment != "" {
126 r.HasFragmentOnly = true
130 r.HasFileScheme = refURL.Scheme == "file"
131 r.HasFullFilePath = strings.HasPrefix(refURL.Path, "/")
133 // invalid json-pointer error means url has no json-pointer fragment. simply ignore error
134 r.referencePointer, _ = jsonpointer.New(refURL.Fragment)
139 // Inherits creates a new reference from a parent and a child
140 // If the child cannot inherit from the parent, an error is returned
141 func (r *Ref) Inherits(child Ref) (*Ref, error) {
142 childURL := child.GetURL()
143 parentURL := r.GetURL()
145 return nil, errors.New("child url is nil")
147 if parentURL == nil {
151 ref, err := New(parentURL.ResolveReference(childURL).String())