| 1 |
// Copyright 2015 go-swagger maintainers |
| 2 |
// |
| 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 |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 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. |
| 14 |
|
| 15 |
package spec |
| 16 |
|
| 17 |
import ( |
| 18 |
"encoding/json" |
| 19 |
"github.com/go-openapi/jsonreference" |
| 20 |
|
| 21 |
"k8s.io/kube-openapi/pkg/internal" |
| 22 |
) |
| 23 |
|
| 24 |
// Refable is a struct for things that accept a $ref property |
| 25 |
type Refable struct { |
| 26 |
Ref Ref |
| 27 |
} |
| 28 |
|
| 29 |
// MarshalJSON marshals the ref to json |
| 30 |
func (r Refable) MarshalJSON() ([]byte, error) { |
| 31 |
return r.Ref.MarshalJSON() |
| 32 |
} |
| 33 |
|
| 34 |
// UnmarshalJSON unmarshalss the ref from json |
| 35 |
func (r *Refable) UnmarshalJSON(d []byte) error { |
| 36 |
return json.Unmarshal(d, &r.Ref) |
| 37 |
} |
| 38 |
|
| 39 |
// Ref represents a json reference that is potentially resolved |
| 40 |
type Ref struct { |
| 41 |
jsonreference.Ref |
| 42 |
} |
| 43 |
|
| 44 |
// RemoteURI gets the remote uri part of the ref |
| 45 |
func (r *Ref) RemoteURI() string { |
| 46 |
if r.String() == "" { |
| 47 |
return r.String() |
| 48 |
} |
| 49 |
|
| 50 |
u := *r.GetURL() |
| 51 |
u.Fragment = "" |
| 52 |
return u.String() |
| 53 |
} |
| 54 |
|
| 55 |
// Inherits creates a new reference from a parent and a child |
| 56 |
// If the child cannot inherit from the parent, an error is returned |
| 57 |
func (r *Ref) Inherits(child Ref) (*Ref, error) { |
| 58 |
ref, err := r.Ref.Inherits(child.Ref) |
| 59 |
if err != nil { |
| 60 |
return nil, err |
| 61 |
} |
| 62 |
return &Ref{Ref: *ref}, nil |
| 63 |
} |
| 64 |
|
| 65 |
// NewRef creates a new instance of a ref object |
| 66 |
// returns an error when the reference uri is an invalid uri |
| 67 |
func NewRef(refURI string) (Ref, error) { |
| 68 |
ref, err := jsonreference.New(refURI) |
| 69 |
if err != nil { |
| 70 |
return Ref{}, err |
| 71 |
} |
| 72 |
return Ref{Ref: ref}, nil |
| 73 |
} |
| 74 |
|
| 75 |
// MustCreateRef creates a ref object but panics when refURI is invalid. |
| 76 |
// Use the NewRef method for a version that returns an error. |
| 77 |
func MustCreateRef(refURI string) Ref { |
| 78 |
return Ref{Ref: jsonreference.MustCreateRef(refURI)} |
| 79 |
} |
| 80 |
|
| 81 |
// MarshalJSON marshals this ref into a JSON object |
| 82 |
func (r Ref) MarshalJSON() ([]byte, error) { |
| 83 |
str := r.String() |
| 84 |
if str == "" { |
| 85 |
if r.IsRoot() { |
| 86 |
return []byte(`{"$ref":""}`), nil |
| 87 |
} |
| 88 |
return []byte("{}"), nil |
| 89 |
} |
| 90 |
v := map[string]interface{}{"$ref": str} |
| 91 |
return json.Marshal(v) |
| 92 |
} |
| 93 |
|
| 94 |
// UnmarshalJSON unmarshals this ref from a JSON object |
| 95 |
func (r *Ref) UnmarshalJSON(d []byte) error { |
| 96 |
var v map[string]interface{} |
| 97 |
if err := json.Unmarshal(d, &v); err != nil { |
| 98 |
return err |
| 99 |
} |
| 100 |
return r.fromMap(v) |
| 101 |
} |
| 102 |
|
| 103 |
func (r *Ref) fromMap(v map[string]interface{}) error { |
| 104 |
return internal.JSONRefFromMap(&r.Ref, v) |
| 105 |
} |