Skip to content
Browse files

removed tailrec annotation to allow compiling under 2.7.7; added a fe…

…w more tests
  • Loading branch information...
1 parent 7d8ba56 commit df78b45485490a8f3ff31b50ee6c743a1f9fcd4f Jeremy Cloud committed Dec 15, 2010
View
2 project/build.properties
@@ -3,6 +3,6 @@
project.organization=com.twitter
project.name=streamyj
sbt.version=0.7.4
-project.version=0.2-SNAPSHOT
+project.version=0.2
build.scala.versions=2.8.1 2.7.7
project.initialize=false
View
30 src/main/scala/com/twitter/streamyj/Streamy.scala
@@ -3,7 +3,6 @@ package com.twitter.streamyj
import java.io.{File, Reader, StringWriter}
import org.codehaus.jackson._
import org.codehaus.jackson.JsonToken._
-import scala.annotation.tailrec
/**
* Just store the PartialFunction type for parse functions so
@@ -62,11 +61,8 @@ class Streamy(parser: JsonParser) {
* Looks at the next token without consuming it.
*/
def peek(): StreamyToken = {
- peekedToken match {
- case NotAvailable =>
- peekedToken = StreamToken(parser)
- case _ =>
- }
+ if (peekedToken == NotAvailable)
+ peekedToken = StreamToken(parser)
peekedToken
}
@@ -113,7 +109,7 @@ class Streamy(parser: JsonParser) {
/**
* Reads an object from open-curly to close-curly. Any field name not
- * recognized by the given PartialFunction will be skipped.
+ * recognized by the given PartialFunction will be skipped.
* If the PartialFunction matches a field name, it MUST either fully
* read the corresponding value or skip it. Not doing so will leave
* the parser in an unpredictable state.
@@ -143,7 +139,7 @@ class Streamy(parser: JsonParser) {
* the parser in an unpredictable state.
*/
def readObjectBody(fn: ObjectParseFunc) {
- @tailrec def loop() {
+ def loop() {
next() match {
case EndObject => // done
case FieldName(name) =>
@@ -173,7 +169,7 @@ class Streamy(parser: JsonParser) {
* the parser in an unpredictable state.
*/
def foldObjectBody[T](start: T)(fn: PartialFunction[(T,String), T]): T = {
- @tailrec def loop(accum: T): T = {
+ def loop(accum: T): T = {
next() match {
case EndObject => accum
case FieldName(name) =>
@@ -218,7 +214,7 @@ class Streamy(parser: JsonParser) {
* applies the supplied function to the current
* JSON array. Note that the current token should
* be the start of the array (either an opening bracket or null)
- * The given function MUST either fully read the corresponding value
+ * The given function MUST either fully read the corresponding value
* or skip it. Not doing so will leave the parser in an unpredictable state.
*/
def readArray(fn: ArrayParseFunc) {
@@ -230,20 +226,20 @@ class Streamy(parser: JsonParser) {
* applies the supplied function to the current
* JSON array. Note that the current token should
* be the start of the array (either an opening bracket or null)
- * The given function MUST either fully read the corresponding value
+ * The given function MUST either fully read the corresponding value
* or skip it. Not doing so will leave the parser in an unpredictable state.
*/
def readArrayOption(fn: ArrayParseFunc): Boolean = {
startArrayOption() && { readArrayBody(fn); true }
}
/**
- * Reads the body of an array upto the close-bracket.
- * The given function MUST either fully read the corresponding value
+ * Reads the body of an array upto the close-bracket.
+ * The given function MUST either fully read the corresponding value
* or skip it. Not doing so will leave the parser in an unpredictable state.
*/
def readArrayBody(fn: ArrayParseFunc) {
- @tailrec def loop(index: Int) {
+ def loop(index: Int) {
if (peek() == EndArray) {
next() // skip ]
} else {
@@ -256,7 +252,7 @@ class Streamy(parser: JsonParser) {
/**
* Reads an array using an accumulator.
- * The given function MUST either fully read the corresponding value
+ * The given function MUST either fully read the corresponding value
* or skip it. Not doing so will leave the parser in an unpredictable state.
*/
def foldArray[T](start: T)(fn: (T,Int) => T): T = {
@@ -266,11 +262,11 @@ class Streamy(parser: JsonParser) {
/**
* Reads an array using an accumulator.
- * The given function MUST either fully read the corresponding value
+ * The given function MUST either fully read the corresponding value
* or skip it. Not doing so will leave the parser in an unpredictable state.
*/
def foldArrayBody[T](start: T)(fn: (T,Int) => T): T = {
- @tailrec def loop(accum: T, index: Int): T = {
+ def loop(accum: T, index: Int): T = {
if (peek() == EndArray) {
next() // skip ]
accum
View
18 src/test/scala/com/twitter/streamyj/StreamySpec.scala
@@ -7,6 +7,24 @@ import scala.collection.mutable.ListBuffer
object StreamySpec extends Specification {
"Streamy" should {
+ "next() advances and returns next token" in {
+ val s = Streamy("1 2 3 4")
+ s.next() mustEqual ValueLong(1)
+ s.next() mustEqual ValueLong(2)
+ s.next() mustEqual ValueLong(3)
+ s.next() mustEqual ValueLong(4)
+ }
+
+ "peek() doesn't advance" in {
+ val s = Streamy("1 2 3 4")
+ s.peek() mustEqual ValueLong(1)
+ s.peek() mustEqual ValueLong(1)
+ s.next() mustEqual ValueLong(1)
+ s.peek() mustEqual ValueLong(2)
+ s.peek() mustEqual ValueLong(2)
+ s.next() mustEqual ValueLong(2)
+ }
+
"handle scalar values" in {
val s = Streamy("true false 123456789 3.1415927 \"hello world\"")
s.readBoolean() must beTrue

0 comments on commit df78b45

Please sign in to comment.
Something went wrong with that request. Please try again.