|
| 1 | +// Copyright 2018 The Linux Foundation |
| 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 schema |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "encoding/json" |
| 20 | + "fmt" |
| 21 | + "io" |
| 22 | + "io/ioutil" |
| 23 | + "net/http" |
| 24 | + "strings" |
| 25 | + |
| 26 | + "github.com/xeipuuv/gojsonreference" |
| 27 | + "github.com/xeipuuv/gojsonschema" |
| 28 | +) |
| 29 | + |
| 30 | +// fsLoaderFactory implements gojsonschema.JSONLoaderFactory by reading files under the specified namespaces from the root of fs. |
| 31 | +type fsLoaderFactory struct { |
| 32 | + namespaces []string |
| 33 | + fs http.FileSystem |
| 34 | +} |
| 35 | + |
| 36 | +// newFSLoaderFactory returns a fsLoaderFactory reading files under the specified namespaces from the root of fs. |
| 37 | +func newFSLoaderFactory(namespaces []string, fs http.FileSystem) *fsLoaderFactory { |
| 38 | + return &fsLoaderFactory{ |
| 39 | + namespaces: namespaces, |
| 40 | + fs: fs, |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func (factory *fsLoaderFactory) New(source string) gojsonschema.JSONLoader { |
| 45 | + return &fsLoader{ |
| 46 | + factory: factory, |
| 47 | + source: source, |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// refContents returns the contents of ref, if available in fsLoaderFactory. |
| 52 | +func (factory *fsLoaderFactory) refContents(ref gojsonreference.JsonReference) ([]byte, error) { |
| 53 | + refStr := ref.String() |
| 54 | + path := "" |
| 55 | + for _, ns := range factory.namespaces { |
| 56 | + if strings.HasPrefix(refStr, ns) { |
| 57 | + path = "/" + strings.TrimPrefix(refStr, ns) |
| 58 | + break |
| 59 | + } |
| 60 | + } |
| 61 | + if path == "" { |
| 62 | + return nil, fmt.Errorf("Schema reference %#v unexpectedly not available in fsLoaderFactory with namespaces %#v", path, factory.namespaces) |
| 63 | + } |
| 64 | + |
| 65 | + f, err := factory.fs.Open(path) |
| 66 | + if err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + defer f.Close() |
| 70 | + |
| 71 | + return ioutil.ReadAll(f) |
| 72 | +} |
| 73 | + |
| 74 | +// fsLoader implements gojsonschema.JSONLoader by reading the document named by source from a fsLoaderFactory. |
| 75 | +type fsLoader struct { |
| 76 | + factory *fsLoaderFactory |
| 77 | + source string |
| 78 | +} |
| 79 | + |
| 80 | +// JsonSource implements gojsonschema.JSONLoader.JsonSource. The "Json" capitalization needs to be maintained to conform to the interface. |
| 81 | +func (l *fsLoader) JsonSource() interface{} { // nolint: golint |
| 82 | + return l.source |
| 83 | +} |
| 84 | + |
| 85 | +func (l *fsLoader) LoadJSON() (interface{}, error) { |
| 86 | + // Based on gojsonschema.jsonReferenceLoader.LoadJSON. |
| 87 | + reference, err := gojsonreference.NewJsonReference(l.source) |
| 88 | + if err != nil { |
| 89 | + return nil, err |
| 90 | + } |
| 91 | + |
| 92 | + refToURL := reference |
| 93 | + refToURL.GetUrl().Fragment = "" |
| 94 | + |
| 95 | + body, err := l.factory.refContents(refToURL) |
| 96 | + if err != nil { |
| 97 | + return nil, err |
| 98 | + } |
| 99 | + |
| 100 | + return decodeJSONUsingNumber(bytes.NewReader(body)) |
| 101 | +} |
| 102 | + |
| 103 | +// decodeJSONUsingNumber returns JSON parsed from an io.Reader |
| 104 | +func decodeJSONUsingNumber(r io.Reader) (interface{}, error) { |
| 105 | + // Copied from gojsonschema. |
| 106 | + var document interface{} |
| 107 | + |
| 108 | + decoder := json.NewDecoder(r) |
| 109 | + decoder.UseNumber() |
| 110 | + |
| 111 | + err := decoder.Decode(&document) |
| 112 | + if err != nil { |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + |
| 116 | + return document, nil |
| 117 | +} |
| 118 | + |
| 119 | +// JsonReference implements gojsonschema.JSONLoader.JsonReference. The "Json" capitalization needs to be maintained to conform to the interface. |
| 120 | +func (l *fsLoader) JsonReference() (gojsonreference.JsonReference, error) { // nolint: golint |
| 121 | + return gojsonreference.NewJsonReference(l.JsonSource().(string)) |
| 122 | +} |
| 123 | + |
| 124 | +func (l *fsLoader) LoaderFactory() gojsonschema.JSONLoaderFactory { |
| 125 | + return l.factory |
| 126 | +} |
0 commit comments