Doing my first steps in Kaitai Struct, I've been trying to do BSON parser as an excercise. My .ksy code that parses BSON element now looks like that:
  element:
    seq:
      - id: el_type
        type: u1
        enum: bson_type
      - id: el_name
        type: strz
        encoding: UTF-8
        if: el_type != bson_type::end_of_document
      - id: el_string
        type: bson_string
        if: el_type == bson_type::string
      - id: el_document
        type: bson_document
        if: el_type == bson_type::document
      - id: el_boolean
        type: u1
        if: el_type == bson_type::boolean
      - id: el_int32
        type: s4
        if: el_type == bson_type::int32
      - id: el_int64
        type: s4
        if: el_type == bson_type::int64
enums:
  bson_type:
    0: end_of_document
    1: double
    2: string
    3: document
    8: boolean
    0x10: int32
    0x12: int64
As you might have noticed, there's lots of repetition. One just have go duplicate if block every time one wants to do additional element type. Even worse, you basically have to duplicate stuff 3 times in every such field, i.e.:
  - id: el_string                    # <= string!
    type: bson_string                # <= string!
    if: el_type == bson_type::string # <= string!
My target language is Java. Before Kaitai, I've only tried Preon, and there we had clauses such as:
@Choices(prefixSize = 8, alternatives = {
    @Choice(condition = "prefix==0x01", type = FloatNamedElement.class),
    @Choice(condition = "prefix==0x02", type = UTF8NamedElement.class)
}
private NamedElement elements;
There you automatically get these two elements based on value of "prefix". Is it possible to do it in Kaitai?
 
    