Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go: introduce special GoOutputWriter and move generation of error check to it #299

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Go: introduce defaultTypeValue to translate type to the default val…
…ue of type

It will be used in KST to check that we return default values when error is expected
Mingun committed Sep 15, 2024
commit 032007ae8fa5872a20e13d17437c492913f0a822
Original file line number Diff line number Diff line change
@@ -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 {