mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-01 19:58:15 +00:00
[CS] Naming collision if field has same name as table and used as key (#7842)
* Fix C/C++ Create<Type>Direct with sorted vectors If a struct has a key the vector has to be sorted. To sort the vector you can't use "const". * Changes due to code review * Improve code readability * Add generate of JSON schema to string to lib * option indent_step is supported * Remove unused variables * Fix break in test * Fix style to be consistent with rest of the code * [TS] Fix reserved words as arguments (#6955) * [TS] Fix generation of reserved words in object api (#7106) * [TS] Fix generation of object api * [TS] Fix MakeCamel -> ConvertCase * [C#] Fix collision of field name and type name * [TS] Add test for struct of struct of struct * Update generated files * Add missing files * [TS] Fix query of null/undefined fields in object api * Fix collision if field name is equal to table name and used as key in an array --------- Co-authored-by: Derek Bailey <derekbailey@google.com>
This commit is contained in:
@@ -477,8 +477,9 @@ class CSharpGenerator : public BaseGenerator {
|
||||
const std::string &offset) const {
|
||||
// Use the generated type directly, to properly handle default values that
|
||||
// might not be written to the buffer.
|
||||
return GetObjectConstructor(struct_def, data_buffer, offset) + "." +
|
||||
Name(*key_field);
|
||||
auto name = Name(*key_field);
|
||||
if (name == struct_def.name) { name += "_"; }
|
||||
return GetObjectConstructor(struct_def, data_buffer, offset) + "." + name;
|
||||
}
|
||||
|
||||
// Direct mutation is only allowed for scalar fields.
|
||||
@@ -1303,6 +1304,8 @@ class CSharpGenerator : public BaseGenerator {
|
||||
// because `key_field` is not set for struct
|
||||
if (struct_def.has_key && !struct_def.fixed) {
|
||||
FLATBUFFERS_ASSERT(key_field);
|
||||
auto name = Name(*key_field);
|
||||
if (name == struct_def.name) { name += "_"; }
|
||||
code += "\n public static VectorOffset ";
|
||||
code += "CreateSortedVectorOf" + struct_def.name;
|
||||
code += "(FlatBufferBuilder builder, ";
|
||||
@@ -1332,8 +1335,7 @@ class CSharpGenerator : public BaseGenerator {
|
||||
"(start + middle), bb);\n";
|
||||
|
||||
code += " obj_.__assign(tableOffset, bb);\n";
|
||||
code +=
|
||||
" int comp = obj_." + Name(*key_field) + ".CompareTo(key);\n";
|
||||
code += " int comp = obj_." + name + ".CompareTo(key);\n";
|
||||
code += " if (comp > 0) {\n";
|
||||
code += " span = middle;\n";
|
||||
code += " } else if (comp < 0) {\n";
|
||||
|
||||
@@ -3,6 +3,10 @@ namespace union_value_collsion;
|
||||
table IntValue {
|
||||
value:int;
|
||||
}
|
||||
table Collide {
|
||||
collide: string (key);
|
||||
value: string;
|
||||
}
|
||||
|
||||
union Value { IntValue }
|
||||
|
||||
@@ -12,6 +16,7 @@ union Other { IntValue }
|
||||
table Collision {
|
||||
some_value : Value;
|
||||
value : Other;
|
||||
collide : [Collision];
|
||||
}
|
||||
|
||||
root_type Collision;
|
||||
@@ -198,6 +198,110 @@ public class IntValueT
|
||||
}
|
||||
}
|
||||
|
||||
public struct Collide : IFlatbufferObject
|
||||
{
|
||||
private Table __p;
|
||||
public ByteBuffer ByteBuffer { get { return __p.bb; } }
|
||||
public static void ValidateVersion() { FlatBufferConstants.FLATBUFFERS_23_1_21(); }
|
||||
public static Collide GetRootAsCollide(ByteBuffer _bb) { return GetRootAsCollide(_bb, new Collide()); }
|
||||
public static Collide GetRootAsCollide(ByteBuffer _bb, Collide obj) { return (obj.__assign(_bb.GetInt(_bb.Position) + _bb.Position, _bb)); }
|
||||
public void __init(int _i, ByteBuffer _bb) { __p = new Table(_i, _bb); }
|
||||
public Collide __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
|
||||
|
||||
public string Collide_ { get { int o = __p.__offset(4); return o != 0 ? __p.__string(o + __p.bb_pos) : null; } }
|
||||
#if ENABLE_SPAN_T
|
||||
public Span<byte> GetCollideBytes() { return __p.__vector_as_span<byte>(4, 1); }
|
||||
#else
|
||||
public ArraySegment<byte>? GetCollideBytes() { return __p.__vector_as_arraysegment(4); }
|
||||
#endif
|
||||
public byte[] GetCollideArray() { return __p.__vector_as_array<byte>(4); }
|
||||
public string Value { get { int o = __p.__offset(6); return o != 0 ? __p.__string(o + __p.bb_pos) : null; } }
|
||||
#if ENABLE_SPAN_T
|
||||
public Span<byte> GetValueBytes() { return __p.__vector_as_span<byte>(6, 1); }
|
||||
#else
|
||||
public ArraySegment<byte>? GetValueBytes() { return __p.__vector_as_arraysegment(6); }
|
||||
#endif
|
||||
public byte[] GetValueArray() { return __p.__vector_as_array<byte>(6); }
|
||||
|
||||
public static Offset<union_value_collsion.Collide> CreateCollide(FlatBufferBuilder builder,
|
||||
StringOffset collideOffset = default(StringOffset),
|
||||
StringOffset valueOffset = default(StringOffset)) {
|
||||
builder.StartTable(2);
|
||||
Collide.AddValue(builder, valueOffset);
|
||||
Collide.AddCollide(builder, collideOffset);
|
||||
return Collide.EndCollide(builder);
|
||||
}
|
||||
|
||||
public static void StartCollide(FlatBufferBuilder builder) { builder.StartTable(2); }
|
||||
public static void AddCollide(FlatBufferBuilder builder, StringOffset collideOffset) { builder.AddOffset(0, collideOffset.Value, 0); }
|
||||
public static void AddValue(FlatBufferBuilder builder, StringOffset valueOffset) { builder.AddOffset(1, valueOffset.Value, 0); }
|
||||
public static Offset<union_value_collsion.Collide> EndCollide(FlatBufferBuilder builder) {
|
||||
int o = builder.EndTable();
|
||||
builder.Required(o, 4); // collide
|
||||
return new Offset<union_value_collsion.Collide>(o);
|
||||
}
|
||||
|
||||
public static VectorOffset CreateSortedVectorOfCollide(FlatBufferBuilder builder, Offset<Collide>[] offsets) {
|
||||
Array.Sort(offsets,
|
||||
(Offset<Collide> o1, Offset<Collide> o2) =>
|
||||
new Collide().__assign(builder.DataBuffer.Length - o1.Value, builder.DataBuffer).Collide_.CompareTo(new Collide().__assign(builder.DataBuffer.Length - o2.Value, builder.DataBuffer).Collide_));
|
||||
return builder.CreateVectorOfTables(offsets);
|
||||
}
|
||||
|
||||
public static Collide? __lookup_by_key(int vectorLocation, string key, ByteBuffer bb) {
|
||||
Collide obj_ = new Collide();
|
||||
int span = bb.GetInt(vectorLocation - 4);
|
||||
int start = 0;
|
||||
while (span != 0) {
|
||||
int middle = span / 2;
|
||||
int tableOffset = Table.__indirect(vectorLocation + 4 * (start + middle), bb);
|
||||
obj_.__assign(tableOffset, bb);
|
||||
int comp = obj_.Collide_.CompareTo(key);
|
||||
if (comp > 0) {
|
||||
span = middle;
|
||||
} else if (comp < 0) {
|
||||
middle++;
|
||||
start += middle;
|
||||
span -= middle;
|
||||
} else {
|
||||
return obj_;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public CollideT UnPack() {
|
||||
var _o = new CollideT();
|
||||
this.UnPackTo(_o);
|
||||
return _o;
|
||||
}
|
||||
public void UnPackTo(CollideT _o) {
|
||||
_o.Collide_ = this.Collide_;
|
||||
_o.Value = this.Value;
|
||||
}
|
||||
public static Offset<union_value_collsion.Collide> Pack(FlatBufferBuilder builder, CollideT _o) {
|
||||
if (_o == null) return default(Offset<union_value_collsion.Collide>);
|
||||
var _collide = _o.Collide_ == null ? default(StringOffset) : builder.CreateString(_o.Collide_);
|
||||
var _value = _o.Value == null ? default(StringOffset) : builder.CreateString(_o.Value);
|
||||
return CreateCollide(
|
||||
builder,
|
||||
_collide,
|
||||
_value);
|
||||
}
|
||||
}
|
||||
|
||||
public class CollideT
|
||||
{
|
||||
[Newtonsoft.Json.JsonProperty("collide")]
|
||||
public string Collide_ { get; set; }
|
||||
[Newtonsoft.Json.JsonProperty("value")]
|
||||
public string Value { get; set; }
|
||||
|
||||
public CollideT() {
|
||||
this.Collide_ = null;
|
||||
this.Value = null;
|
||||
}
|
||||
}
|
||||
|
||||
public struct Collision : IFlatbufferObject
|
||||
{
|
||||
private Table __p;
|
||||
@@ -214,13 +318,17 @@ public struct Collision : IFlatbufferObject
|
||||
public union_value_collsion.Other ValueType { get { int o = __p.__offset(8); return o != 0 ? (union_value_collsion.Other)__p.bb.Get(o + __p.bb_pos) : union_value_collsion.Other.NONE; } }
|
||||
public TTable? Value<TTable>() where TTable : struct, IFlatbufferObject { int o = __p.__offset(10); return o != 0 ? (TTable?)__p.__union<TTable>(o + __p.bb_pos) : null; }
|
||||
public union_value_collsion.IntValue ValueAsIntValue() { return Value<union_value_collsion.IntValue>().Value; }
|
||||
public union_value_collsion.Collision? Collide(int j) { int o = __p.__offset(12); return o != 0 ? (union_value_collsion.Collision?)(new union_value_collsion.Collision()).__assign(__p.__indirect(__p.__vector(o) + j * 4), __p.bb) : null; }
|
||||
public int CollideLength { get { int o = __p.__offset(12); return o != 0 ? __p.__vector_len(o) : 0; } }
|
||||
|
||||
public static Offset<union_value_collsion.Collision> CreateCollision(FlatBufferBuilder builder,
|
||||
union_value_collsion.Value some_value_type = union_value_collsion.Value.NONE,
|
||||
int some_valueOffset = 0,
|
||||
union_value_collsion.Other value_type = union_value_collsion.Other.NONE,
|
||||
int valueOffset = 0) {
|
||||
builder.StartTable(4);
|
||||
int valueOffset = 0,
|
||||
VectorOffset collideOffset = default(VectorOffset)) {
|
||||
builder.StartTable(5);
|
||||
Collision.AddCollide(builder, collideOffset);
|
||||
Collision.AddValue(builder, valueOffset);
|
||||
Collision.AddSomeValue(builder, some_valueOffset);
|
||||
Collision.AddValueType(builder, value_type);
|
||||
@@ -228,11 +336,17 @@ public struct Collision : IFlatbufferObject
|
||||
return Collision.EndCollision(builder);
|
||||
}
|
||||
|
||||
public static void StartCollision(FlatBufferBuilder builder) { builder.StartTable(4); }
|
||||
public static void StartCollision(FlatBufferBuilder builder) { builder.StartTable(5); }
|
||||
public static void AddSomeValueType(FlatBufferBuilder builder, union_value_collsion.Value someValueType) { builder.AddByte(0, (byte)someValueType, 0); }
|
||||
public static void AddSomeValue(FlatBufferBuilder builder, int someValueOffset) { builder.AddOffset(1, someValueOffset, 0); }
|
||||
public static void AddValueType(FlatBufferBuilder builder, union_value_collsion.Other valueType) { builder.AddByte(2, (byte)valueType, 0); }
|
||||
public static void AddValue(FlatBufferBuilder builder, int valueOffset) { builder.AddOffset(3, valueOffset, 0); }
|
||||
public static void AddCollide(FlatBufferBuilder builder, VectorOffset collideOffset) { builder.AddOffset(4, collideOffset.Value, 0); }
|
||||
public static VectorOffset CreateCollideVector(FlatBufferBuilder builder, Offset<union_value_collsion.Collision>[] data) { builder.StartVector(4, data.Length, 4); for (int i = data.Length - 1; i >= 0; i--) builder.AddOffset(data[i].Value); return builder.EndVector(); }
|
||||
public static VectorOffset CreateCollideVectorBlock(FlatBufferBuilder builder, Offset<union_value_collsion.Collision>[] data) { builder.StartVector(4, data.Length, 4); builder.Add(data); return builder.EndVector(); }
|
||||
public static VectorOffset CreateCollideVectorBlock(FlatBufferBuilder builder, ArraySegment<Offset<union_value_collsion.Collision>> data) { builder.StartVector(4, data.Count, 4); builder.Add(data); return builder.EndVector(); }
|
||||
public static VectorOffset CreateCollideVectorBlock(FlatBufferBuilder builder, IntPtr dataPtr, int sizeInBytes) { builder.StartVector(1, sizeInBytes, 1); builder.Add<Offset<union_value_collsion.Collision>>(dataPtr, sizeInBytes); return builder.EndVector(); }
|
||||
public static void StartCollideVector(FlatBufferBuilder builder, int numElems) { builder.StartVector(4, numElems, 4); }
|
||||
public static Offset<union_value_collsion.Collision> EndCollision(FlatBufferBuilder builder) {
|
||||
int o = builder.EndTable();
|
||||
return new Offset<union_value_collsion.Collision>(o);
|
||||
@@ -261,6 +375,8 @@ public struct Collision : IFlatbufferObject
|
||||
_o.Value.Value = this.Value<union_value_collsion.IntValue>().HasValue ? this.Value<union_value_collsion.IntValue>().Value.UnPack() : null;
|
||||
break;
|
||||
}
|
||||
_o.Collide = new List<union_value_collsion.CollisionT>();
|
||||
for (var _j = 0; _j < this.CollideLength; ++_j) {_o.Collide.Add(this.Collide(_j).HasValue ? this.Collide(_j).Value.UnPack() : null);}
|
||||
}
|
||||
public static Offset<union_value_collsion.Collision> Pack(FlatBufferBuilder builder, CollisionT _o) {
|
||||
if (_o == null) return default(Offset<union_value_collsion.Collision>);
|
||||
@@ -268,12 +384,19 @@ public struct Collision : IFlatbufferObject
|
||||
var _some_value = _o.SomeValue == null ? 0 : union_value_collsion.ValueUnion.Pack(builder, _o.SomeValue);
|
||||
var _value_type = _o.Value == null ? union_value_collsion.Other.NONE : _o.Value.Type;
|
||||
var _value = _o.Value == null ? 0 : union_value_collsion.OtherUnion.Pack(builder, _o.Value);
|
||||
var _collide = default(VectorOffset);
|
||||
if (_o.Collide != null) {
|
||||
var __collide = new Offset<union_value_collsion.Collision>[_o.Collide.Count];
|
||||
for (var _j = 0; _j < __collide.Length; ++_j) { __collide[_j] = union_value_collsion.Collision.Pack(builder, _o.Collide[_j]); }
|
||||
_collide = CreateCollideVector(builder, __collide);
|
||||
}
|
||||
return CreateCollision(
|
||||
builder,
|
||||
_some_value_type,
|
||||
_some_value,
|
||||
_value_type,
|
||||
_value);
|
||||
_value,
|
||||
_collide);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -305,10 +428,13 @@ public class CollisionT
|
||||
[Newtonsoft.Json.JsonProperty("value")]
|
||||
[Newtonsoft.Json.JsonConverter(typeof(union_value_collsion.OtherUnion_JsonConverter))]
|
||||
public union_value_collsion.OtherUnion Value { get; set; }
|
||||
[Newtonsoft.Json.JsonProperty("collide")]
|
||||
public List<union_value_collsion.CollisionT> Collide { get; set; }
|
||||
|
||||
public CollisionT() {
|
||||
this.SomeValue = null;
|
||||
this.Value = null;
|
||||
this.Collide = null;
|
||||
}
|
||||
|
||||
public static CollisionT DeserializeFromJson(string jsonText) {
|
||||
|
||||
Reference in New Issue
Block a user