Skip to content

Commit

Permalink
Added "enumInverseMap" following "enumDirectMap".
Browse files Browse the repository at this point in the history
"enumToInt" didn't take the fully qualified path of an ENUM into account when rendering usage of the associated inverse map. If that map needed to be accessed in some external KSY compared to where the map is hosted, things failed because the map was wrongly assumed to be in the KSY where it is used. This commit changes "I__ENUM" to "Class::I__ENUM" following what has been available already as "enumDirectMap".

This fixes kaitai-io/kaitai_struct#552.
  • Loading branch information
ams-tschoening committed Apr 5, 2019
1 parent d0b59a1 commit e05d888
Showing 1 changed file with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ class RubyTranslator(provider: TypeProvider) extends BaseTranslator(provider) {
}
}

def enumInverseMap(et: EnumType): String = {
val enumTypeAndName = et.enumSpec.get.name
val enumDirectMap = this.enumDirectMap(enumTypeAndName)
val enumNameDirect = enumTypeAndName.last.toUpperCase
val enumNameInverse = RubyCompiler.inverseEnumName(enumNameDirect)

enumDirectMap.replace(enumNameDirect, enumNameInverse)
}

override def doSubscript(container: Ast.expr, idx: Ast.expr): String =
s"${translate(container)}[${translate(idx)}]"
override def doIfExp(condition: Ast.expr, ifTrue: Ast.expr, ifFalse: Ast.expr): String =
Expand All @@ -69,7 +78,7 @@ class RubyTranslator(provider: TypeProvider) extends BaseTranslator(provider) {
})
}
override def enumToInt(v: Ast.expr, et: EnumType): String =
s"${RubyCompiler.inverseEnumName(et.name.last.toUpperCase)}[${translate(v)}]"
s"${enumInverseMap(et)}[${translate(v)}]"
override def floatToInt(v: Ast.expr): String =
s"(${translate(v)}).to_i"
override def intToStr(i: Ast.expr, base: Ast.expr): String =
Expand Down

0 comments on commit e05d888

Please sign in to comment.