Skip to content

Commit

Permalink
Prohibit duplicate enum member IDs (names)
Browse files Browse the repository at this point in the history
  • Loading branch information
generalmimon committed Jun 26, 2022
1 parent 0a3dcbf commit 1cbaff9
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions shared/src/main/scala/io/kaitai/struct/format/EnumSpec.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package io.kaitai.struct.format

import io.kaitai.struct.problems.KSYParseError

import scala.collection.mutable

case class EnumSpec(map: Map[Long, EnumValueSpec]) {
var name = List[String]()

Expand All @@ -19,10 +23,18 @@ case class EnumSpec(map: Map[Long, EnumValueSpec]) {
object EnumSpec {
def fromYaml(src: Any, path: List[String]): EnumSpec = {
val srcMap = ParseUtils.asMap(src, path)
val memberNameMap = mutable.Map[String, Long]()
EnumSpec(srcMap.map { case (id, desc) =>
val idLong = ParseUtils.asLong(id, path)
val value = EnumValueSpec.fromYaml(desc, path ++ List(idLong.toString))

memberNameMap.get(value.name).foreach { (prevIdLong) =>
throw KSYParseError.withText(
s"duplicate enum member ID: '${value.name}', previously defined at /${(path ++ List(prevIdLong.toString)).mkString("/")}",
path ++ List(idLong.toString)
)
}
memberNameMap.put(value.name, idLong)
idLong -> value
})
}
Expand Down

0 comments on commit 1cbaff9

Please sign in to comment.