Skip to content

Missing imports when type switching on enum field of imported type #1255

@Axelen123

Description

@Axelen123

KSC: 0.11

I have two ksy files:

# common.ksy
meta:
  id: common

types:
  header:
    seq:
      - id: type
        type: u1
        enum: chunk_type
enums:
  chunk_type:
    0: x
    1: y
# my_type.ksy
meta:
  id: my_type
  imports:
    - common
  endian: le

seq:
  - id: header
    type: common::header
  - id: data
    type:
      switch-on: header.type
      cases:
        "common::chunk_type::x": u2
        "common::chunk_type::y": u4

The compiler generates the following java code for my_type.ksy:

// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

import io.kaitai.struct.ByteBufferKaitaiStream;
import io.kaitai.struct.KaitaiStruct;
import io.kaitai.struct.KaitaiStream;
import java.io.IOException;

public class MyType extends KaitaiStruct {
    public static MyType fromFile(String fileName) throws IOException {
        return new MyType(new ByteBufferKaitaiStream(fileName));
    }

    public MyType(KaitaiStream _io) {
        this(_io, null, null);
    }

    public MyType(KaitaiStream _io, KaitaiStruct _parent) {
        this(_io, _parent, null);
    }

    public MyType(KaitaiStream _io, KaitaiStruct _parent, MyType _root) {
        super(_io);
        this._parent = _parent;
        this._root = _root == null ? this : _root;
        _read();
    }
    private void _read() {
        this.header = new Common.Header(this._io);
        {
            ChunkType on = header().type();
            if (on != null) {
                switch (header().type()) {
                case X: {
                    this.data = ((Number) (this._io.readU2le())).longValue();
                    break;
                }
                case Y: {
                    this.data = this._io.readU4le();
                    break;
                }
                }
            }
        }
    }

    public void _fetchInstances() {
        this.header._fetchInstances();
        {
            ChunkType on = header().type();
            if (on != null) {
                switch (header().type()) {
                case X: {
                    break;
                }
                case Y: {
                    break;
                }
                }
            }
        }
    }
    private Common.Header header;
    private Long data;
    private MyType _root;
    private KaitaiStruct _parent;
    public Common.Header header() { return header; }
    public Long data() { return data; }
    public MyType _root() { return _root; }
    public KaitaiStruct _parent() { return _parent; }
}

The type switching code does not correctly reference the imported enum type. This does not seem to happen if the enum is stored in my_type. I don't know if this occurs in other languages.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions