Skip to content

Commit

Permalink
Remove dead codes in TypeCast and related test codes
Browse files Browse the repository at this point in the history
Currently, `TypeCast` is not used anymore. This PR removes the dead codes and related tests in `TypeCastSuite`.

Author: hyukjinkwon <[email protected]>

Closes databricks#144 from HyukjinKwon/dead-code.
  • Loading branch information
HyukjinKwon committed Aug 31, 2016
1 parent c75163e commit beb3584
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 54 deletions.
30 changes: 0 additions & 30 deletions src/main/scala/com/databricks/spark/xml/util/TypeCast.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,44 +59,14 @@ object TypeCast {
.getOrElse(NumberFormat.getInstance(Locale.getDefault).parse(datum).doubleValue())
case _: BooleanType => datum.toBoolean
case _: DecimalType => new BigDecimal(datum.replaceAll(",", ""))
// TODO(hossein): would be good to support other common timestamp formats
case _: TimestampType => Timestamp.valueOf(datum)
// TODO(hossein): would be good to support other common date formats
case _: DateType => Date.valueOf(datum)
case _: StringType => datum
case _ => throw new RuntimeException(s"Unsupported type: ${castType.typeName}")
}
}
}

/**
* Helper method that converts string representation of a character to actual character.
* It handles some Java escaped strings and throws exception if given string is longer than one
* character.
*
*/
@throws[IllegalArgumentException]
private[xml] def toChar(str: String): Char = {
if (str.charAt(0) == '\\') {
str.charAt(1)
match {
case 't' => '\t'
case 'r' => '\r'
case 'b' => '\b'
case 'f' => '\f'
case '\"' => '\"' // In case user changes quote char and uses \" as delimiter in options
case '\'' => '\''
case 'u' if str == """\u0000""" => '\u0000'
case _ =>
throw new IllegalArgumentException(s"Unsupported special character for delimiter: $str")
}
} else if (str.length == 1) {
str.charAt(0)
} else {
throw new IllegalArgumentException(s"Delimiter cannot be more than one character: $str")
}
}

/**
* Helper method that checks and cast string representation of a numeric types.
*/
Expand Down
24 changes: 0 additions & 24 deletions src/test/scala/com/databricks/spark/xml/util/TypeCastSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,6 @@ class TypeCastSuite extends FunSuite {
}
}

test("Can parse escaped characters") {
assert(TypeCast.toChar("""\t""") === '\t')
assert(TypeCast.toChar("""\r""") === '\r')
assert(TypeCast.toChar("""\b""") === '\b')
assert(TypeCast.toChar("""\f""") === '\f')
assert(TypeCast.toChar("""\"""") === '\"')
assert(TypeCast.toChar("""\'""") === '\'')
assert(TypeCast.toChar("""\u0000""") === '\u0000')
}

test("Does not accept delimiter larger than one character") {
val exception = intercept[IllegalArgumentException]{
TypeCast.toChar("ab")
}
assert(exception.getMessage.contains("cannot be more than one character"))
}

test("Throws exception for unsupported escaped characters") {
val exception = intercept[IllegalArgumentException]{
TypeCast.toChar("""\1""")
}
assert(exception.getMessage.contains("Unsupported special character for delimiter"))
}

test("Nullable types are handled") {
assert(TypeCast.castTo("", IntegerType, nullable = true) == null)
}
Expand Down

0 comments on commit beb3584

Please sign in to comment.