mirror of
https://github.com/google/flatbuffers.git
synced 2026-07-02 16:28:19 +00:00
[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>
This commit is contained in:
@@ -191,10 +191,11 @@ class KotlinGenerator : public BaseGenerator {
|
|||||||
auto r_type = GenTypeGet(field.value.type);
|
auto r_type = GenTypeGet(field.value.type);
|
||||||
if (field.IsScalarOptional() ||
|
if (field.IsScalarOptional() ||
|
||||||
// string, structs and unions
|
// string, structs and unions
|
||||||
(base_type == BASE_TYPE_STRING || base_type == BASE_TYPE_STRUCT ||
|
(!field.IsRequired() &&
|
||||||
base_type == BASE_TYPE_UNION) ||
|
(base_type == BASE_TYPE_STRING || base_type == BASE_TYPE_STRUCT ||
|
||||||
|
base_type == BASE_TYPE_UNION)) ||
|
||||||
// vector of anything not scalar
|
// vector of anything not scalar
|
||||||
(base_type == BASE_TYPE_VECTOR &&
|
(base_type == BASE_TYPE_VECTOR && !field.IsRequired() &&
|
||||||
!IsScalar(field.value.type.VectorType().base_type))) {
|
!IsScalar(field.value.type.VectorType().base_type))) {
|
||||||
r_type += "?";
|
r_type += "?";
|
||||||
}
|
}
|
||||||
@@ -998,7 +999,15 @@ class KotlinGenerator : public BaseGenerator {
|
|||||||
OffsetWrapper(
|
OffsetWrapper(
|
||||||
writer, offset_val,
|
writer, offset_val,
|
||||||
[&]() { writer += "obj.__assign({{seek}}, bb)"; },
|
[&]() { writer += "obj.__assign({{seek}}, bb)"; },
|
||||||
[&]() { writer += "null"; });
|
[&]() {
|
||||||
|
if (field.IsRequired()) {
|
||||||
|
writer +=
|
||||||
|
"throw AssertionError(\"No value for "
|
||||||
|
"(required) field {{field_name}}\")";
|
||||||
|
} else {
|
||||||
|
writer += "null";
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -1008,12 +1017,30 @@ class KotlinGenerator : public BaseGenerator {
|
|||||||
// val Name : String?
|
// val Name : String?
|
||||||
// get() = {
|
// get() = {
|
||||||
// val o = __offset(10)
|
// 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
|
// ? adds nullability annotation
|
||||||
GenerateGetter(writer, field_name, return_type, [&]() {
|
GenerateGetter(writer, field_name, return_type, [&]() {
|
||||||
writer += "val o = __offset({{offset}})";
|
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;
|
break;
|
||||||
case BASE_TYPE_VECTOR: {
|
case BASE_TYPE_VECTOR: {
|
||||||
@@ -1038,7 +1065,11 @@ class KotlinGenerator : public BaseGenerator {
|
|||||||
GenerateFun(writer, field_name, params, return_type, [&]() {
|
GenerateFun(writer, field_name, params, return_type, [&]() {
|
||||||
auto inline_size = NumToString(InlineSize(vectortype));
|
auto inline_size = NumToString(InlineSize(vectortype));
|
||||||
auto index = "__vector(o) + j * " + inline_size;
|
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 = "";
|
auto found = "";
|
||||||
writer.SetValue("index", index);
|
writer.SetValue("index", index);
|
||||||
switch (vectortype.base_type) {
|
switch (vectortype.base_type) {
|
||||||
|
|||||||
@@ -68,10 +68,14 @@ class Monster : Table() {
|
|||||||
false
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
val name : String?
|
val name : String
|
||||||
get() {
|
get() {
|
||||||
val o = __offset(10)
|
val o = __offset(10)
|
||||||
return if (o != 0) __string(o + bb_pos) else null
|
return if (o != 0) {
|
||||||
|
__string(o + bb_pos)
|
||||||
|
} else {
|
||||||
|
throw AssertionError("No value for (required) field name")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
val nameAsByteBuffer : ByteBuffer get() = __vector_as_bytebuffer(10, 1)
|
val nameAsByteBuffer : ByteBuffer get() = __vector_as_bytebuffer(10, 1)
|
||||||
fun nameInByteBuffer(_bb: ByteBuffer) : ByteBuffer = __vector_in_bytebuffer(_bb, 10, 1)
|
fun nameInByteBuffer(_bb: ByteBuffer) : ByteBuffer = __vector_in_bytebuffer(_bb, 10, 1)
|
||||||
|
|||||||
@@ -31,7 +31,11 @@ class Stat : Table() {
|
|||||||
val id : String?
|
val id : String?
|
||||||
get() {
|
get() {
|
||||||
val o = __offset(4)
|
val o = __offset(4)
|
||||||
return if (o != 0) __string(o + bb_pos) else null
|
return if (o != 0) {
|
||||||
|
__string(o + bb_pos)
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
val idAsByteBuffer : ByteBuffer get() = __vector_as_bytebuffer(4, 1)
|
val idAsByteBuffer : ByteBuffer get() = __vector_as_bytebuffer(4, 1)
|
||||||
fun idInByteBuffer(_bb: ByteBuffer) : ByteBuffer = __vector_in_bytebuffer(_bb, 4, 1)
|
fun idInByteBuffer(_bb: ByteBuffer) : ByteBuffer = __vector_in_bytebuffer(_bb, 4, 1)
|
||||||
|
|||||||
Reference in New Issue
Block a user