-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tool/imports): import analysis using upstream jsonnet (#84)
* feat(jsonnet): switch back to google/go-jsonnet Due to recent changes to google/go-jsonnet we can drop our dirty patches and use the official compiler again! * feat(jsonnet): directly compute imports Because we are now manually working with the AST, we do not need to use the TraceImporter anymore, as we have direct access to the importing process. * chore: go mod tidy * fix(tool/imports): switch to upstream jsonnet As google/go-jsonnet#327 is merged, we do not need my custom fork anymore. Now fully switches to the upstream jsonnet library * test(tool/imports): unit test Adds a unit test checking whether all imports are correctly caught
- Loading branch information
Showing
17 changed files
with
301 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package jsonnet | ||
|
||
import ( | ||
"io/ioutil" | ||
"path/filepath" | ||
|
||
jsonnet "github.com/google/go-jsonnet" | ||
"github.com/pkg/errors" | ||
|
||
"github.com/grafana/tanka/pkg/jpath" | ||
"github.com/grafana/tanka/pkg/native" | ||
) | ||
|
||
// EvaluateFile opens the file, reads it into memory and evaluates it afterwards (`Evaluate()`) | ||
func EvaluateFile(jsonnetFile string) (string, error) { | ||
bytes, err := ioutil.ReadFile(jsonnetFile) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
jpath, _, _, err := jpath.Resolve(filepath.Dir(jsonnetFile)) | ||
if err != nil { | ||
return "", errors.Wrap(err, "resolving jpath") | ||
} | ||
return Evaluate(string(bytes), jpath) | ||
} | ||
|
||
// Evaluate renders the given jsonnet into a string | ||
func Evaluate(sonnet string, jpath []string) (string, error) { | ||
importer := jsonnet.FileImporter{ | ||
JPaths: jpath, | ||
} | ||
|
||
vm := jsonnet.MakeVM() | ||
vm.Importer(&importer) | ||
for _, nf := range native.Funcs() { | ||
vm.NativeFunction(nf) | ||
} | ||
|
||
return vm.EvaluateSnippet("main.jsonnet", sonnet) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package jsonnet | ||
|
||
import ( | ||
"io/ioutil" | ||
"path/filepath" | ||
|
||
jsonnet "github.com/google/go-jsonnet" | ||
"github.com/google/go-jsonnet/ast" | ||
"github.com/google/go-jsonnet/toolutils" | ||
"github.com/pkg/errors" | ||
|
||
"github.com/grafana/tanka/pkg/jpath" | ||
"github.com/grafana/tanka/pkg/native" | ||
) | ||
|
||
// TransitiveImports returns all recursive imports of a file | ||
func TransitiveImports(filename string) ([]string, error) { | ||
sonnet, err := ioutil.ReadFile(filename) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "opening file") | ||
} | ||
|
||
jpath, _, _, err := jpath.Resolve(filepath.Dir(filename)) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "resolving JPATH") | ||
} | ||
importer := jsonnet.FileImporter{ | ||
JPaths: jpath, | ||
} | ||
|
||
vm := jsonnet.MakeVM() | ||
vm.Importer(&importer) | ||
for _, nf := range native.Funcs() { | ||
vm.NativeFunction(nf) | ||
} | ||
|
||
node, err := jsonnet.SnippetToAST("main.jsonnet", string(sonnet)) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "creating Jsonnet AST") | ||
} | ||
|
||
imports := make([]string, 0, 0) | ||
err = importRecursive(&imports, vm, node, "main.jsonnet") | ||
|
||
return uniqueStringSlice(imports), err | ||
} | ||
|
||
// importRecursive takes a Jsonnet VM and recursively imports the AST. Every | ||
// found import is added to the `list` string slice, which will ultimately | ||
// contain all recursive imports | ||
func importRecursive(list *[]string, vm *jsonnet.VM, node ast.Node, currentPath string) error { | ||
switch node := node.(type) { | ||
// we have an `import` | ||
case *ast.Import: | ||
p := node.File.Value | ||
|
||
contents, foundAt, err := vm.ImportAST(currentPath, p) | ||
if err != nil { | ||
return errors.Wrap(err, "importing jsonnet") | ||
} | ||
|
||
*list = append(*list, foundAt) | ||
|
||
if err := importRecursive(list, vm, contents, foundAt); err != nil { | ||
return err | ||
} | ||
|
||
// we have an `importstr` | ||
case *ast.ImportStr: | ||
p := node.File.Value | ||
|
||
foundAt, err := vm.ResolveImport(currentPath, p) | ||
if err != nil { | ||
return errors.Wrap(err, "importing string") | ||
} | ||
|
||
*list = append(*list, foundAt) | ||
|
||
// neither `import` nor `importstr`, probably object or similar: try children | ||
default: | ||
for _, child := range toolutils.Children(node) { | ||
if err := importRecursive(list, vm, child, currentPath); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func uniqueStringSlice(s []string) []string { | ||
seen := make(map[string]struct{}, len(s)) | ||
j := 0 | ||
for _, v := range s { | ||
if _, ok := seen[v]; ok { | ||
continue | ||
} | ||
seen[v] = struct{}{} | ||
s[j] = v | ||
j++ | ||
} | ||
return s[:j] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package jsonnet | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// TestTransitiveImports checks that TransitiveImports is able to report all | ||
// recursive imports of a file | ||
func TestTransitiveImports(t *testing.T) { | ||
imports, err := TransitiveImports("testdata/main.jsonnet") | ||
require.NoError(t, err) | ||
assert.ElementsMatch(t, []string{ | ||
"testdata/trees.jsonnet", | ||
|
||
"testdata/trees/apple.jsonnet", | ||
"testdata/trees/cherry.jsonnet", | ||
"testdata/trees/peach.jsonnet", | ||
|
||
"testdata/trees/generic.libsonnet", | ||
}, imports) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Importing testdata | ||
|
||
This directory contains some jsonnet files importing each other, to test if the import analysis tooling works correctly. | ||
|
||
![](test.svg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
local trees = import 'trees.jsonnet'; | ||
|
||
// a list of trees | ||
[ | ||
trees.apple, | ||
trees.cherry, | ||
trees.peach, | ||
] |
Oops, something went wrong.