-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hide the Jsonnet Go implementation behind an interface (#914)
* Hide the Jsonnet Go implementation behind an interface Tanka currently evals jsonnet using the Go native code. However, some other implementations have come up in the past years that could be worth using (ex: https://github.com/CertainLach/jrsonnet, which is much faster) In this PR is the first step: I create an interface where all the jsonnet eval code happens. The Go Jsonnet implementation is now hidden behind this interface. The setting can either be passed as a global flag or as an env spec attribute to be used when exporting (`spec.exportJsonnetImplementation`) * PR review changes `VM` -> `Evaluator` Add some docstrings Add some comments * Select the Jsonnet implementation when constructing the loader Add the `path` as a struct member of the Go-jsonnet implementation. We have this available when we construct the loader. This lets us then drop the same argument from `EvaluateAnonymousSnippet()` in the interface - as not all implementations need it. --------- Co-authored-by: Iain Lane <iain.lane@grafana.com>
- Loading branch information
1 parent
253c0f2
commit 6a4f83f
Showing
21 changed files
with
201 additions
and
107 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
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,32 @@ | ||
package goimpl | ||
|
||
import ( | ||
"github.com/google/go-jsonnet" | ||
"github.com/grafana/tanka/pkg/jsonnet/implementations/types" | ||
) | ||
|
||
type JsonnetGoVM struct { | ||
vm *jsonnet.VM | ||
|
||
path string | ||
} | ||
|
||
func (vm *JsonnetGoVM) EvaluateAnonymousSnippet(snippet string) (string, error) { | ||
return vm.vm.EvaluateAnonymousSnippet(vm.path, snippet) | ||
} | ||
|
||
func (vm *JsonnetGoVM) EvaluateFile(filename string) (string, error) { | ||
return vm.vm.EvaluateFile(filename) | ||
} | ||
|
||
type JsonnetGoImplementation struct { | ||
Path string | ||
} | ||
|
||
func (i *JsonnetGoImplementation) MakeEvaluator(importPaths []string, extCode map[string]string, tlaCode map[string]string, maxStack int) types.JsonnetEvaluator { | ||
return &JsonnetGoVM{ | ||
vm: MakeRawVM(importPaths, extCode, tlaCode, maxStack), | ||
|
||
path: i.Path, | ||
} | ||
} |
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
2 changes: 1 addition & 1 deletion
2
pkg/jsonnet/tk.libsonnet.go → ...et/implementations/goimpl/tk.libsonnet.go
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package jsonnet | ||
package goimpl | ||
|
||
import jsonnet "github.com/google/go-jsonnet" | ||
|
||
|
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,33 @@ | ||
package goimpl | ||
|
||
import ( | ||
"github.com/google/go-jsonnet" | ||
"github.com/grafana/tanka/pkg/jsonnet/native" | ||
) | ||
|
||
// MakeRawVM returns a Jsonnet VM with some extensions of Tanka, including: | ||
// - extended importer | ||
// - extCode and tlaCode applied | ||
// - native functions registered | ||
// This is exposed because Go is used for advanced use cases, like finding transitive imports or linting. | ||
func MakeRawVM(importPaths []string, extCode map[string]string, tlaCode map[string]string, maxStack int) *jsonnet.VM { | ||
vm := jsonnet.MakeVM() | ||
vm.Importer(newExtendedImporter(importPaths)) | ||
|
||
for k, v := range extCode { | ||
vm.ExtCode(k, v) | ||
} | ||
for k, v := range tlaCode { | ||
vm.TLACode(k, v) | ||
} | ||
|
||
for _, nf := range native.Funcs() { | ||
vm.NativeFunction(nf) | ||
} | ||
|
||
if maxStack > 0 { | ||
vm.MaxStack = maxStack | ||
} | ||
|
||
return vm | ||
} |
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,13 @@ | ||
package types | ||
|
||
// JsonnetEvaluator represents a struct that can evaluate Jsonnet code | ||
// It is configured with import paths, external code and top-level arguments | ||
type JsonnetEvaluator interface { | ||
EvaluateAnonymousSnippet(snippet string) (string, error) | ||
EvaluateFile(filename string) (string, error) | ||
} | ||
|
||
// JsonnetImplementation is a factory for JsonnetEvaluator | ||
type JsonnetImplementation interface { | ||
MakeEvaluator(importPaths []string, extCode map[string]string, tlaCode map[string]string, maxStack int) JsonnetEvaluator | ||
} |
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
Oops, something went wrong.