podman
193 строки · 4.2 Кб
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
15package spec16
17import (18"bytes"19"encoding/gob"20"encoding/json"21"net/http"22"os"23"path/filepath"24
25"github.com/go-openapi/jsonreference"26)
27
28// Refable is a struct for things that accept a $ref property
29type Refable struct {30Ref Ref
31}
32
33// MarshalJSON marshals the ref to json
34func (r Refable) MarshalJSON() ([]byte, error) {35return r.Ref.MarshalJSON()36}
37
38// UnmarshalJSON unmarshalss the ref from json
39func (r *Refable) UnmarshalJSON(d []byte) error {40return json.Unmarshal(d, &r.Ref)41}
42
43// Ref represents a json reference that is potentially resolved
44type Ref struct {45jsonreference.Ref46}
47
48// RemoteURI gets the remote uri part of the ref
49func (r *Ref) RemoteURI() string {50if r.String() == "" {51return ""52}53
54u := *r.GetURL()55u.Fragment = ""56return u.String()57}
58
59// IsValidURI returns true when the url the ref points to can be found
60func (r *Ref) IsValidURI(basepaths ...string) bool {61if r.String() == "" {62return true63}64
65v := r.RemoteURI()66if v == "" {67return true68}69
70if r.HasFullURL {71//nolint:noctx,gosec72rr, err := http.Get(v)73if err != nil {74return false75}76defer rr.Body.Close()77
78return rr.StatusCode/100 == 279}80
81if !(r.HasFileScheme || r.HasFullFilePath || r.HasURLPathOnly) {82return false83}84
85// check for local file86pth := v87if r.HasURLPathOnly {88base := "."89if len(basepaths) > 0 {90base = filepath.Dir(filepath.Join(basepaths...))91}92p, e := filepath.Abs(filepath.ToSlash(filepath.Join(base, pth)))93if e != nil {94return false95}96pth = p97}98
99fi, err := os.Stat(filepath.ToSlash(pth))100if err != nil {101return false102}103
104return !fi.IsDir()105}
106
107// Inherits creates a new reference from a parent and a child
108// If the child cannot inherit from the parent, an error is returned
109func (r *Ref) Inherits(child Ref) (*Ref, error) {110ref, err := r.Ref.Inherits(child.Ref)111if err != nil {112return nil, err113}114return &Ref{Ref: *ref}, nil115}
116
117// NewRef creates a new instance of a ref object
118// returns an error when the reference uri is an invalid uri
119func NewRef(refURI string) (Ref, error) {120ref, err := jsonreference.New(refURI)121if err != nil {122return Ref{}, err123}124return Ref{Ref: ref}, nil125}
126
127// MustCreateRef creates a ref object but panics when refURI is invalid.
128// Use the NewRef method for a version that returns an error.
129func MustCreateRef(refURI string) Ref {130return Ref{Ref: jsonreference.MustCreateRef(refURI)}131}
132
133// MarshalJSON marshals this ref into a JSON object
134func (r Ref) MarshalJSON() ([]byte, error) {135str := r.String()136if str == "" {137if r.IsRoot() {138return []byte(`{"$ref":""}`), nil139}140return []byte("{}"), nil141}142v := map[string]interface{}{"$ref": str}143return json.Marshal(v)144}
145
146// UnmarshalJSON unmarshals this ref from a JSON object
147func (r *Ref) UnmarshalJSON(d []byte) error {148var v map[string]interface{}149if err := json.Unmarshal(d, &v); err != nil {150return err151}152return r.fromMap(v)153}
154
155// GobEncode provides a safe gob encoder for Ref
156func (r Ref) GobEncode() ([]byte, error) {157var b bytes.Buffer158raw, err := r.MarshalJSON()159if err != nil {160return nil, err161}162err = gob.NewEncoder(&b).Encode(raw)163return b.Bytes(), err164}
165
166// GobDecode provides a safe gob decoder for Ref
167func (r *Ref) GobDecode(b []byte) error {168var raw []byte169buf := bytes.NewBuffer(b)170err := gob.NewDecoder(buf).Decode(&raw)171if err != nil {172return err173}174return json.Unmarshal(raw, r)175}
176
177func (r *Ref) fromMap(v map[string]interface{}) error {178if v == nil {179return nil180}181
182if vv, ok := v["$ref"]; ok {183if str, ok := vv.(string); ok {184ref, err := jsonreference.New(str)185if err != nil {186return err187}188*r = Ref{Ref: ref}189}190}191
192return nil193}
194