summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFlorian Wagner <f_wagner@me.com>2023-01-06 05:16:31 +0100
committerGitHub <noreply@github.com>2023-01-06 04:16:31 +0000
commite61b00359b1809bfdb1b217048318745a091456f (patch)
treeeac4eb5f5f1de9bb936759225264ea0f4d21193a /src
parent74b51950894ee361e456fcd2b8b411fe2bb75b08 (diff)
downloadflatbuffers-e61b00359b1809bfdb1b217048318745a091456f.tar.gz
flatbuffers-e61b00359b1809bfdb1b217048318745a091456f.tar.bz2
flatbuffers-e61b00359b1809bfdb1b217048318745a091456f.zip
[Kotlin] Improve field nullability based on (required) (#7658)
* [Kotlin] Only generate nullable return types if the field is not required * [Kotlin] Fix generated code formatting according to kotlin style guide Co-authored-by: Derek Bailey <derekbailey@google.com> Co-authored-by: Paulo Pinheiro <paulovictor.pinheiro@gmail.com>
Diffstat (limited to 'src')
-rw-r--r--src/idl_gen_kotlin.cpp45
1 files changed, 38 insertions, 7 deletions
diff --git a/src/idl_gen_kotlin.cpp b/src/idl_gen_kotlin.cpp
index 58c7b17f..b19f2a3d 100644
--- a/src/idl_gen_kotlin.cpp
+++ b/src/idl_gen_kotlin.cpp
@@ -191,10 +191,11 @@ class KotlinGenerator : public BaseGenerator {
auto r_type = GenTypeGet(field.value.type);
if (field.IsScalarOptional() ||
// string, structs and unions
- (base_type == BASE_TYPE_STRING || base_type == BASE_TYPE_STRUCT ||
- base_type == BASE_TYPE_UNION) ||
+ (!field.IsRequired() &&
+ (base_type == BASE_TYPE_STRING || base_type == BASE_TYPE_STRUCT ||
+ base_type == BASE_TYPE_UNION)) ||
// vector of anything not scalar
- (base_type == BASE_TYPE_VECTOR &&
+ (base_type == BASE_TYPE_VECTOR && !field.IsRequired() &&
!IsScalar(field.value.type.VectorType().base_type))) {
r_type += "?";
}
@@ -998,7 +999,15 @@ class KotlinGenerator : public BaseGenerator {
OffsetWrapper(
writer, offset_val,
[&]() { writer += "obj.__assign({{seek}}, bb)"; },
- [&]() { writer += "null"; });
+ [&]() {
+ if (field.IsRequired()) {
+ writer +=
+ "throw AssertionError(\"No value for "
+ "(required) field {{field_name}}\")";
+ } else {
+ writer += "null";
+ }
+ });
});
}
break;
@@ -1008,12 +1017,30 @@ class KotlinGenerator : public BaseGenerator {
// val Name : String?
// get() = {
// val o = __offset(10)
- // return if (o != 0) __string(o + bb_pos) else null
+ // return if (o != 0) {
+ // __string(o + bb_pos)
+ // } else {
+ // null
+ // }
// }
// ? adds nullability annotation
GenerateGetter(writer, field_name, return_type, [&]() {
writer += "val o = __offset({{offset}})";
- writer += "return if (o != 0) __string(o + bb_pos) else null";
+ writer += "return if (o != 0) {";
+ writer.IncrementIdentLevel();
+ writer += "__string(o + bb_pos)";
+ writer.DecrementIdentLevel();
+ writer += "} else {";
+ writer.IncrementIdentLevel();
+ if (field.IsRequired()) {
+ writer +=
+ "throw AssertionError(\"No value for (required) field "
+ "{{field_name}}\")";
+ } else {
+ writer += "null";
+ }
+ writer.DecrementIdentLevel();
+ writer += "}";
});
break;
case BASE_TYPE_VECTOR: {
@@ -1038,7 +1065,11 @@ class KotlinGenerator : public BaseGenerator {
GenerateFun(writer, field_name, params, return_type, [&]() {
auto inline_size = NumToString(InlineSize(vectortype));
auto index = "__vector(o) + j * " + inline_size;
- auto not_found = NotFoundReturn(field.value.type.element);
+ auto not_found =
+ field.IsRequired()
+ ? "throw IndexOutOfBoundsException(\"Index out of range: "
+ "$j, vector {{field_name}} is empty\")"
+ : NotFoundReturn(field.value.type.element);
auto found = "";
writer.SetValue("index", index);
switch (vectortype.base_type) {