Update vendored dependencies

This commit is contained in:
Christian Huffman
2020-07-23 15:19:22 -04:00
parent 547e88e4fb
commit bda8f8c0ae
909 changed files with 119096 additions and 130549 deletions

View File

@@ -17,13 +17,14 @@ package compiler
import (
"errors"
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"net/http"
"net/url"
"path/filepath"
"strings"
yaml "gopkg.in/yaml.v2"
)
var fileCache map[string][]byte
@@ -31,6 +32,8 @@ var infoCache map[string]interface{}
var count int64
var verboseReader = false
var fileCacheEnable = true
var infoCacheEnable = true
func initializeFileCache() {
if fileCache == nil {
@@ -44,29 +47,84 @@ func initializeInfoCache() {
}
}
func EnableFileCache() {
fileCacheEnable = true
}
func EnableInfoCache() {
infoCacheEnable = true
}
func DisableFileCache() {
fileCacheEnable = false
}
func DisableInfoCache() {
infoCacheEnable = false
}
func RemoveFromFileCache(fileurl string) {
if !fileCacheEnable {
return
}
initializeFileCache()
delete(fileCache, fileurl)
}
func RemoveFromInfoCache(filename string) {
if !infoCacheEnable {
return
}
initializeInfoCache()
delete(infoCache, filename)
}
func GetInfoCache() map[string]interface{} {
if infoCache == nil {
initializeInfoCache()
}
return infoCache
}
func ClearFileCache() {
fileCache = make(map[string][]byte, 0)
}
func ClearInfoCache() {
infoCache = make(map[string]interface{})
}
func ClearCaches() {
ClearFileCache()
ClearInfoCache()
}
// FetchFile gets a specified file from the local filesystem or a remote location.
func FetchFile(fileurl string) ([]byte, error) {
var bytes []byte
initializeFileCache()
bytes, ok := fileCache[fileurl]
if ok {
if verboseReader {
log.Printf("Cache hit %s", fileurl)
if fileCacheEnable {
bytes, ok := fileCache[fileurl]
if ok {
if verboseReader {
log.Printf("Cache hit %s", fileurl)
}
return bytes, nil
}
if verboseReader {
log.Printf("Fetching %s", fileurl)
}
return bytes, nil
}
if verboseReader {
log.Printf("Fetching %s", fileurl)
}
response, err := http.Get(fileurl)
if err != nil {
return nil, err
}
defer response.Body.Close()
if response.StatusCode != 200 {
return nil, errors.New(fmt.Sprintf("Error downloading %s: %s", fileurl, response.Status))
}
defer response.Body.Close()
bytes, err = ioutil.ReadAll(response.Body)
if err == nil {
if fileCacheEnable && err == nil {
fileCache[fileurl] = bytes
}
return bytes, err
@@ -95,22 +153,24 @@ func ReadBytesForFile(filename string) ([]byte, error) {
// ReadInfoFromBytes unmarshals a file as a yaml.MapSlice.
func ReadInfoFromBytes(filename string, bytes []byte) (interface{}, error) {
initializeInfoCache()
cachedInfo, ok := infoCache[filename]
if ok {
if verboseReader {
log.Printf("Cache hit info for file %s", filename)
if infoCacheEnable {
cachedInfo, ok := infoCache[filename]
if ok {
if verboseReader {
log.Printf("Cache hit info for file %s", filename)
}
return cachedInfo, nil
}
if verboseReader {
log.Printf("Reading info for file %s", filename)
}
return cachedInfo, nil
}
if verboseReader {
log.Printf("Reading info for file %s", filename)
}
var info yaml.MapSlice
err := yaml.Unmarshal(bytes, &info)
if err != nil {
return nil, err
}
if len(filename) > 0 {
if infoCacheEnable && len(filename) > 0 {
infoCache[filename] = info
}
return info, nil
@@ -119,7 +179,7 @@ func ReadInfoFromBytes(filename string, bytes []byte) (interface{}, error) {
// ReadInfoForRef reads a file and return the fragment needed to resolve a $ref.
func ReadInfoForRef(basefile string, ref string) (interface{}, error) {
initializeInfoCache()
{
if infoCacheEnable {
info, ok := infoCache[ref]
if ok {
if verboseReader {
@@ -127,16 +187,20 @@ func ReadInfoForRef(basefile string, ref string) (interface{}, error) {
}
return info, nil
}
}
if verboseReader {
log.Printf("Reading info for ref %s#%s", basefile, ref)
if verboseReader {
log.Printf("Reading info for ref %s#%s", basefile, ref)
}
}
count = count + 1
basedir, _ := filepath.Split(basefile)
parts := strings.Split(ref, "#")
var filename string
if parts[0] != "" {
filename = basedir + parts[0]
filename = parts[0]
if _, err := url.ParseRequestURI(parts[0]); err != nil {
// It is not an URL, so the file is local
filename = basedir + parts[0]
}
} else {
filename = basefile
}
@@ -170,6 +234,8 @@ func ReadInfoForRef(basefile string, ref string) (interface{}, error) {
}
}
}
infoCache[ref] = info
if infoCacheEnable {
infoCache[ref] = info
}
return info, nil
}