[Java] Fix key lookup returning null clashing with default value (#7236)

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.
This commit is contained in:
Paulo Pinheiro
2022-04-12 02:16:41 +02:00
committed by GitHub
parent 7f663b1204
commit 7181d77700
9 changed files with 186 additions and 11 deletions

View File

@@ -1,6 +1,7 @@
import static com.google.flatbuffers.Constants.*;
import DictionaryLookup.*;
import MyGame.Example.*;
import optional_scalars.ScalarStuff;
import optional_scalars.OptionalByte;
@@ -111,6 +112,8 @@ class JavaTest {
TestPackUnpack(bb);
TestDictionaryLookup();
System.out.println("FlatBuffers test: completed successfully");
}
@@ -1148,6 +1151,25 @@ class JavaTest {
testFlexBuffersMapLookup();
}
static void TestDictionaryLookup() {
FlatBufferBuilder fbb = new FlatBufferBuilder(16);
int lfIndex = LongFloatEntry.createLongFloatEntry(fbb, 0, 99);
int vectorEntriesIdx = LongFloatMap.createEntriesVector(fbb, new int[] { lfIndex });
int rootIdx = LongFloatMap.createLongFloatMap(fbb, vectorEntriesIdx);
LongFloatMap.finishLongFloatMapBuffer(fbb, rootIdx);
LongFloatMap map = LongFloatMap.getRootAsLongFloatMap(fbb.dataBuffer());
TestEq(map.entriesLength(), 1);
LongFloatEntry e = map.entries(0);
TestEq(e.key(), 0L);
TestEq(e.value(), 99.0f);
LongFloatEntry e2 = map.entriesByKey(0);
TestEq(e2.key(), 0L);
TestEq(e2.value(), 99.0f);
}
static void TestVectorOfBytes() {
FlatBufferBuilder fbb = new FlatBufferBuilder(16);
int str = fbb.createString("ByteMonster");