-
Notifications
You must be signed in to change notification settings - Fork 200
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
How to dynamically switch type based on a condition? #584
Comments
Please NEVER post source code as screenshots 👿👿👿👿.
What is your code? Can Also, as you have the same selection of the type in each field, it is obvious you should encapsulate it into a type. You may also want to subscribe to #88 . |
type:
switch-on: _unplayed
cases:
_parent.osu_ver < 20140609: u1
_: f4 I remembered its something like this.
I don't really understand it. I just begin using kaitai last few hours ago |
oh and also I think i tried using |
@takase1121 I would suggest writing the condition like this: type:
switch-on: _parent.osu_ver < 20140609
cases:
true: u1
false: f4 That way you don't have to use another unrelated field in the condition. I think @KOLANICH's suggestion is that you should define the seq:
- id: cs
type: version_dependent_number
- id: hpd
type: version_dependent_number
- id: od
type: version_dependent_number
types:
version_dependent_number:
seq:
- id: value
type:
switch-on: _parent.osu_ver < 20140609
cases:
true: u1
false: f4 |
As I understand, you are implementing https://osu.ppy.sh/help/wiki/osu!_File_Formats/Db_(file_format) So seq:
- id: osu_ver
type: u4
....
instances:
float_or_int_selector:
value: osu_ver < 20140609
types:
float_or_int:
seq:
- id: value
type:
switch-on: _root.float_or_int_selector
cases:
true: u1
false: f4 |
Oh my. I must be a mega idiot to not realize I can do that |
Yes |
@dgelessus I tried your method and this happens when I run the script through
|
@KOLANICH I have the |
I don't understand the question. It is a module you can use in an own program. |
@KOLANICH sorry i wasnt clear and another file called |
1 clone https://github.com/kaitai-io/kaitai_struct_formats (we abbreviate it as |
should i fork? |
yes |
If i understood what you said correctly, this should be it |
Yeah, let[s have the further discussion in that PR. |
I think this structure will not work here. |
@dgelessus |
Parametric types might be a solution as well, those are designed to forward types to types and circumvent the concepts of implicit |
This will not work as kaitai parses that as the
type
without checking it. Case does not work (at least in my case) when used to compare values. What else can I do to achieve the same effect?The text was updated successfully, but these errors were encountered: