Skip to content

Commit ac0dbfd

Browse files
dbaileychessLuckyRu
authored andcommitted
[C++, Java, C#, TypeScript, JavaScript] Skip generation of mutable union types (google#5599)
* Skip generation of mutable union types * Removed C# and Java unit tests that mutated a Union type
1 parent 4bbd26c commit ac0dbfd

17 files changed

+64
-209
lines changed

include/flatbuffers/idl.h

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -338,44 +338,7 @@ struct StructDef : public Definition {
338338
flatbuffers::unique_ptr<std::string> original_location;
339339
};
340340

341-
inline bool IsStruct(const Type &type) {
342-
return type.base_type == BASE_TYPE_STRUCT && type.struct_def->fixed;
343-
}
344-
345-
inline bool IsVector(const Type &type) {
346-
return type.base_type == BASE_TYPE_VECTOR;
347-
}
348-
349-
inline bool IsArray(const Type &type) {
350-
return type.base_type == BASE_TYPE_ARRAY;
351-
}
352-
353-
inline bool IsSeries(const Type &type) {
354-
return IsVector(type) || IsArray(type);
355-
}
356-
357-
inline bool IsEnum(const Type &type) {
358-
return type.enum_def != nullptr && IsInteger(type.base_type);
359-
}
360-
361-
inline size_t InlineSize(const Type &type) {
362-
return IsStruct(type)
363-
? type.struct_def->bytesize
364-
: (IsArray(type)
365-
? InlineSize(type.VectorType()) * type.fixed_length
366-
: SizeOf(type.base_type));
367-
}
368341

369-
inline size_t InlineAlignment(const Type &type) {
370-
if (IsStruct(type)) {
371-
return type.struct_def->minalign;
372-
} else if (IsArray(type)) {
373-
return IsStruct(type.VectorType()) ? type.struct_def->minalign
374-
: SizeOf(type.element);
375-
} else {
376-
return SizeOf(type.base_type);
377-
}
378-
}
379342

380343
struct EnumDef;
381344
struct EnumValBuilder;
@@ -460,6 +423,48 @@ struct EnumDef : public Definition {
460423
SymbolTable<EnumVal> vals;
461424
};
462425

426+
inline bool IsStruct(const Type &type) {
427+
return type.base_type == BASE_TYPE_STRUCT && type.struct_def->fixed;
428+
}
429+
430+
inline bool IsUnion(const Type &type) {
431+
return type.enum_def != nullptr && type.enum_def->is_union;
432+
}
433+
434+
inline bool IsVector(const Type &type) {
435+
return type.base_type == BASE_TYPE_VECTOR;
436+
}
437+
438+
inline bool IsArray(const Type &type) {
439+
return type.base_type == BASE_TYPE_ARRAY;
440+
}
441+
442+
inline bool IsSeries(const Type &type) {
443+
return IsVector(type) || IsArray(type);
444+
}
445+
446+
inline bool IsEnum(const Type &type) {
447+
return type.enum_def != nullptr && IsInteger(type.base_type);
448+
}
449+
450+
inline size_t InlineSize(const Type &type) {
451+
return IsStruct(type)
452+
? type.struct_def->bytesize
453+
: (IsArray(type)
454+
? InlineSize(type.VectorType()) * type.fixed_length
455+
: SizeOf(type.base_type));
456+
}
457+
458+
inline size_t InlineAlignment(const Type &type) {
459+
if (IsStruct(type)) {
460+
return type.struct_def->minalign;
461+
} else if (IsArray(type)) {
462+
return IsStruct(type.VectorType()) ? type.struct_def->minalign
463+
: SizeOf(type.element);
464+
} else {
465+
return SizeOf(type.base_type);
466+
}
467+
}
463468
inline bool operator==(const EnumVal &lhs, const EnumVal &rhs) {
464469
return lhs.value == rhs.value;
465470
}

samples/monster_generated.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,6 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
316316
MyGame::Sample::Equipment equipped_type() const {
317317
return static_cast<MyGame::Sample::Equipment>(GetField<uint8_t>(VT_EQUIPPED_TYPE, 0));
318318
}
319-
bool mutate_equipped_type(MyGame::Sample::Equipment _equipped_type) {
320-
return SetField<uint8_t>(VT_EQUIPPED_TYPE, static_cast<uint8_t>(_equipped_type), 0);
321-
}
322319
const void *equipped() const {
323320
return GetPointer<const void *>(VT_EQUIPPED);
324321
}

src/idl_gen_cpp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1925,7 +1925,7 @@ class CppGenerator : public BaseGenerator {
19251925
}
19261926
}
19271927

1928-
if (parser_.opts.mutable_buffer) {
1928+
if (parser_.opts.mutable_buffer && !(is_scalar && IsUnion(field.value.type))) {
19291929
if (is_scalar) {
19301930
const auto type = GenTypeWire(field.value.type, "", false);
19311931
code_.SetValue("SET_FN", "SetField<" + type + ">");

src/idl_gen_general.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1367,7 +1367,7 @@ class GeneralGenerator : public BaseGenerator {
13671367
? lang_.accessor_prefix + "bb_pos + " +
13681368
NumToString(field.value.offset)
13691369
: "o + " + lang_.accessor_prefix + "bb_pos");
1370-
if (IsScalar(underlying_type.base_type)) {
1370+
if (IsScalar(underlying_type.base_type) && !IsUnion(field.value.type)) {
13711371
code += " public ";
13721372
code += struct_def.fixed ? "void " : lang_.bool_type;
13731373
code += mutator_prefix + MakeCamel(field.name, true);

src/idl_gen_js_ts.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ class JsTsGenerator : public BaseGenerator {
10151015
}
10161016

10171017
// Adds the mutable scalar value to the output
1018-
if (IsScalar(field.value.type.base_type) && parser.opts.mutable_buffer) {
1018+
if (IsScalar(field.value.type.base_type) && parser.opts.mutable_buffer && !IsUnion(field.value.type)) {
10191019
std::string annotations = GenTypeAnnotation(
10201020
kParam, GenTypeName(field.value.type, true), "value");
10211021
GenDocComment(

tests/FlatBuffers.Test/FlatBuffersExampleTests.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,7 @@ private void CanCreateNewFlatBufferFromScratch(bool sizePrefix)
156156
Assert.IsTrue(monster.TestarrayoftablesByKey("Barney") != null);
157157
Assert.IsTrue(monster.TestarrayoftablesByKey("Wilma") != null);
158158

159-
// testType is an existing field and mutating it should succeed
160-
Assert.AreEqual(monster.TestType, Any.Monster);
161-
Assert.AreEqual(monster.MutateTestType(Any.NONE), true);
162-
Assert.AreEqual(monster.TestType, Any.NONE);
163-
Assert.AreEqual(monster.MutateTestType(Any.Monster), true);
159+
// testType is an existing field
164160
Assert.AreEqual(monster.TestType, Any.Monster);
165161

166162
//mutate the inventory vector

tests/JavaTest.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,3 @@
1-
/*
2-
* Copyright 2014 Google Inc. All rights reserved.
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
161

172
import static com.google.flatbuffers.Constants.*;
183

@@ -36,6 +21,24 @@
3621
import java.util.HashMap;
3722
import java.util.Map;
3823

24+
/*
25+
* Copyright 2014 Google Inc. All rights reserved.
26+
*
27+
* Licensed under the Apache License, Version 2.0 (the "License");
28+
* you may not use this file except in compliance with the License.
29+
* You may obtain a copy of the License at
30+
*
31+
* http://www.apache.org/licenses/LICENSE-2.0
32+
*
33+
* Unless required by applicable law or agreed to in writing, software
34+
* distributed under the License is distributed on an "AS IS" BASIS,
35+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36+
* See the License for the specific language governing permissions and
37+
* limitations under the License.
38+
*/
39+
40+
41+
3942
class JavaTest {
4043
public static void main(String[] args) {
4144

@@ -438,10 +441,6 @@ static void TestBuilderBasics(FlatBufferBuilder fbb, boolean sizePrefix) {
438441

439442
// testType is an existing field and mutating it should succeed
440443
TestEq(monster.testType(), (byte)Any.Monster);
441-
TestEq(monster.mutateTestType(Any.NONE), true);
442-
TestEq(monster.testType(), (byte)Any.NONE);
443-
TestEq(monster.mutateTestType(Any.Monster), true);
444-
TestEq(monster.testType(), (byte)Any.Monster);
445444

446445
//mutate the inventory vector
447446
TestEq(monster.mutateInventory(0, 1), true);

tests/MyGame/Example/Monster.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ public struct Monster : IFlatbufferObject
4444
public MyGame.Example.Color Color { get { int o = __p.__offset(16); return o != 0 ? (MyGame.Example.Color)__p.bb.Get(o + __p.bb_pos) : MyGame.Example.Color.Blue; } }
4545
public bool MutateColor(MyGame.Example.Color color) { int o = __p.__offset(16); if (o != 0) { __p.bb.Put(o + __p.bb_pos, (byte)color); return true; } else { return false; } }
4646
public MyGame.Example.Any TestType { get { int o = __p.__offset(18); return o != 0 ? (MyGame.Example.Any)__p.bb.Get(o + __p.bb_pos) : MyGame.Example.Any.NONE; } }
47-
public bool MutateTestType(MyGame.Example.Any test_type) { int o = __p.__offset(18); if (o != 0) { __p.bb.Put(o + __p.bb_pos, (byte)test_type); return true; } else { return false; } }
4847
public TTable? Test<TTable>() where TTable : struct, IFlatbufferObject { int o = __p.__offset(20); return o != 0 ? (TTable?)__p.__union<TTable>(o + __p.bb_pos) : null; }
4948
public MyGame.Example.Test? Test4(int j) { int o = __p.__offset(22); return o != 0 ? (MyGame.Example.Test?)(new MyGame.Example.Test()).__assign(__p.__vector(o) + j * 4, __p.bb) : null; }
5049
public int Test4Length { get { int o = __p.__offset(22); return o != 0 ? __p.__vector_len(o) : 0; } }
@@ -174,10 +173,8 @@ public struct Monster : IFlatbufferObject
174173
public ulong[] GetVectorOfNonOwningReferencesArray() { return __p.__vector_as_array<ulong>(88); }
175174
public bool MutateVectorOfNonOwningReferences(int j, ulong vector_of_non_owning_references) { int o = __p.__offset(88); if (o != 0) { __p.bb.PutUlong(__p.__vector(o) + j * 8, vector_of_non_owning_references); return true; } else { return false; } }
176175
public MyGame.Example.AnyUniqueAliases AnyUniqueType { get { int o = __p.__offset(90); return o != 0 ? (MyGame.Example.AnyUniqueAliases)__p.bb.Get(o + __p.bb_pos) : MyGame.Example.AnyUniqueAliases.NONE; } }
177-
public bool MutateAnyUniqueType(MyGame.Example.AnyUniqueAliases any_unique_type) { int o = __p.__offset(90); if (o != 0) { __p.bb.Put(o + __p.bb_pos, (byte)any_unique_type); return true; } else { return false; } }
178176
public TTable? AnyUnique<TTable>() where TTable : struct, IFlatbufferObject { int o = __p.__offset(92); return o != 0 ? (TTable?)__p.__union<TTable>(o + __p.bb_pos) : null; }
179177
public MyGame.Example.AnyAmbiguousAliases AnyAmbiguousType { get { int o = __p.__offset(94); return o != 0 ? (MyGame.Example.AnyAmbiguousAliases)__p.bb.Get(o + __p.bb_pos) : MyGame.Example.AnyAmbiguousAliases.NONE; } }
180-
public bool MutateAnyAmbiguousType(MyGame.Example.AnyAmbiguousAliases any_ambiguous_type) { int o = __p.__offset(94); if (o != 0) { __p.bb.Put(o + __p.bb_pos, (byte)any_ambiguous_type); return true; } else { return false; } }
181178
public TTable? AnyAmbiguous<TTable>() where TTable : struct, IFlatbufferObject { int o = __p.__offset(96); return o != 0 ? (TTable?)__p.__union<TTable>(o + __p.bb_pos) : null; }
182179
public MyGame.Example.Color VectorOfEnums(int j) { int o = __p.__offset(98); return o != 0 ? (MyGame.Example.Color)__p.bb.Get(__p.__vector(o) + j * 1) : (MyGame.Example.Color)0; }
183180
public int VectorOfEnumsLength { get { int o = __p.__offset(98); return o != 0 ? __p.__vector_len(o) : 0; } }

tests/MyGame/Example/Monster.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public final class Monster extends Table {
3838
public int color() { int o = __offset(16); return o != 0 ? bb.get(o + bb_pos) & 0xFF : 8; }
3939
public boolean mutateColor(int color) { int o = __offset(16); if (o != 0) { bb.put(o + bb_pos, (byte)color); return true; } else { return false; } }
4040
public byte testType() { int o = __offset(18); return o != 0 ? bb.get(o + bb_pos) : 0; }
41-
public boolean mutateTestType(byte test_type) { int o = __offset(18); if (o != 0) { bb.put(o + bb_pos, test_type); return true; } else { return false; } }
4241
public Table test(Table obj) { int o = __offset(20); return o != 0 ? __union(obj, o + bb_pos) : null; }
4342
public MyGame.Example.Test test4(int j) { return test4(new MyGame.Example.Test(), j); }
4443
public MyGame.Example.Test test4(MyGame.Example.Test obj, int j) { int o = __offset(22); return o != 0 ? obj.__assign(__vector(o) + j * 4, bb) : null; }
@@ -183,10 +182,8 @@ public final class Monster extends Table {
183182
public ByteBuffer vectorOfNonOwningReferencesInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 88, 8); }
184183
public boolean mutateVectorOfNonOwningReferences(int j, long vector_of_non_owning_references) { int o = __offset(88); if (o != 0) { bb.putLong(__vector(o) + j * 8, vector_of_non_owning_references); return true; } else { return false; } }
185184
public byte anyUniqueType() { int o = __offset(90); return o != 0 ? bb.get(o + bb_pos) : 0; }
186-
public boolean mutateAnyUniqueType(byte any_unique_type) { int o = __offset(90); if (o != 0) { bb.put(o + bb_pos, any_unique_type); return true; } else { return false; } }
187185
public Table anyUnique(Table obj) { int o = __offset(92); return o != 0 ? __union(obj, o + bb_pos) : null; }
188186
public byte anyAmbiguousType() { int o = __offset(94); return o != 0 ? bb.get(o + bb_pos) : 0; }
189-
public boolean mutateAnyAmbiguousType(byte any_ambiguous_type) { int o = __offset(94); if (o != 0) { bb.put(o + bb_pos, any_ambiguous_type); return true; } else { return false; } }
190187
public Table anyAmbiguous(Table obj) { int o = __offset(96); return o != 0 ? __union(obj, o + bb_pos) : null; }
191188
public int vectorOfEnums(int j) { int o = __offset(98); return o != 0 ? bb.get(__vector(o) + j * 1) & 0xFF : 0; }
192189
public int vectorOfEnumsLength() { int o = __offset(98); return o != 0 ? __vector_len(o) : 0; }

tests/monster_test_generated.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,9 +1353,6 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
13531353
MyGame::Example::Any test_type() const {
13541354
return static_cast<MyGame::Example::Any>(GetField<uint8_t>(VT_TEST_TYPE, 0));
13551355
}
1356-
bool mutate_test_type(MyGame::Example::Any _test_type) {
1357-
return SetField<uint8_t>(VT_TEST_TYPE, static_cast<uint8_t>(_test_type), 0);
1358-
}
13591356
const void *test() const {
13601357
return GetPointer<const void *>(VT_TEST);
13611358
}
@@ -1587,9 +1584,6 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
15871584
MyGame::Example::AnyUniqueAliases any_unique_type() const {
15881585
return static_cast<MyGame::Example::AnyUniqueAliases>(GetField<uint8_t>(VT_ANY_UNIQUE_TYPE, 0));
15891586
}
1590-
bool mutate_any_unique_type(MyGame::Example::AnyUniqueAliases _any_unique_type) {
1591-
return SetField<uint8_t>(VT_ANY_UNIQUE_TYPE, static_cast<uint8_t>(_any_unique_type), 0);
1592-
}
15931587
const void *any_unique() const {
15941588
return GetPointer<const void *>(VT_ANY_UNIQUE);
15951589
}
@@ -1609,9 +1603,6 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
16091603
MyGame::Example::AnyAmbiguousAliases any_ambiguous_type() const {
16101604
return static_cast<MyGame::Example::AnyAmbiguousAliases>(GetField<uint8_t>(VT_ANY_AMBIGUOUS_TYPE, 0));
16111605
}
1612-
bool mutate_any_ambiguous_type(MyGame::Example::AnyAmbiguousAliases _any_ambiguous_type) {
1613-
return SetField<uint8_t>(VT_ANY_AMBIGUOUS_TYPE, static_cast<uint8_t>(_any_ambiguous_type), 0);
1614-
}
16151606
const void *any_ambiguous() const {
16161607
return GetPointer<const void *>(VT_ANY_AMBIGUOUS);
16171608
}

0 commit comments

Comments
 (0)