From 396d769e6e1dd95c2ba5c21283a5bc311425afdd Mon Sep 17 00:00:00 2001 From: Mingun Date: Sun, 14 Apr 2024 00:47:37 +0500 Subject: [PATCH 1/2] Go: add a couple of words to document some variables and overridable methods --- .../scala/io/kaitai/struct/translators/GoTranslator.scala | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/shared/src/main/scala/io/kaitai/struct/translators/GoTranslator.scala b/shared/src/main/scala/io/kaitai/struct/translators/GoTranslator.scala index 28647aec8..c3b2dbb50 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/GoTranslator.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/GoTranslator.scala @@ -26,6 +26,10 @@ class GoTranslator(out: StringLanguageOutputWriter, provider: TypeProvider, impo import io.kaitai.struct.languages.GoCompiler._ + /** + * Dummy return value that should be returned in case of error just because + * we cannot return nothing. + */ var returnRes: Option[String] = None override def translate(v: Ast.expr, extPrec: Int): String = resToStr(translateExpr(v, extPrec)) @@ -542,6 +546,10 @@ class GoTranslator(out: StringLanguageOutputWriter, provider: TypeProvider, impo def localVarName(n: Int) = s"tmp$n" + /** + * Puts to the output code to check variable `err` for error value and emit + * a premature return with value of error. + */ def outAddErrCheck(): Unit = { out.puts("if err != nil {") out.inc From 032007ae8fa5872a20e13d17437c492913f0a822 Mon Sep 17 00:00:00 2001 From: Mingun Date: Sun, 14 Apr 2024 01:50:41 +0500 Subject: [PATCH 2/2] Go: introduce `defaultTypeValue` to translate type to the default value of type It will be used in KST to check that we return default values when error is expected --- .../io/kaitai/struct/languages/GoCompiler.scala | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/shared/src/main/scala/io/kaitai/struct/languages/GoCompiler.scala b/shared/src/main/scala/io/kaitai/struct/languages/GoCompiler.scala index 49e4343e1..b7d8d6255 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/GoCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/GoCompiler.scala @@ -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 = { @@ -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 {