Skip to content

Commit

Permalink
Merge pull request #2 from sketch-hq/master
Browse files Browse the repository at this point in the history
Merge master into develop
  • Loading branch information
InvisiblePixels authored Aug 19, 2021
2 parents d6f51ea + c5b1236 commit 2e0dd3d
Show file tree
Hide file tree
Showing 39 changed files with 702 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/idl_gen_csharp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1335,7 +1335,7 @@ class CSharpGenerator : public BaseGenerator {
GenPackUnPack_ObjectAPI(struct_def, code_ptr, opts, struct_has_create,
field_has_create_set);
}
code += "};\n\n";
code += "}\n\n";

if (opts.generate_object_based_api) {
GenStruct_ObjectAPI(struct_def, code_ptr, opts);
Expand Down
25 changes: 16 additions & 9 deletions src/idl_gen_rust.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ std::string MakeUpper(const std::string &in) {
return s;
}

std::string UnionTypeFieldName(const FieldDef &field) {
return MakeSnakeCase(field.name + "_type");
}

// Encapsulate all logical field types in this enum. This allows us to write
// field logic based on type switches, instead of branches on the properties
// set on the Type.
Expand Down Expand Up @@ -407,7 +411,7 @@ class RustGenerator : public BaseGenerator {
gen_symbol(symbol);
std::stringstream file_path;
file_path << path_;
// DO NOT SUBMIT: CASPER: Refactor out common path name generation.
// Create filepath.
if (symbol.defined_namespace)
for (auto i = symbol.defined_namespace->components.begin();
i != symbol.defined_namespace->components.end(); i++) {
Expand Down Expand Up @@ -1634,6 +1638,7 @@ class RustGenerator : public BaseGenerator {
code_.SetValue("OFFSET_VALUE", NumToString(field.value.offset));
code_.SetValue("FIELD_NAME", Name(field));
code_.SetValue("BLDR_DEF_VAL", GetDefaultValue(field, kBuilder));
code_.SetValue("DISCRIMINANT", UnionTypeFieldName(field));
cb(field);
};
const auto &fields = struct_def.fields.vec;
Expand Down Expand Up @@ -1905,7 +1910,6 @@ class RustGenerator : public BaseGenerator {
// Explicit specializations for union accessors
ForAllTableFields(struct_def, [&](const FieldDef &field) {
if (field.value.type.base_type != BASE_TYPE_UNION) return;
code_.SetValue("FIELD_TYPE_FIELD_NAME", field.name);
ForAllUnionVariantsBesidesNone(
*field.value.type.enum_def, [&](const EnumVal &unused) {
(void)unused;
Expand All @@ -1926,8 +1930,7 @@ class RustGenerator : public BaseGenerator {
//
// To avoid this problem the type field name is used unescaped here:
code_ +=
" if self.{{FIELD_TYPE_FIELD_NAME}}_type() == "
"{{U_ELEMENT_ENUM_TYPE}} {";
" if self.{{DISCRIMINANT}}() == {{U_ELEMENT_ENUM_TYPE}} {";

// The following logic is not tested in the integration test,
// as of April 10, 2020
Expand Down Expand Up @@ -1973,11 +1976,16 @@ class RustGenerator : public BaseGenerator {
return;
}
// Unions.
EnumDef &union_def = *field.value.type.enum_def;
const EnumDef &union_def = *field.value.type.enum_def;
code_.SetValue("UNION_TYPE", WrapInNameSpace(union_def));
// TODO: Use the same function that generates the _type field for
// consistency. We do not call Name() because it inconsistently
// escapes keywords.
code_.SetValue("UNION_TYPE_OFFSET_NAME",
"VT_" + MakeUpper(field.name + "_type"));
code_ +=
"\n .visit_union::<{{UNION_TYPE}}, _>("
"\"{{FIELD_NAME}}_type\", Self::{{OFFSET_NAME}}_TYPE, "
"\"{{FIELD_NAME}}_type\", Self::{{UNION_TYPE_OFFSET_NAME}}, "
"\"{{FIELD_NAME}}\", Self::{{OFFSET_NAME}}, {{IS_REQ}}, "
"|key, v, pos| {";
code_ += " match key {";
Expand Down Expand Up @@ -2101,19 +2109,18 @@ class RustGenerator : public BaseGenerator {
if (GetFullType(field.value.type) == ftUnionValue) {
// Generate a match statement to handle unions properly.
code_.SetValue("KEY_TYPE", GenTableAccessorFuncReturnType(field, ""));
code_.SetValue("FIELD_TYPE_FIELD_NAME", field.name);
code_.SetValue("UNION_ERR",
"&\"InvalidFlatbuffer: Union discriminant"
" does not match value.\"");

code_ += " match self.{{FIELD_NAME}}_type() {";
code_ += " match self.{{DISCRIMINANT}}() {";
ForAllUnionVariantsBesidesNone(
*field.value.type.enum_def, [&](const EnumVal &unused) {
(void)unused;
code_ += " {{U_ELEMENT_ENUM_TYPE}} => {";
code_ +=
" if let Some(x) = "
"self.{{FIELD_TYPE_FIELD_NAME}}_as_"
"self.{{FIELD_NAME}}_as_"
"{{U_ELEMENT_NAME}}() {";
code_ += " ds.field(\"{{FIELD_NAME}}\", &x)";
code_ += " } else {";
Expand Down
2 changes: 1 addition & 1 deletion tests/MyGame/Example/Ability.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void UnPackTo(AbilityT _o) {
_o.Id,
_o.Distance);
}
};
}

public class AbilityT
{
Expand Down
2 changes: 1 addition & 1 deletion tests/MyGame/Example/ArrayStruct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void UnPackTo(ArrayStructT _o) {
_o.E,
_f);
}
};
}

public class ArrayStructT
{
Expand Down
2 changes: 1 addition & 1 deletion tests/MyGame/Example/ArrayTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void UnPackTo(ArrayTableT _o) {
AddA(builder, MyGame.Example.ArrayStruct.Pack(builder, _o.A));
return EndArrayTable(builder);
}
};
}

public class ArrayTableT
{
Expand Down
2 changes: 1 addition & 1 deletion tests/MyGame/Example/Monster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ public void UnPackTo(MonsterT _o) {
_testrequirednestedflatbuffer,
_scalar_key_sorted_tables);
}
};
}

public class MonsterT
{
Expand Down
2 changes: 1 addition & 1 deletion tests/MyGame/Example/NestedStruct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void UnPackTo(NestedStructT _o) {
_c,
_d);
}
};
}

public class NestedStructT
{
Expand Down
2 changes: 1 addition & 1 deletion tests/MyGame/Example/Referrable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void UnPackTo(ReferrableT _o) {
builder,
_o.Id);
}
};
}

public class ReferrableT
{
Expand Down
2 changes: 1 addition & 1 deletion tests/MyGame/Example/Stat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void UnPackTo(StatT _o) {
_o.Val,
_o.Count);
}
};
}

public class StatT
{
Expand Down
2 changes: 1 addition & 1 deletion tests/MyGame/Example/StructOfStructs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void UnPackTo(StructOfStructsT _o) {
_c_id,
_c_distance);
}
};
}

public class StructOfStructsT
{
Expand Down
2 changes: 1 addition & 1 deletion tests/MyGame/Example/Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void UnPackTo(TestT _o) {
_o.A,
_o.B);
}
};
}

public class TestT
{
Expand Down
2 changes: 1 addition & 1 deletion tests/MyGame/Example/TestSimpleTableWithEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void UnPackTo(TestSimpleTableWithEnumT _o) {
builder,
_o.Color);
}
};
}

internal partial class TestSimpleTableWithEnumT
{
Expand Down
2 changes: 1 addition & 1 deletion tests/MyGame/Example/TypeAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public void UnPackTo(TypeAliasesT _o) {
_v8,
_vf64);
}
};
}

public class TypeAliasesT
{
Expand Down
2 changes: 1 addition & 1 deletion tests/MyGame/Example/Vec3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void UnPackTo(Vec3T _o) {
_test3_a,
_test3_b);
}
};
}

public class Vec3T
{
Expand Down
2 changes: 1 addition & 1 deletion tests/MyGame/Example2/Monster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void UnPackTo(MonsterT _o) {
StartMonster(builder);
return EndMonster(builder);
}
};
}

public class MonsterT
{
Expand Down
2 changes: 1 addition & 1 deletion tests/MyGame/InParentNamespace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void UnPackTo(InParentNamespaceT _o) {
StartInParentNamespace(builder);
return EndInParentNamespace(builder);
}
};
}

public class InParentNamespaceT
{
Expand Down
2 changes: 1 addition & 1 deletion tests/MyGame/MonsterExtra.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public void UnPackTo(MonsterExtraT _o) {
_dvec,
_fvec);
}
};
}

public class MonsterExtraT
{
Expand Down
1 change: 1 addition & 0 deletions tests/generate_code.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ $TEST_NOINCL_FLAGS $TEST_CPP_FLAGS $TEST_CS_FLAGS $TEST_TS_FLAGS -o namespace_te

# Generate the keywords tests
../flatc --csharp $TEST_BASE_FLAGS $TEST_CS_FLAGS -o keyword_test ./keyword_test.fbs
../flatc --rust $TEST_RUST_FLAGS -o keyword_test ./keyword_test.fbs

working_dir=`pwd`
cd FlatBuffers.Test.Swift/Tests/FlatBuffers.Test.SwiftTests
Expand Down
3 changes: 2 additions & 1 deletion tests/keyword_test.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ enum public: int { }
table KeywordsInTable {
is: ABC = void;
private: public;
type: int;
}

union KeywordsInUnion {
static: KeywordsInTable,
internal: string
internal: KeywordsInTable,
}
20 changes: 15 additions & 5 deletions tests/keyword_test/KeywordsInTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,24 @@ public struct KeywordsInTable : IFlatbufferObject
public bool MutateIs(ABC is_) { int o = __p.__offset(4); if (o != 0) { __p.bb.PutInt(o + __p.bb_pos, (int)is_); return true; } else { return false; } }
public public_ Private { get { int o = __p.__offset(6); return o != 0 ? (public_)__p.bb.GetInt(o + __p.bb_pos) : public_.NONE; } }
public bool MutatePrivate(public_ private_) { int o = __p.__offset(6); if (o != 0) { __p.bb.PutInt(o + __p.bb_pos, (int)private_); return true; } else { return false; } }
public int Type { get { int o = __p.__offset(8); return o != 0 ? __p.bb.GetInt(o + __p.bb_pos) : (int)0; } }
public bool MutateType(int type) { int o = __p.__offset(8); if (o != 0) { __p.bb.PutInt(o + __p.bb_pos, type); return true; } else { return false; } }

public static Offset<KeywordsInTable> CreateKeywordsInTable(FlatBufferBuilder builder,
ABC is_ = ABC.void_,
public_ private_ = public_.NONE) {
builder.StartTable(2);
public_ private_ = public_.NONE,
int type = 0) {
builder.StartTable(3);
KeywordsInTable.AddType(builder, type);
KeywordsInTable.AddPrivate(builder, private_);
KeywordsInTable.AddIs(builder, is_);
return KeywordsInTable.EndKeywordsInTable(builder);
}

public static void StartKeywordsInTable(FlatBufferBuilder builder) { builder.StartTable(2); }
public static void StartKeywordsInTable(FlatBufferBuilder builder) { builder.StartTable(3); }
public static void AddIs(FlatBufferBuilder builder, ABC is_) { builder.AddInt(0, (int)is_, 0); }
public static void AddPrivate(FlatBufferBuilder builder, public_ private_) { builder.AddInt(1, (int)private_, 0); }
public static void AddType(FlatBufferBuilder builder, int type) { builder.AddInt(2, type, 0); }
public static Offset<KeywordsInTable> EndKeywordsInTable(FlatBufferBuilder builder) {
int o = builder.EndTable();
return new Offset<KeywordsInTable>(o);
Expand All @@ -45,26 +50,31 @@ public KeywordsInTableT UnPack() {
public void UnPackTo(KeywordsInTableT _o) {
_o.Is = this.Is;
_o.Private = this.Private;
_o.Type = this.Type;
}
public static Offset<KeywordsInTable> Pack(FlatBufferBuilder builder, KeywordsInTableT _o) {
if (_o == null) return default(Offset<KeywordsInTable>);
return CreateKeywordsInTable(
builder,
_o.Is,
_o.Private);
_o.Private,
_o.Type);
}
};
}

public class KeywordsInTableT
{
[Newtonsoft.Json.JsonProperty("is")]
public ABC Is { get; set; }
[Newtonsoft.Json.JsonProperty("private")]
public public_ Private { get; set; }
[Newtonsoft.Json.JsonProperty("type")]
public int Type { get; set; }

public KeywordsInTableT() {
this.Is = ABC.void_;
this.Private = public_.NONE;
this.Type = 0;
}
}

8 changes: 4 additions & 4 deletions tests/keyword_test/KeywordsInUnion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ public KeywordsInUnionUnion() {
public T As<T>() where T : class { return this.Value as T; }
public KeywordsInTableT Asstatic() { return this.As<KeywordsInTableT>(); }
public static KeywordsInUnionUnion Fromstatic(KeywordsInTableT _static) { return new KeywordsInUnionUnion{ Type = KeywordsInUnion.static_, Value = _static }; }
public string Asinternal() { return this.As<string>(); }
public static KeywordsInUnionUnion Frominternal(string _internal) { return new KeywordsInUnionUnion{ Type = KeywordsInUnion.internal_, Value = _internal }; }
public KeywordsInTableT Asinternal() { return this.As<KeywordsInTableT>(); }
public static KeywordsInUnionUnion Frominternal(KeywordsInTableT _internal) { return new KeywordsInUnionUnion{ Type = KeywordsInUnion.internal_, Value = _internal }; }

public static int Pack(FlatBuffers.FlatBufferBuilder builder, KeywordsInUnionUnion _o) {
switch (_o.Type) {
default: return 0;
case KeywordsInUnion.static_: return KeywordsInTable.Pack(builder, _o.Asstatic()).Value;
case KeywordsInUnion.internal_: return builder.CreateString(_o.Asinternal()).Value;
case KeywordsInUnion.internal_: return KeywordsInTable.Pack(builder, _o.Asinternal()).Value;
}
}
}
Expand Down Expand Up @@ -70,7 +70,7 @@ public KeywordsInUnionUnion ReadJson(Newtonsoft.Json.JsonReader reader, Keywords
switch (_o.Type) {
default: break;
case KeywordsInUnion.static_: _o.Value = serializer.Deserialize<KeywordsInTableT>(reader); break;
case KeywordsInUnion.internal_: _o.Value = serializer.Deserialize<string>(reader); break;
case KeywordsInUnion.internal_: _o.Value = serializer.Deserialize<KeywordsInTableT>(reader); break;
}
return _o;
}
Expand Down
Loading

0 comments on commit 2e0dd3d

Please sign in to comment.