[Kotlin] Control the generation of reflection with --reflect-names (#7775)

* [Kotlin] Control the generation of reflection with --reflect-names.
Tested:
```
$ cmake -G "Unix Makefiles" && make && ./tests/flatc/main.py
...
KotlinTests.EnumValAttributes
 [PASSED]
KotlinTests.EnumValAttributes_ReflectNames
 [PASSED]
KotlinTests: 2 of 2 passsed
...

35 of 35 tests passed
```

* [Kotlin] Fix SampleBinary by converting Byte to UByte for ubyte fields.

* [Kotlin] Annotate all generated classes with kotlin.ExperimentalUnsignedTypes.
This commit is contained in:
Anton Bobukh
2023-01-10 10:03:39 -08:00
committed by GitHub
parent 7bf83f5ea0
commit b50b6be60a
35 changed files with 74 additions and 6 deletions

View File

@@ -287,6 +287,7 @@ class KotlinGenerator : public BaseGenerator {
GenerateComment(enum_def.doc_comment, writer, &comment_config);
writer += "@Suppress(\"unused\")";
writer += "@kotlin.ExperimentalUnsignedTypes";
writer += "class " + namer_.Type(enum_def) + " private constructor() {";
writer.IncrementIdentLevel();
@@ -313,7 +314,10 @@ class KotlinGenerator : public BaseGenerator {
// Average distance between values above which we consider a table
// "too sparse". Change at will.
static const uint64_t kMaxSparseness = 5;
if (range / static_cast<uint64_t>(enum_def.size()) < kMaxSparseness) {
bool generate_names =
range / static_cast<uint64_t>(enum_def.size()) < kMaxSparseness &&
parser_.opts.mini_reflect == IDLOptions::kTypesAndNames;
if (generate_names) {
GeneratePropertyOneLine(writer, "names", "Array<String>", [&]() {
writer += "arrayOf(\\";
auto val = enum_def.Vals().front();
@@ -489,6 +493,7 @@ class KotlinGenerator : public BaseGenerator {
writer.SetValue("superclass", fixed ? "Struct" : "Table");
writer += "@Suppress(\"unused\")";
writer += "@kotlin.ExperimentalUnsignedTypes";
writer += "class {{struct_name}} : {{superclass}}() {\n";
writer.IncrementIdentLevel();