Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. All Rights Reserved. |
| 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 compiler |
| 16 | |
| 17 | import ( |
| 18 | "errors" |
| 19 | "fmt" |
| 20 | "io/ioutil" |
| 21 | "log" |
| 22 | "net/http" |
| 23 | "net/url" |
| 24 | "path/filepath" |
| 25 | "strings" |
| 26 | |
| 27 | yaml "gopkg.in/yaml.v2" |
| 28 | ) |
| 29 | |
| 30 | var fileCache map[string][]byte |
| 31 | var infoCache map[string]interface{} |
| 32 | var count int64 |
| 33 | |
| 34 | var verboseReader = false |
| 35 | var fileCacheEnable = true |
| 36 | var infoCacheEnable = true |
| 37 | |
| 38 | func initializeFileCache() { |
| 39 | if fileCache == nil { |
| 40 | fileCache = make(map[string][]byte, 0) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func initializeInfoCache() { |
| 45 | if infoCache == nil { |
| 46 | infoCache = make(map[string]interface{}, 0) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func EnableFileCache() { |
| 51 | fileCacheEnable = true |
| 52 | } |
| 53 | |
| 54 | func EnableInfoCache() { |
| 55 | infoCacheEnable = true |
| 56 | } |
| 57 | |
| 58 | func DisableFileCache() { |
| 59 | fileCacheEnable = false |
| 60 | } |
| 61 | |
| 62 | func DisableInfoCache() { |
| 63 | infoCacheEnable = false |
| 64 | } |
| 65 | |
| 66 | func RemoveFromFileCache(fileurl string) { |
| 67 | if !fileCacheEnable { |
| 68 | return |
| 69 | } |
| 70 | initializeFileCache() |
| 71 | delete(fileCache, fileurl) |
| 72 | } |
| 73 | |
| 74 | func RemoveFromInfoCache(filename string) { |
| 75 | if !infoCacheEnable { |
| 76 | return |
| 77 | } |
| 78 | initializeInfoCache() |
| 79 | delete(infoCache, filename) |
| 80 | } |
| 81 | |
| 82 | func GetInfoCache() map[string]interface{} { |
| 83 | if infoCache == nil { |
| 84 | initializeInfoCache() |
| 85 | } |
| 86 | return infoCache |
| 87 | } |
| 88 | |
| 89 | func ClearFileCache() { |
| 90 | fileCache = make(map[string][]byte, 0) |
| 91 | } |
| 92 | |
| 93 | func ClearInfoCache() { |
| 94 | infoCache = make(map[string]interface{}) |
| 95 | } |
| 96 | |
| 97 | func ClearCaches() { |
| 98 | ClearFileCache() |
| 99 | ClearInfoCache() |
| 100 | } |
| 101 | |
| 102 | // FetchFile gets a specified file from the local filesystem or a remote location. |
| 103 | func FetchFile(fileurl string) ([]byte, error) { |
| 104 | var bytes []byte |
| 105 | initializeFileCache() |
| 106 | if fileCacheEnable { |
| 107 | bytes, ok := fileCache[fileurl] |
| 108 | if ok { |
| 109 | if verboseReader { |
| 110 | log.Printf("Cache hit %s", fileurl) |
| 111 | } |
| 112 | return bytes, nil |
| 113 | } |
| 114 | if verboseReader { |
| 115 | log.Printf("Fetching %s", fileurl) |
| 116 | } |
| 117 | } |
| 118 | response, err := http.Get(fileurl) |
| 119 | if err != nil { |
| 120 | return nil, err |
| 121 | } |
| 122 | defer response.Body.Close() |
| 123 | if response.StatusCode != 200 { |
| 124 | return nil, errors.New(fmt.Sprintf("Error downloading %s: %s", fileurl, response.Status)) |
| 125 | } |
| 126 | bytes, err = ioutil.ReadAll(response.Body) |
| 127 | if fileCacheEnable && err == nil { |
| 128 | fileCache[fileurl] = bytes |
| 129 | } |
| 130 | return bytes, err |
| 131 | } |
| 132 | |
| 133 | // ReadBytesForFile reads the bytes of a file. |
| 134 | func ReadBytesForFile(filename string) ([]byte, error) { |
| 135 | // is the filename a url? |
| 136 | fileurl, _ := url.Parse(filename) |
| 137 | if fileurl.Scheme != "" { |
| 138 | // yes, fetch it |
| 139 | bytes, err := FetchFile(filename) |
| 140 | if err != nil { |
| 141 | return nil, err |
| 142 | } |
| 143 | return bytes, nil |
| 144 | } |
| 145 | // no, it's a local filename |
| 146 | bytes, err := ioutil.ReadFile(filename) |
| 147 | if err != nil { |
| 148 | return nil, err |
| 149 | } |
| 150 | return bytes, nil |
| 151 | } |
| 152 | |
| 153 | // ReadInfoFromBytes unmarshals a file as a yaml.MapSlice. |
| 154 | func ReadInfoFromBytes(filename string, bytes []byte) (interface{}, error) { |
| 155 | initializeInfoCache() |
| 156 | if infoCacheEnable { |
| 157 | cachedInfo, ok := infoCache[filename] |
| 158 | if ok { |
| 159 | if verboseReader { |
| 160 | log.Printf("Cache hit info for file %s", filename) |
| 161 | } |
| 162 | return cachedInfo, nil |
| 163 | } |
| 164 | if verboseReader { |
| 165 | log.Printf("Reading info for file %s", filename) |
| 166 | } |
| 167 | } |
| 168 | var info yaml.MapSlice |
| 169 | err := yaml.Unmarshal(bytes, &info) |
| 170 | if err != nil { |
| 171 | return nil, err |
| 172 | } |
| 173 | if infoCacheEnable && len(filename) > 0 { |
| 174 | infoCache[filename] = info |
| 175 | } |
| 176 | return info, nil |
| 177 | } |
| 178 | |
| 179 | // ReadInfoForRef reads a file and return the fragment needed to resolve a $ref. |
| 180 | func ReadInfoForRef(basefile string, ref string) (interface{}, error) { |
| 181 | initializeInfoCache() |
| 182 | if infoCacheEnable { |
| 183 | info, ok := infoCache[ref] |
| 184 | if ok { |
| 185 | if verboseReader { |
| 186 | log.Printf("Cache hit for ref %s#%s", basefile, ref) |
| 187 | } |
| 188 | return info, nil |
| 189 | } |
| 190 | if verboseReader { |
| 191 | log.Printf("Reading info for ref %s#%s", basefile, ref) |
| 192 | } |
| 193 | } |
| 194 | count = count + 1 |
| 195 | basedir, _ := filepath.Split(basefile) |
| 196 | parts := strings.Split(ref, "#") |
| 197 | var filename string |
| 198 | if parts[0] != "" { |
| 199 | filename = parts[0] |
| 200 | if _, err := url.ParseRequestURI(parts[0]); err != nil { |
| 201 | // It is not an URL, so the file is local |
| 202 | filename = basedir + parts[0] |
| 203 | } |
| 204 | } else { |
| 205 | filename = basefile |
| 206 | } |
| 207 | bytes, err := ReadBytesForFile(filename) |
| 208 | if err != nil { |
| 209 | return nil, err |
| 210 | } |
| 211 | info, err := ReadInfoFromBytes(filename, bytes) |
| 212 | if err != nil { |
| 213 | log.Printf("File error: %v\n", err) |
| 214 | } else { |
| 215 | if len(parts) > 1 { |
| 216 | path := strings.Split(parts[1], "/") |
| 217 | for i, key := range path { |
| 218 | if i > 0 { |
| 219 | m, ok := info.(yaml.MapSlice) |
| 220 | if ok { |
| 221 | found := false |
| 222 | for _, section := range m { |
| 223 | if section.Key == key { |
| 224 | info = section.Value |
| 225 | found = true |
| 226 | } |
| 227 | } |
| 228 | if !found { |
| 229 | infoCache[ref] = nil |
| 230 | return nil, NewError(nil, fmt.Sprintf("could not resolve %s", ref)) |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | if infoCacheEnable { |
| 238 | infoCache[ref] = info |
| 239 | } |
| 240 | return info, nil |
| 241 | } |