Skip to content
This repository has been archived by the owner on Nov 17, 2021. It is now read-only.

Commit

Permalink
Update to go-jsonnet 0.12.1
Browse files Browse the repository at this point in the history
Relevant changelog (0.10.x -> 0.12.x):

- Fix std.parseHex that rejected certain correct inputs
- Added std.find and std.findSubstr
- Added std.parseJson
- std.native("foo") now returns null if foo doesn't exist, instead of an error. This is useful for falling back on Jsonnet implementations if a faster native one isn't available.
- sort and set functions can now be given a "keyF" function for controlling the ordering
- std.trace for debugging
- std.parseHex
- std.parseOctal
- Major (2x-10x) improvements in performance via a substantial refactoring of the interpreter
  • Loading branch information
Marko Mikulicic committed Jan 17, 2019
1 parent 59bc7de commit 34b86e4
Show file tree
Hide file tree
Showing 23 changed files with 283,822 additions and 120,567 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ GOFMT ?= gofmt
GINKGO ?= ginkgo
GO_BINDATA ?= go-bindata

JSONNET_FILES = testdata/kubecfg_test.jsonnet examples/guestbook.jsonnet
JSONNET_FILES = testdata/kubecfg_test.jsonnet examples/guestbook.jsonnet testdata/import_test.jsonnet
# TODO: Simplify this once ./... ignores ./vendor
GO_PACKAGES = ./cmd/... ./utils/... ./pkg/...

Expand Down
1 change: 1 addition & 0 deletions testdata/import_test.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(import "imported.jsonnet") + (import "imported.jsonnet")
1 change: 1 addition & 0 deletions testdata/imported.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
43 changes: 24 additions & 19 deletions utils/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"strings"
"time"

"github.com/elazarl/go-bindata-assetfs"
assetfs "github.com/elazarl/go-bindata-assetfs"
jsonnet "github.com/google/go-jsonnet"
log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -81,60 +81,65 @@ func MakeUniversalImporter(searchUrls []*url.URL) jsonnet.Importer {
return &universalImporter{
BaseSearchURLs: searchUrls,
HTTPClient: &http.Client{Transport: t},
cache: map[string]jsonnet.Contents{},
}
}

type universalImporter struct {
BaseSearchURLs []*url.URL
HTTPClient *http.Client
cache map[string]jsonnet.Contents
}

func (importer *universalImporter) Import(dir, importedPath string) (*jsonnet.ImportedData, error) {
func (importer *universalImporter) Import(dir, importedPath string) (jsonnet.Contents, string, error) {
log.Debugf("Importing %q from %q", importedPath, dir)

candidateURLs, err := importer.expandImportToCandidateURLs(dir, importedPath)
if err != nil {
return nil, fmt.Errorf("Could not get candidate URLs for when importing %s (import dir is %s)", importedPath, dir)
return jsonnet.Contents{}, "", fmt.Errorf("Could not get candidate URLs for when importing %s (import dir is %s)", importedPath, dir)
}

var tried []string
for _, u := range candidateURLs {
tried = append(tried, u.String())
importedData, err := importer.tryImport(u)
foundAt := u.String()
if c, ok := importer.cache[foundAt]; ok {
return c, foundAt, nil
}

tried = append(tried, foundAt)
importedData, err := importer.tryImport(foundAt)
if err == nil {
return importedData, nil
importer.cache[foundAt] = importedData
return importedData, foundAt, nil
} else if err != errNotFound {
return nil, err
return jsonnet.Contents{}, "", err
}
}

return nil, fmt.Errorf("Couldn't open import %q, no match locally or in library search paths. Tried: %s",
return jsonnet.Contents{}, "", fmt.Errorf("Couldn't open import %q, no match locally or in library search paths. Tried: %s",
importedPath,
strings.Join(tried, ";"),
)
}

func (importer *universalImporter) tryImport(url *url.URL) (*jsonnet.ImportedData, error) {
res, err := importer.HTTPClient.Get(url.String())
func (importer *universalImporter) tryImport(url string) (jsonnet.Contents, error) {
res, err := importer.HTTPClient.Get(url)
if err != nil {
return nil, err
return jsonnet.Contents{}, err
}
defer res.Body.Close()
log.Debugf("GET %s -> %s", url, res.Status)
log.Debugf("GET %q -> %s", url, res.Status)
if res.StatusCode == http.StatusNotFound {
return nil, errNotFound
return jsonnet.Contents{}, errNotFound
} else if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("error reading content: %s", res.Status)
return jsonnet.Contents{}, fmt.Errorf("error reading content: %s", res.Status)
}

bodyBytes, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
return jsonnet.Contents{}, err
}
return &jsonnet.ImportedData{
FoundHere: url.String(),
Content: string(bodyBytes),
}, nil
return jsonnet.MakeContents(string(bodyBytes)), nil
}

func (importer *universalImporter) expandImportToCandidateURLs(dir, importedPath string) ([]*url.URL, error) {
Expand Down
1 change: 1 addition & 0 deletions utils/nativefuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func resolveImage(resolver Resolver, image string) (string, error) {

// RegisterNativeFuncs adds kubecfg's native jsonnet functions to provided VM
func RegisterNativeFuncs(vm *jsonnet.VM, resolver Resolver) {
// TODO(mkm): go-jsonnet 0.12.x now contains native std.parseJson; deprecate and remove this one.
vm.NativeFunction(&jsonnet.NativeFunction{
Name: "parseJson",
Params: []jsonnetAst.Identifier{"json"},
Expand Down
1 change: 1 addition & 0 deletions vendor/github.com/google/go-jsonnet/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

179 changes: 179 additions & 0 deletions vendor/github.com/google/go-jsonnet/ast/fodder.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 34b86e4

Please sign in to comment.