Skip to content

Commit

Permalink
[C#] Fix collision of field name and type name (#7149)
Browse files Browse the repository at this point in the history
* 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

* Add example for type field name collision
  • Loading branch information
tira-misu authored Mar 30, 2022
1 parent 2d21853 commit 6c5603f
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 9 deletions.
5 changes: 5 additions & 0 deletions scripts/generate_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,11 @@ def glob(path, pattern):

flatc(NO_INCL_OPTS + CPP_OPTS, schema=optional_scalars_schema)

# Type / field collsion
type_field_collsion_schema = "type_field_collsion.fbs"

flatc(["--csharp", "--gen-object-api"], schema=type_field_collsion_schema)

# Generate string/vector default code for tests
flatc(RUST_OPTS, prefix="more_defaults", schema="more_defaults.fbs")

Expand Down
30 changes: 21 additions & 9 deletions src/idl_gen_csharp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,9 @@ class CSharpGenerator : public BaseGenerator {
code += member_suffix;
code += "}\n";
if (IsVector(field.value.type)) {
code += " public int " + Name(field);
auto camel_name = Name(field);
if (camel_name == struct_def.name) { camel_name += "_"; }
code += " public int " + camel_name;
code += "Length";
code += " { get";
code += offset_prefix;
Expand Down Expand Up @@ -1566,6 +1568,7 @@ class CSharpGenerator : public BaseGenerator {

void GenUnionUnPack_ObjectAPI(const EnumDef &enum_def, std::string *code_ptr,
const std::string &camel_name,
const std::string &camel_name_short,
bool is_vector) const {
auto &code = *code_ptr;
std::string varialbe_name = "_o." + camel_name;
Expand All @@ -1584,10 +1587,11 @@ class CSharpGenerator : public BaseGenerator {
code += indent + varialbe_name + " = new ";
}
code += NamespacedName(enum_def) + "Union();\n";
code += indent + varialbe_name + ".Type = this." + camel_name + "Type" +
code += indent + varialbe_name + ".Type = this." + camel_name_short +
"Type" +
type_suffix + ";\n";
code +=
indent + "switch (this." + camel_name + "Type" + type_suffix + ") {\n";
code += indent + "switch (this." + camel_name_short + "Type" + type_suffix +
") {\n";
for (auto eit = enum_def.Vals().begin(); eit != enum_def.Vals().end();
++eit) {
auto &ev = **eit;
Expand Down Expand Up @@ -1633,6 +1637,8 @@ class CSharpGenerator : public BaseGenerator {
auto &field = **it;
if (field.deprecated) continue;
auto camel_name = Name(field);
if (camel_name == struct_def.name) { camel_name += "_"; }
auto camel_name_short = Name(field);
auto start = " _o." + camel_name + " = ";
switch (field.value.type.base_type) {
case BASE_TYPE_STRUCT: {
Expand Down Expand Up @@ -1666,7 +1672,7 @@ class CSharpGenerator : public BaseGenerator {
code += " for (var _j = 0; _j < this." + camel_name +
"Length; ++_j) {\n";
GenUnionUnPack_ObjectAPI(*field.value.type.enum_def, code_ptr,
camel_name, true);
camel_name, camel_name_short, true);
code += " }\n";
} else if (field.value.type.element != BASE_TYPE_UTYPE) {
auto fixed = field.value.type.struct_def == nullptr;
Expand All @@ -1687,7 +1693,7 @@ class CSharpGenerator : public BaseGenerator {
case BASE_TYPE_UTYPE: break;
case BASE_TYPE_UNION: {
GenUnionUnPack_ObjectAPI(*field.value.type.enum_def, code_ptr,
camel_name, false);
camel_name, camel_name_short, false);
break;
}
default: {
Expand All @@ -1707,6 +1713,8 @@ class CSharpGenerator : public BaseGenerator {
auto &field = **it;
if (field.deprecated) continue;
auto camel_name = Name(field);
if (camel_name == struct_def.name) { camel_name += "_"; }
auto camel_name_short = Name(field);
// pre
switch (field.value.type.base_type) {
case BASE_TYPE_STRUCT: {
Expand Down Expand Up @@ -1782,7 +1790,7 @@ class CSharpGenerator : public BaseGenerator {
code += " var " + array_name + " = _o." + property_name +
".ToArray();\n";
}
code += " _" + field.name + " = Create" + camel_name +
code += " _" + field.name + " = Create" + camel_name_short +
"Vector(builder, " + array_name + ");\n";
code += " }\n";
} else {
Expand All @@ -1794,7 +1802,7 @@ class CSharpGenerator : public BaseGenerator {
camel_name + "[_j]);";
code += " var _" + field.name + " = default(VectorOffset);\n";
code += " if (_o." + camel_name + " != null) {\n";
code += " Start" + camel_name + "Vector(builder, _o." +
code += " Start" + camel_name_short + "Vector(builder, _o." +
camel_name + ".Count);\n";
code += " for (var _j = _o." + camel_name +
".Count - 1; _j >= 0; --_j) { " + pack_method + " }\n";
Expand Down Expand Up @@ -1840,6 +1848,7 @@ class CSharpGenerator : public BaseGenerator {
auto &field = **it;
if (field.deprecated) continue;
auto camel_name = Name(field);
if (camel_name == struct_def.name) { camel_name += "_"; }
switch (field.value.type.base_type) {
case BASE_TYPE_STRUCT: {
if (struct_def.fixed) {
Expand Down Expand Up @@ -2094,6 +2103,7 @@ class CSharpGenerator : public BaseGenerator {
auto type_name = GenTypeGet_ObjectAPI(field.value.type, opts);
if (field.IsScalarOptional()) type_name += "?";
auto camel_name = Name(field);
if (camel_name == struct_def.name) { camel_name += "_"; }
if (opts.cs_gen_json_serializer) {
if (IsUnion(field.value.type)) {
auto utype_name = NamespacedName(*field.value.type.enum_def);
Expand Down Expand Up @@ -2159,7 +2169,9 @@ class CSharpGenerator : public BaseGenerator {
if (field.deprecated) continue;
if (field.value.type.base_type == BASE_TYPE_UTYPE) continue;
if (field.value.type.element == BASE_TYPE_UTYPE) continue;
code += " this." + Name(field) + " = ";
auto camel_name = Name(field);
if (camel_name == struct_def.name) { camel_name += "_"; }
code += " this." + camel_name + " = ";
auto type_name = GenTypeGet_ObjectAPI(field.value.type, opts);
if (IsScalar(field.value.type.base_type)) {
code += GenDefaultValue(field) + ";\n";
Expand Down
8 changes: 8 additions & 0 deletions tests/type_field_collsion.fbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace type_field_collsion;

// This table tests collsions of identifiers.
table Collision {
collision : int;
}

root_type Collision;
73 changes: 73 additions & 0 deletions tests/type_field_collsion/Collision.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// <auto-generated>
// automatically generated by the FlatBuffers compiler, do not modify
// </auto-generated>

namespace type_field_collsion
{

using global::System;
using global::System.Collections.Generic;
using global::FlatBuffers;

public struct Collision : IFlatbufferObject
{
private Table __p;
public ByteBuffer ByteBuffer { get { return __p.bb; } }
public static void ValidateVersion() { FlatBufferConstants.FLATBUFFERS_2_0_0(); }
public static Collision GetRootAsCollision(ByteBuffer _bb) { return GetRootAsCollision(_bb, new Collision()); }
public static Collision GetRootAsCollision(ByteBuffer _bb, Collision obj) { return (obj.__assign(_bb.GetInt(_bb.Position) + _bb.Position, _bb)); }
public void __init(int _i, ByteBuffer _bb) { __p = new Table(_i, _bb); }
public Collision __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }

public int Collision_ { get { int o = __p.__offset(4); return o != 0 ? __p.bb.GetInt(o + __p.bb_pos) : (int)0; } }

public static Offset<type_field_collsion.Collision> CreateCollision(FlatBufferBuilder builder,
int collision = 0) {
builder.StartTable(1);
Collision.AddCollision(builder, collision);
return Collision.EndCollision(builder);
}

public static void StartCollision(FlatBufferBuilder builder) { builder.StartTable(1); }
public static void AddCollision(FlatBufferBuilder builder, int collision) { builder.AddInt(0, collision, 0); }
public static Offset<type_field_collsion.Collision> EndCollision(FlatBufferBuilder builder) {
int o = builder.EndTable();
return new Offset<type_field_collsion.Collision>(o);
}
public static void FinishCollisionBuffer(FlatBufferBuilder builder, Offset<type_field_collsion.Collision> offset) { builder.Finish(offset.Value); }
public static void FinishSizePrefixedCollisionBuffer(FlatBufferBuilder builder, Offset<type_field_collsion.Collision> offset) { builder.FinishSizePrefixed(offset.Value); }
public CollisionT UnPack() {
var _o = new CollisionT();
this.UnPackTo(_o);
return _o;
}
public void UnPackTo(CollisionT _o) {
_o.Collision_ = this.Collision_;
}
public static Offset<type_field_collsion.Collision> Pack(FlatBufferBuilder builder, CollisionT _o) {
if (_o == null) return default(Offset<type_field_collsion.Collision>);
return CreateCollision(
builder,
_o.Collision_);
}
}

public class CollisionT
{
public int Collision_ { get; set; }

public CollisionT() {
this.Collision_ = 0;
}
public static CollisionT DeserializeFromBinary(byte[] fbBuffer) {
return Collision.GetRootAsCollision(new ByteBuffer(fbBuffer)).UnPack();
}
public byte[] SerializeToBinary() {
var fbb = new FlatBufferBuilder(0x10000);
Collision.FinishCollisionBuffer(fbb, Collision.Pack(fbb, this));
return fbb.DataBuffer.ToSizedArray();
}
}


}

0 comments on commit 6c5603f

Please sign in to comment.