Skip to content

Commit 8a3fcc0

Browse files
authored
Allow reset of ext and TLA vars for VM reuse (google#509)
Allow reset of ext and TLA vars for VM reuse Currently ext and TLA vars can be set but not reset. This makes re-using VMs that have different variables impossible. Add VM methods to be able to reset ext and TLA vars
1 parent cd59751 commit 8a3fcc0

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

jsonnet_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package jsonnet
33
import (
44
"bytes"
55
"reflect"
6+
"strings"
67
"testing"
78
"unicode/utf8"
89

@@ -228,3 +229,39 @@ func TestContents(t *testing.T) {
228229
t.Errorf("Contents should distinguish between different instances even if they have the same data inside")
229230
}
230231
}
232+
233+
func TestExtReset(t *testing.T) {
234+
vm := MakeVM()
235+
vm.ExtVar("fooString", "bar")
236+
vm.ExtCode("fooCode", "true")
237+
_, err := vm.EvaluateAnonymousSnippet("test.jsonnet", `{ str: std.extVar('fooString'), code: std.extVar('fooCode') }`)
238+
if err != nil {
239+
t.Fatalf("unexpected error %v", err)
240+
}
241+
vm.ExtReset()
242+
_, err = vm.EvaluateAnonymousSnippet("test.jsonnet", `{ str: std.extVat('fooString'), code: std.extVar('fooCode') }`)
243+
if err == nil {
244+
t.Fatalf("expected error, got nil")
245+
}
246+
if !strings.Contains(err.Error(), "Undefined external variable") {
247+
t.Errorf("unexpected error %v", err)
248+
}
249+
}
250+
251+
func TestTLAReset(t *testing.T) {
252+
vm := MakeVM()
253+
vm.TLAVar("fooString", "bar")
254+
vm.TLACode("fooCode", "true")
255+
_, err := vm.EvaluateAnonymousSnippet("test.jsonnet", `function (fooString, fooCode) { str: fooString, code: fooCode }`)
256+
if err != nil {
257+
t.Fatalf("unexpected error %v", err)
258+
}
259+
vm.TLAReset()
260+
_, err = vm.EvaluateAnonymousSnippet("test.jsonnet", `function(fooString, fooCode) { str: fooString, code: fooCode }`)
261+
if err == nil {
262+
t.Fatalf("expected error, got nil")
263+
}
264+
if !strings.Contains(err.Error(), "Missing argument") {
265+
t.Errorf("unexpected error %v", err)
266+
}
267+
}

vm.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ func (vm *VM) ExtCode(key string, val string) {
9595
vm.flushValueCache()
9696
}
9797

98+
// ExtReset rests all external variables registered for this VM.
99+
func (vm *VM) ExtReset() {
100+
vm.ext = make(vmExtMap)
101+
vm.flushValueCache()
102+
}
103+
98104
// TLAVar binds a Jsonnet top level argument to the given value.
99105
func (vm *VM) TLAVar(key string, val string) {
100106
vm.tla[key] = vmExt{value: val, isCode: false}
@@ -109,6 +115,11 @@ func (vm *VM) TLACode(key string, val string) {
109115
// Setting a TLA does not require flushing the cache - see above.
110116
}
111117

118+
// TLAReset resets all TLAs registered for this VM.
119+
func (vm *VM) TLAReset() {
120+
vm.tla = make(vmExtMap)
121+
}
122+
112123
// Importer sets Importer to use during evaluation (import callback).
113124
func (vm *VM) Importer(i Importer) {
114125
vm.importer = i

0 commit comments

Comments
 (0)