Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Regex support in valid key #200
base: master
Are you sure you want to change the base?
Regex support in valid key #200
Changes from 1 commit
7120c8e
78bc787
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For equality checks (and storing original value in question for C++), we need to know original value data type. But regular expression matches would likely only work for strings. What's the point in passing
_dt: DataType
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: unnecessary double newline.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: unnecessary newline
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this fail if it will be used twice in the same function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And I think it's not wise to ignore the error in case it occurs (doing
matched, _ :=
). Please follow the same approach used in the rest of theGoTranslator
, notice that the methods you added are ones of the few in the file returningString
and notTranslatorResult
. There is as special methodoutVarCheckRes
handling the errors returned from functions, I suggest using that one.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to modify the translateExpr func in GoTranslator, but it seems to me that this function is not executed (I use some logs), could you explain it to me ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it probably will, because the variables can't be redeclared in the same scope in Go unless some new variables are introduced. I've made a simple test: https://repl.it/repls/WonderfulJointProcedure
@jocelynke Go language didn't play well with the classic
BaseTranslator
model that returnsString
s, because of the error handling in Go. Go doesn't havetry/catch
as other languages, it treats errors as values. This means that if you want the error from a function you call to bubble up in the call stack, it won't happen automatically. You have to check if theerror
value returned from the function isnil
and if it's not, then an error occured and you shouldreturn
it to the caller. (Just returning it isn't exactly debug-friendly, because the caller loses any context which function in the stack actually caused the error, but it's unfortunately done this way in KS currently.)This means that parsing a single
u1
field from the stream can't be done in a single expression (because it can of course result in an EOF error), but instead it is a 3-phase process:Because of this,
GoTranslator
can't just extendBaseTranslator
as other languages do, because the methods inBaseTranslator
have return typeString
which doesn't fit for Go (and it can't be changed in the derived classes). Not all methods inGoTranslator
can be implemented in a single expression which can be passed asString
, for these the return value of the called error-returning method is saved to a temporary variable (namedtmp1
,tmp2
, ... to avoid name conflicts), the error is handled and only the "serial number" of the temp variable is returned. This is done by the methodoutVarCheckRes
.Hence most methods in
GoTranslator
returnTranslatorResult
, which is defined on the top:kaitai_struct_compiler/shared/src/main/scala/io/kaitai/struct/translators/GoTranslator.scala
Lines 11 to 13 in ef7178c
And now how you should do it. I think the
doRegexMatchOp
here could look like this:Then you'll have to move the abstract definition of this method from
CommonOps
toBaseTranslator
:kaitai_struct_compiler/shared/src/main/scala/io/kaitai/struct/translators/CommonOps.scala
Line 68 in 7120c8e
The reason is that
CommonOps
is inherited byGoTranslator
too (whereasBaseTranslator
is not), but inGoTranslator
you need to specify a different return type, which won't work in Scala. And theAst.expr.RegexMatch
handling inBaseTranslator
kaitai_struct_compiler/shared/src/main/scala/io/kaitai/struct/translators/BaseTranslator.scala
Lines 105 to 112 in 7120c8e
needs to be in
GoTranslator
as well, because again, it doesn't extendBaseTranslator
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for this deep explanation, I had implemented what you told me, but the thing is I have no log of a call to translateExpr in the GoTranslator. The consequence is that no code is generated to validate the field that needs it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@generalmimon I found that the valid key is not supported by Go in kaitai, so it's normal that no code is generated...