Skip to content

Commit

Permalink
Fix unchecked casts
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingun committed Jul 16, 2024
1 parent 54524e1 commit eed22dc
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 17 deletions.
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(obj: Map[_, _]): 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
14 changes: 7 additions & 7 deletions shared/src/main/scala/io/kaitai/struct/format/ParseUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,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 @@ -197,9 +197,9 @@ object ParseUtils {
}
}

def asMap(src: Any, path: List[String]): Map[Any, Any] = {
def asMap(src: Any, path: List[String]): Map[_, _] = {
src match {
case srcMap: Map[Any, Any] =>
case srcMap: Map[_, _] =>
srcMap
case unknown =>
throw KSYParseError.badType("map", unknown, path)
Expand All @@ -212,14 +212,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 @@ -86,7 +86,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

0 comments on commit eed22dc

Please sign in to comment.