diff --git a/.golangci.yml b/.golangci.yml index 5006306..015e33a 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -22,7 +22,9 @@ linters: - nestif - nlreturn - nonamedreturns + - noinlineerr - paralleltest + - recvcheck - testpackage - thelper - tparallel @@ -31,6 +33,7 @@ linters: - whitespace - wrapcheck - wsl + - wsl_v5 settings: dupl: threshold: 200 diff --git a/reference.go b/reference.go index fdfd93f..6a1fed5 100644 --- a/reference.go +++ b/reference.go @@ -40,13 +40,23 @@ const ( var ErrChildURL = errors.New("child url is nil") +// Ref represents a json reference object +type Ref struct { + referenceURL *url.URL + referencePointer jsonpointer.Pointer + + HasFullURL bool + HasURLPathOnly bool + HasFragmentOnly bool + HasFileScheme bool + HasFullFilePath bool +} + // New creates a new reference for the given string func New(jsonReferenceString string) (Ref, error) { - var r Ref err := r.parse(jsonReferenceString) return r, err - } // MustCreateRef parses the ref string and panics when it's invalid. @@ -56,19 +66,8 @@ func MustCreateRef(ref string) Ref { if err != nil { panic(err) } - return r -} - -// Ref represents a json reference object -type Ref struct { - referenceURL *url.URL - referencePointer jsonpointer.Pointer - HasFullURL bool - HasURLPathOnly bool - HasFragmentOnly bool - HasFileScheme bool - HasFullFilePath bool + return r } // GetURL gets the URL for this reference @@ -83,7 +82,6 @@ func (r *Ref) GetPointer() *jsonpointer.Pointer { // String returns the best version of the url for this reference func (r *Ref) String() string { - if r.referenceURL != nil { return r.referenceURL.String() } @@ -108,9 +106,27 @@ func (r *Ref) IsCanonical() bool { return (r.HasFileScheme && r.HasFullFilePath) || (!r.HasFileScheme && r.HasFullURL) } +// Inherits creates a new reference from a parent and a child +// If the child cannot inherit from the parent, an error is returned +func (r *Ref) Inherits(child Ref) (*Ref, error) { + childURL := child.GetURL() + parentURL := r.GetURL() + if childURL == nil { + return nil, ErrChildURL + } + if parentURL == nil { + return &child, nil + } + + ref, err := New(parentURL.ResolveReference(childURL).String()) + if err != nil { + return nil, err + } + return &ref, nil +} + // "Constructor", parses the given string JSON reference func (r *Ref) parse(jsonReferenceString string) error { - parsed, err := url.Parse(jsonReferenceString) if err != nil { return err @@ -139,22 +155,3 @@ func (r *Ref) parse(jsonReferenceString string) error { return nil } - -// Inherits creates a new reference from a parent and a child -// If the child cannot inherit from the parent, an error is returned -func (r *Ref) Inherits(child Ref) (*Ref, error) { - childURL := child.GetURL() - parentURL := r.GetURL() - if childURL == nil { - return nil, ErrChildURL - } - if parentURL == nil { - return &child, nil - } - - ref, err := New(parentURL.ResolveReference(childURL).String()) - if err != nil { - return nil, err - } - return &ref, nil -}