Skip to content

Commit 3144d87

Browse files
authored
1.7.x: add default values for interpreter execute (#1598)
Update interpreter API to be more consistent with recent changes in 1.8.
1 parent 9a89045 commit 3144d87

File tree

4 files changed

+33
-28
lines changed

4 files changed

+33
-28
lines changed

atlas-core/src/main/scala/com/netflix/atlas/core/stacklang/Interpreter.scala

+13-9
Original file line numberDiff line numberDiff line change
@@ -106,21 +106,25 @@ case class Interpreter(vocabulary: List[Word]) {
106106
if (s.program.isEmpty) s.context else execute(nextStep(s))
107107
}
108108

109-
final def execute(program: List[Any], context: Context, unfreeze: Boolean = true): Context = {
109+
final def executeProgram(
110+
program: List[Any],
111+
context: Context,
112+
unfreeze: Boolean = true
113+
): Context = {
110114
val result = execute(Step(program, context))
111115
if (unfreeze) result.unfreeze else result
112116
}
113117

114-
final def execute(program: List[Any]): Context = {
115-
execute(program, Context(this, Nil, Map.empty))
118+
final def executeProgram(program: List[Any]): Context = {
119+
executeProgram(program, Context(this, Nil, Map.empty))
116120
}
117121

118-
final def execute(program: String): Context = {
119-
execute(splitAndTrim(program))
120-
}
121-
122-
final def execute(program: String, vars: Map[String, Any], features: Features): Context = {
123-
execute(splitAndTrim(program), Context(this, Nil, vars, vars, features = features))
122+
final def execute(
123+
program: String,
124+
vars: Map[String, Any] = Map.empty,
125+
features: Features = Features.STABLE
126+
): Context = {
127+
executeProgram(splitAndTrim(program), Context(this, Nil, vars, vars, features = features))
124128
}
125129

126130
@scala.annotation.tailrec

atlas-core/src/main/scala/com/netflix/atlas/core/stacklang/StandardVocabulary.scala

+5-4
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ object StandardVocabulary extends Vocabulary {
5757
override def matches(stack: List[Any]): Boolean = true
5858

5959
override def execute(context: Context): Context = {
60-
context.interpreter.execute(body, context, unfreeze = false)
60+
context.interpreter.executeProgram(body, context, unfreeze = false)
6161
}
6262

6363
override def summary: String =
@@ -81,7 +81,7 @@ object StandardVocabulary extends Vocabulary {
8181
override def execute(context: Context): Context = {
8282
context.stack match {
8383
case (vs: List[_]) :: stack =>
84-
context.interpreter.execute(vs, context.copy(stack = stack), unfreeze = false)
84+
context.interpreter.executeProgram(vs, context.copy(stack = stack), unfreeze = false)
8585
case _ => invalidStack
8686
}
8787
}
@@ -198,7 +198,7 @@ object StandardVocabulary extends Vocabulary {
198198
context.stack match {
199199
case (f: List[_]) :: (vs: List[_]) :: stack =>
200200
vs.reverse.foldLeft(context.copy(stack = stack)) { (c, v) =>
201-
c.interpreter.execute(f, c.copy(stack = v :: c.stack), unfreeze = false)
201+
c.interpreter.executeProgram(f, c.copy(stack = v :: c.stack), unfreeze = false)
202202
}
203203
case _ => invalidStack
204204
}
@@ -362,7 +362,8 @@ object StandardVocabulary extends Vocabulary {
362362
val init = context.copy(stack = stack)
363363
val res = vs.foldLeft(List.empty[Any] -> init) {
364364
case ((rs, c), v) =>
365-
val rc = c.interpreter.execute(f, c.copy(stack = v :: c.stack), unfreeze = false)
365+
val rc =
366+
c.interpreter.executeProgram(f, c.copy(stack = v :: c.stack), unfreeze = false)
366367
(rc.stack.head :: rs) -> rc.copy(stack = rc.stack.tail)
367368
}
368369
res._2.copy(stack = res._1.reverse :: res._2.stack)

atlas-core/src/test/scala/com/netflix/atlas/core/stacklang/InterpreterSuite.scala

+14-14
Original file line numberDiff line numberDiff line change
@@ -40,82 +40,82 @@ class InterpreterSuite extends FunSuite {
4040
}
4141

4242
test("empty") {
43-
assertEquals(interpreter.execute(Nil), context(Nil))
43+
assertEquals(interpreter.executeProgram(Nil), context(Nil))
4444
}
4545

4646
test("push items") {
47-
assertEquals(interpreter.execute(List("foo", "bar")), context(List("bar", "foo")))
47+
assertEquals(interpreter.executeProgram(List("foo", "bar")), context(List("bar", "foo")))
4848
}
4949

5050
test("execute word") {
51-
assertEquals(interpreter.execute(List(":push-foo")), context(List("foo")))
51+
assertEquals(interpreter.executeProgram(List(":push-foo")), context(List("foo")))
5252
}
5353

5454
test("overloaded word") {
55-
assertEquals(interpreter.execute(List(":overloaded")), context(List("one")))
55+
assertEquals(interpreter.executeProgram(List(":overloaded")), context(List("one")))
5656
}
5757

5858
test("overloaded word and some don't match") {
59-
assertEquals(interpreter.execute(List(":overloaded2")), context(List("two")))
59+
assertEquals(interpreter.executeProgram(List(":overloaded2")), context(List("two")))
6060
}
6161

6262
test("word with no matches") {
6363
val e = intercept[IllegalStateException] {
64-
interpreter.execute(List(":no-match"))
64+
interpreter.executeProgram(List(":no-match"))
6565
}
6666
val expected = "no matches for word ':no-match' with stack [], candidates: [exception]"
6767
assertEquals(e.getMessage, expected)
6868
}
6969

7070
test("using unstable word fails by default") {
7171
val e = intercept[IllegalStateException] {
72-
interpreter.execute(List(":unstable"))
72+
interpreter.executeProgram(List(":unstable"))
7373
}
7474
val expected = "to use :unstable enable unstable features"
7575
assertEquals(e.getMessage, expected)
7676
}
7777

7878
test("unknown word") {
7979
val e = intercept[IllegalStateException] {
80-
interpreter.execute(List("foo", ":unknown"))
80+
interpreter.executeProgram(List("foo", ":unknown"))
8181
}
8282
assertEquals(e.getMessage, "unknown word ':unknown'")
8383
}
8484

8585
test("unmatched closing paren") {
8686
val e = intercept[IllegalStateException] {
87-
interpreter.execute(List(")"))
87+
interpreter.executeProgram(List(")"))
8888
}
8989
assertEquals(e.getMessage, "unmatched closing parenthesis")
9090
}
9191

9292
test("unmatched closing paren 2") {
9393
val e = intercept[IllegalStateException] {
94-
interpreter.execute(List("(", ")", ")"))
94+
interpreter.executeProgram(List("(", ")", ")"))
9595
}
9696
assertEquals(e.getMessage, "unmatched closing parenthesis")
9797
}
9898

9999
test("unmatched opening paren") {
100100
val e = intercept[IllegalStateException] {
101-
interpreter.execute(List("("))
101+
interpreter.executeProgram(List("("))
102102
}
103103
assertEquals(e.getMessage, "unmatched opening parenthesis")
104104
}
105105

106106
test("list") {
107107
val list = List("(", "1", ")")
108-
assertEquals(interpreter.execute(list), context(List(List("1"))))
108+
assertEquals(interpreter.executeProgram(list), context(List(List("1"))))
109109
}
110110

111111
test("nested list") {
112112
val list = List("(", "1", "(", ")", ")")
113-
assertEquals(interpreter.execute(list), context(List(List("1", "(", ")"))))
113+
assertEquals(interpreter.executeProgram(list), context(List(List("1", "(", ")"))))
114114
}
115115

116116
test("multiple lists") {
117117
val list = List("(", "1", ")", "(", "2", ")")
118-
assertEquals(interpreter.execute(list), context(List(List("2"), List("1"))))
118+
assertEquals(interpreter.executeProgram(list), context(List(List("2"), List("1"))))
119119
}
120120

121121
test("debug") {

atlas-webapi/src/main/scala/com/netflix/atlas/webapi/ExprApi.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class ExprApi extends WebApi {
154154
// macros it alwasy returns true. This ensures the operation will actually be successful before
155155
// returning to a user.
156156
private def execWorks(interpreter: Interpreter, w: Word, ctxt: Context): Boolean = {
157-
Try(interpreter.execute(List(s":${w.name}"), ctxt)).isSuccess
157+
Try(interpreter.executeProgram(List(s":${w.name}"), ctxt)).isSuccess
158158
}
159159

160160
private def matches(interpreter: Interpreter, w: Word, ctxt: Context): Boolean = {

0 commit comments

Comments
 (0)