mirror of
https://github.com/google/go-jsonnet.git
synced 2025-09-28 17:01:02 +02:00
Support for import callbacks
It was already there, it was a matter of exposing it in the API
This commit is contained in:
parent
5b1798233c
commit
0c43340142
@ -147,9 +147,9 @@ type MemoryImporter struct {
|
|||||||
data map[string]string
|
data map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (importer *MemoryImporter) Import(dir, importedPath string) (*ImportedData, error) {
|
func (importer *MemoryImporter) Import(dir, importedPath string) *ImportedData {
|
||||||
if content, ok := importer.data[importedPath]; ok {
|
if content, ok := importer.data[importedPath]; ok {
|
||||||
return &ImportedData{content: content, foundHere: importedPath}, nil
|
return &ImportedData{content: content, foundHere: importedPath}
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("Import not available %v", importedPath)
|
return &ImportedData{err: fmt.Errorf("Import not available %v", importedPath)}
|
||||||
}
|
}
|
||||||
|
20
main_test.go
20
main_test.go
@ -195,3 +195,23 @@ func TestMinimalError(t *testing.T) {
|
|||||||
|
|
||||||
// TODO(sbarzowski) test pretty errors once they are stable-ish
|
// TODO(sbarzowski) test pretty errors once they are stable-ish
|
||||||
// probably "golden" pattern is the right one for that
|
// probably "golden" pattern is the right one for that
|
||||||
|
|
||||||
|
func TestCustomImporter(t *testing.T) {
|
||||||
|
vm := MakeVM()
|
||||||
|
vm.Importer(&MemoryImporter{
|
||||||
|
map[string]string{
|
||||||
|
"a.jsonnet": "2 + 2",
|
||||||
|
"b.jsonnet": "3 + 3",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
input := `[import "a.jsonnet", importstr "b.jsonnet"]`
|
||||||
|
expected := `[ 4, "3 + 3" ]`
|
||||||
|
actual, err := vm.EvaluateSnippet("custom_import.jsonnet", input)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
actual = removeExcessiveWhitespace(actual)
|
||||||
|
if actual != expected {
|
||||||
|
t.Errorf("Expected %v, but got %v", expected, actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
8
vm.go
8
vm.go
@ -52,6 +52,7 @@ func MakeVM() *VM {
|
|||||||
MaxStack: 500,
|
MaxStack: 500,
|
||||||
ext: make(vmExtMap),
|
ext: make(vmExtMap),
|
||||||
ef: ErrorFormatter{pretty: true, colorful: true, MaxStackTraceSize: 20},
|
ef: ErrorFormatter{pretty: true, colorful: true, MaxStackTraceSize: 20},
|
||||||
|
importer: &FileImporter{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,6 +66,11 @@ func (vm *VM) ExtCode(key string, val string) {
|
|||||||
vm.ext[key] = vmExt{value: val, isCode: true}
|
vm.ext[key] = vmExt{value: val, isCode: true}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Importer sets Importer to use during evaluation (import callback)
|
||||||
|
func (vm *VM) Importer(i Importer) {
|
||||||
|
vm.importer = i
|
||||||
|
}
|
||||||
|
|
||||||
func (vm *VM) evaluateSnippet(filename string, snippet string) (output string, err error) {
|
func (vm *VM) evaluateSnippet(filename string, snippet string) (output string, err error) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
@ -75,7 +81,7 @@ func (vm *VM) evaluateSnippet(filename string, snippet string) (output string, e
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
output, err = evaluate(node, vm.ext, vm.MaxStack, &FileImporter{})
|
output, err = evaluate(node, vm.ext, vm.MaxStack, vm.importer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user