Skip to content

Commit

Permalink
Go: introduce defaultTypeValue to translate type to the default val…
Browse files Browse the repository at this point in the history
…ue of type

It will be used in KST to check that we return default values when error is expected
  • Loading branch information
Mingun committed Sep 15, 2024
1 parent 396d769 commit 032007a
Showing 1 changed file with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -485,12 +485,7 @@ class GoCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
override def instanceHeader(className: List[String], instName: InstanceIdentifier, dataType: DataType, isNullable: Boolean): Unit = {
out.puts(s"func (this *${types2class(className)}) ${publicMemberName(instName)}() (v ${kaitaiType2NativeType(dataType)}, err error) {")
out.inc
translator.returnRes = Some(dataType match {
case _: NumericType => "0"
case _: BooleanType => "false"
case _: StrType => "\"\""
case _ => "nil"
})
translator.returnRes = Some(defaultTypeValue(dataType))
}

override def instanceCalculate(instName: Identifier, dataType: DataType, value: Ast.expr): Unit = {
Expand Down Expand Up @@ -692,6 +687,13 @@ object GoCompiler extends LanguageCompilerStatic
def enumToStr(typeName: List[String], enumName: String): String =
types2class(typeName) + "__" + type2class(enumName)

def defaultTypeValue(dataType: DataType): String = dataType match {
case _: NumericType => "0"
case _: BooleanType => "false"
case _: StrType => "\"\""
case _ => "nil"
}

override def kstreamName: String = "kaitai.Stream"
override def kstructName: String = "kaitai.Struct"
override def ksErrorName(err: KSError): String = err match {
Expand Down

0 comments on commit 032007a

Please sign in to comment.