Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unchecked casts #223

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ object JavaScriptKSYParser {

def yamlJavascriptToScala(src: Any): Any = {
src match {
case array: js.Array[AnyRef] =>
case array: js.Array[_] =>
array.toList.map(yamlJavascriptToScala)
case _: String | _: Int | _: Double | _: Boolean =>
src
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ object JavaKSYParser {

def yamlJavaToScala(src: Any): Any = {
src match {
case jlist: JList[AnyRef] =>
case jlist: JList[_] =>
jlist.asScala.toList.map(yamlJavaToScala)
case jmap: JMap[String, AnyRef] =>
case jmap: JMap[_, _] =>
jmap.asScala.toMap.view.mapValues(yamlJavaToScala).toMap
case _: String =>
src
Expand Down
6 changes: 3 additions & 3 deletions shared/src/main/scala/io/kaitai/struct/JSON.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ object JSON extends CommonLiterals {
case v: Int => v.toString
case v: String => stringToJson(v)
case v: collection.Seq[_] => seqToJson(v)
case v: Map[String, _] => mapToJson(v)
case v: Map[_, _] => mapToJson(v)
}
}

Expand All @@ -37,9 +37,9 @@ object JSON extends CommonLiterals {
def seqToJson(obj: collection.Seq[_]): String =
"[" + obj.map((x) => stringify(x)).mkString(",") + "]"

def mapToJson(obj: Map[String, Any]): String = {
def mapToJson[K, V](obj: Map[K, V]): String = {
val kvs = obj.map { case (k, v) =>
stringToJson(k) + ": " + stringify(v)
stringToJson(k.toString) + ": " + stringify(v)
}
"{" + kvs.mkString(",") + "}"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ object Endianness {
case None => None
case Some("be") => Some(BigEndian)
case Some("le") => Some(LittleEndian)
case Some(srcMap: Map[Any, Any]) =>
case Some(srcMap: Map[_, _]) =>
val endianMap = ParseUtils.asMapStr(srcMap, path)
Some(fromMap(endianMap, path))
case _ =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ object AttrSpec {
DataType.fromYaml(
Some(simpleType), path, metaDef, yamlAttrArgs
)
case switchMap: Map[Any, Any] =>
case switchMap: Map[_, _] =>
val switchMapStr = ParseUtils.anyMapToStrMap(switchMap, path)
parseSwitch(switchMapStr, path ++ List("type"), metaDef, yamlAttrArgs)
case unknown =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object EnumValueSpec {
fromSimpleName(name, path)
case x: Boolean =>
fromSimpleName(x.toString, path)
case srcMap: Map[Any, Any] =>
case srcMap: Map[_, _] =>
fromMap(ParseUtils.anyMapToStrMap(srcMap, path), path)
case _ =>
throw KSYParseError.badType("string or map", src, path)
Expand Down
10 changes: 5 additions & 5 deletions shared/src/main/scala/io/kaitai/struct/format/ParseUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ object ParseUtils {
): List[T] = {
val pathField = path ++ List(field)
src.get(field) match {
case Some(srcList: List[Any]) =>
case Some(srcList: List[_]) =>
srcList.zipWithIndex.map { case (element, idx) =>
convertFunc(element, pathField ++ List(idx.toString))
}
case Some(singleObject: T) =>
List(singleObject)
case Some(singleObject) =>
List(convertFunc(singleObject, pathField))
case None =>
List()
case unknown =>
Expand Down Expand Up @@ -221,14 +221,14 @@ object ParseUtils {
def asMapStrStr(src: Any, path: List[String]): Map[String, String] =
anyMapToStrStrMap(asMap(src, path), path)

def anyMapToStrMap(anyMap: Map[Any, Any], path: List[String]): Map[String, Any] = {
def anyMapToStrMap[V](anyMap: Map[_, V], path: List[String]): Map[String, V] = {
anyMap.map { case (key, value) =>
val keyStr = asStr(key, path)
keyStr -> value
}
}

def anyMapToStrStrMap(anyMap: Map[Any, Any], path: List[String]): Map[String, String] = {
def anyMapToStrStrMap(anyMap: Map[_, _], path: List[String]): Map[String, String] = {
anyMap.map { case (key, value) =>
val keyStr = asStr(key, path)
val valueStr = asStr(value, path ++ List(keyStr))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ object ValidationSpec {
fromString(x.toString, path)
case x: Long =>
fromString(x.toString, path)
case srcMap: Map[Any, Any] =>
case srcMap: Map[_, _] =>
fromMap(ParseUtils.anyMapToStrMap(srcMap, path), path)
case _ =>
throw KSYParseError.badType("string or map", src, path)
Expand Down
Loading