[Kotlin] Fix key lookup returning null clashing with default value (#7237)

* [Java] Fix key lookup returning null clashing with default value

A field with key attribute must always be written on the message so it
can be looked up by key. There is a edge case where inserting a key
field with same value as default would prevent it to be written on
the message and later cannot be found when searched by key.

* [Kotlin] Fix key lookup returning null clashing with default value

A field with key attribute must always be written on the message so it
can be looked up by key. There is a edge case where inserting a key
field with same value as default would prevent it to be written on
the message and later cannot be found when searched by key.

Co-authored-by: Derek Bailey <derekbailey@google.com>
This commit is contained in:
Paulo Pinheiro
2022-04-12 02:17:19 +02:00
committed by GitHub
parent 7181d77700
commit 7b5fd2bd05
8 changed files with 211 additions and 13 deletions

View File

@@ -14,6 +14,7 @@
* limitations under the License.
*/
import DictionaryLookup.*;
import MyGame.Example.*
import optional_scalars.*
import com.google.flatbuffers.ByteBufferUtil
@@ -79,9 +80,29 @@ class KotlinTest {
TestSharedStringPool()
TestScalarOptional()
TestDictionaryLookup()
println("FlatBuffers test: completed successfully")
}
fun TestDictionaryLookup() {
val fbb = FlatBufferBuilder(16)
val lfIndex = LongFloatEntry.createLongFloatEntry(fbb, 0, 99.0f)
val vectorEntriesIdx = LongFloatMap.createEntriesVector(fbb, intArrayOf(lfIndex))
val rootIdx = LongFloatMap.createLongFloatMap(fbb, vectorEntriesIdx)
LongFloatMap.finishLongFloatMapBuffer(fbb, rootIdx)
val map = LongFloatMap.getRootAsLongFloatMap(fbb.dataBuffer())
assert(map.entriesLength == 1)
val e = map.entries(0)!!
assert(e.key == 0L)
assert(e.value == 99.0f)
val e2 = map.entriesByKey(0)!!
assert(e2.key == 0L)
assert(e2.value == 99.0f)
}
fun TestEnums() {
assert(Color.name(Color.Red.toInt()) == "Red")
assert(Color.name(Color.Blue.toInt()) == "Blue")