Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 6 additions & 1 deletion src/idl_gen_kotlin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1138,8 +1138,13 @@ class KotlinGenerator : public BaseGenerator {
case BASE_TYPE_UNION:
GenerateFun(
writer, field_name, "obj: " + field_type, return_type, [&]() {
const auto not_found =
field.IsRequired()
? "throw AssertionError(\"No value for (required) "
"field {{field_name}}\")"
: "null";
writer += OffsetWrapperOneLine(
offset_val, bbgetter + "(obj, o + bb_pos)", "null");
offset_val, bbgetter + "(obj, o + bb_pos)", not_found);
});
break;
default:
Expand Down
14 changes: 14 additions & 0 deletions tests/flatc/flatc_kotlin_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@

class KotlinTests:

def RequiredUnion(self):
flatc(["--kotlin", "required_union.fbs"])

assert_file_and_contents(
"RequiredUnion.kt",
[
"fun payload(obj: Table) : Table {",
"else throw AssertionError(\"No value for (required) field "
"payload\")",
],
)
assert_file_exists("Payload.kt").unlink()
assert_file_exists("PayloadUnion.kt").unlink()

def EnumValAttributes(self):
flatc(["--kotlin", "enum_val_attributes.fbs"])

Expand Down
11 changes: 11 additions & 0 deletions tests/flatc/required_union.fbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
table Payload {}

union PayloadUnion {
Payload,
}

table RequiredUnion {
payload:PayloadUnion (required);
}

root_type RequiredUnion;