Skip to content

Commit

Permalink
Convenient createX methods for TS (google#5044)
Browse files Browse the repository at this point in the history
  • Loading branch information
krojew authored and aardappel committed Nov 16, 2018
1 parent 9068b0e commit 30bae01
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 13 deletions.
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
"name": "flatbuffers",
"version": "1.10.2",
"description": "Memory Efficient Serialization Library",
"files": ["js/flatbuffers.js", "js/flatbuffers.mjs"],
"files": [
"js/flatbuffers.js",
"js/flatbuffers.mjs"
],
"main": "js/flatbuffers",
"module": "js/flatbuffers.mjs",
"directories": {
Expand All @@ -26,5 +29,6 @@
"bugs": {
"url": "https://github.com/google/flatbuffers/issues"
},
"homepage": "https://google.github.io/flatbuffers/"
"homepage": "https://google.github.io/flatbuffers/",
"dependencies": {}
}
56 changes: 45 additions & 11 deletions src/idl_gen_js.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1118,8 +1118,7 @@ class JsGenerator : public BaseGenerator {
it != struct_def.fields.vec.end(); ++it) {
auto &field = **it;
if (field.deprecated) continue;
auto argname = MakeCamel(field.name, false);
if (!IsScalar(field.value.type.base_type)) { argname += "Offset"; }
const auto argname = GetArgName(field);

// Generate the field insertion method
GenDocComment(
Expand All @@ -1129,16 +1128,8 @@ class JsGenerator : public BaseGenerator {
argname, false));

if (lang_.language == IDLOptions::kTs) {
std::string argType;
if (field.value.type.enum_def) {
argType = GenPrefixedTypeName(GenTypeName(field.value.type, true),
field.value.type.enum_def->file);
} else {
argType = GenTypeName(field.value.type, true);
}

code += "static add" + MakeCamel(field.name);
code += "(builder:flatbuffers.Builder, " + argname + ":" + argType +
code += "(builder:flatbuffers.Builder, " + argname + ":" + GetArgType(field) +
") {\n";
} else {
code += object_name + ".add" + MakeCamel(field.name);
Expand Down Expand Up @@ -1268,13 +1259,56 @@ class JsGenerator : public BaseGenerator {
code += ");\n";
code += "};\n\n";
}

if (lang_.language == IDLOptions::kTs) {
// Generate a convenient CreateX function
code += "static create" + struct_def.name + "(builder:flatbuffers.Builder";
for (auto it = struct_def.fields.vec.begin();
it != struct_def.fields.vec.end(); ++it) {
const auto &field = **it;
if (field.deprecated)
continue;

code += ", " + GetArgName(field) + ":" + GetArgType(field);
}

code += "):flatbuffers.Offset {\n";
code += " " + struct_def.name + ".start" + struct_def.name + "(builder);\n";

for (auto it = struct_def.fields.vec.begin();
it != struct_def.fields.vec.end(); ++it) {
const auto &field = **it;
if (field.deprecated)
continue;

code += " " + struct_def.name + ".add" + MakeCamel(field.name) +"(";
code += "builder, " + GetArgName(field) + ");\n";
}

code += " return " + struct_def.name + ".end" + struct_def.name + "(builder);\n";
code += "}\n";
}
}

if (lang_.language == IDLOptions::kTs) {
if (!object_namespace.empty()) { code += "}\n"; }
code += "}\n";
}
}

std::string GetArgType(const FieldDef &field) {
if (field.value.type.enum_def)
return GenPrefixedTypeName(GenTypeName(field.value.type, true),
field.value.type.enum_def->file);
return GenTypeName(field.value.type, true);
}

static std::string GetArgName(const FieldDef &field) {
auto argname = MakeCamel(field.name, false);
if (!IsScalar(field.value.type.base_type)) { argname += "Offset"; }

return argname;
}
};
} // namespace js

Expand Down
92 changes: 92 additions & 0 deletions tests/monster_test_generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ static endInParentNamespace(builder:flatbuffers.Builder):flatbuffers.Offset {
return offset;
};

static createInParentNamespace(builder:flatbuffers.Builder):flatbuffers.Offset {
InParentNamespace.startInParentNamespace(builder);
return InParentNamespace.endInParentNamespace(builder);
}
}
}
/**
Expand Down Expand Up @@ -133,6 +137,10 @@ static endMonster(builder:flatbuffers.Builder):flatbuffers.Offset {
return offset;
};

static createMonster(builder:flatbuffers.Builder):flatbuffers.Offset {
Monster.startMonster(builder);
return Monster.endMonster(builder);
}
}
}
/**
Expand Down Expand Up @@ -289,6 +297,11 @@ static endTestSimpleTableWithEnum(builder:flatbuffers.Builder):flatbuffers.Offse
return offset;
};

static createTestSimpleTableWithEnum(builder:flatbuffers.Builder, color:MyGame.Example.Color):flatbuffers.Offset {
TestSimpleTableWithEnum.startTestSimpleTableWithEnum(builder);
TestSimpleTableWithEnum.addColor(builder, color);
return TestSimpleTableWithEnum.endTestSimpleTableWithEnum(builder);
}
}
}
/**
Expand Down Expand Up @@ -661,6 +674,13 @@ static endStat(builder:flatbuffers.Builder):flatbuffers.Offset {
return offset;
};

static createStat(builder:flatbuffers.Builder, idOffset:flatbuffers.Offset, val:flatbuffers.Long, count:number):flatbuffers.Offset {
Stat.startStat(builder);
Stat.addId(builder, idOffset);
Stat.addVal(builder, val);
Stat.addCount(builder, count);
return Stat.endStat(builder);
}
}
}
/**
Expand Down Expand Up @@ -738,6 +758,11 @@ static endReferrable(builder:flatbuffers.Builder):flatbuffers.Offset {
return offset;
};

static createReferrable(builder:flatbuffers.Builder, id:flatbuffers.Long):flatbuffers.Offset {
Referrable.startReferrable(builder);
Referrable.addId(builder, id);
return Referrable.endReferrable(builder);
}
}
}
/**
Expand Down Expand Up @@ -2444,6 +2469,57 @@ static finishMonsterBuffer(builder:flatbuffers.Builder, offset:flatbuffers.Offse
builder.finish(offset, 'MONS');
};

static createMonster(builder:flatbuffers.Builder, posOffset:flatbuffers.Offset, mana:number, hp:number, nameOffset:flatbuffers.Offset, inventoryOffset:flatbuffers.Offset, color:MyGame.Example.Color, testType:MyGame.Example.Any, testOffset:flatbuffers.Offset, test4Offset:flatbuffers.Offset, testarrayofstringOffset:flatbuffers.Offset, testarrayoftablesOffset:flatbuffers.Offset, enemyOffset:flatbuffers.Offset, testnestedflatbufferOffset:flatbuffers.Offset, testemptyOffset:flatbuffers.Offset, testbool:boolean, testhashs32Fnv1:number, testhashu32Fnv1:number, testhashs64Fnv1:flatbuffers.Long, testhashu64Fnv1:flatbuffers.Long, testhashs32Fnv1a:number, testhashu32Fnv1a:number, testhashs64Fnv1a:flatbuffers.Long, testhashu64Fnv1a:flatbuffers.Long, testarrayofboolsOffset:flatbuffers.Offset, testf:number, testf2:number, testf3:number, testarrayofstring2Offset:flatbuffers.Offset, testarrayofsortedstructOffset:flatbuffers.Offset, flexOffset:flatbuffers.Offset, test5Offset:flatbuffers.Offset, vectorOfLongsOffset:flatbuffers.Offset, vectorOfDoublesOffset:flatbuffers.Offset, parentNamespaceTestOffset:flatbuffers.Offset, vectorOfReferrablesOffset:flatbuffers.Offset, singleWeakReference:flatbuffers.Long, vectorOfWeakReferencesOffset:flatbuffers.Offset, vectorOfStrongReferrablesOffset:flatbuffers.Offset, coOwningReference:flatbuffers.Long, vectorOfCoOwningReferencesOffset:flatbuffers.Offset, nonOwningReference:flatbuffers.Long, vectorOfNonOwningReferencesOffset:flatbuffers.Offset, anyUniqueType:MyGame.Example.AnyUniqueAliases, anyUniqueOffset:flatbuffers.Offset, anyAmbiguousType:MyGame.Example.AnyAmbiguousAliases, anyAmbiguousOffset:flatbuffers.Offset, vectorOfEnumsOffset:flatbuffers.Offset):flatbuffers.Offset {
Monster.startMonster(builder);
Monster.addPos(builder, posOffset);
Monster.addMana(builder, mana);
Monster.addHp(builder, hp);
Monster.addName(builder, nameOffset);
Monster.addInventory(builder, inventoryOffset);
Monster.addColor(builder, color);
Monster.addTestType(builder, testType);
Monster.addTest(builder, testOffset);
Monster.addTest4(builder, test4Offset);
Monster.addTestarrayofstring(builder, testarrayofstringOffset);
Monster.addTestarrayoftables(builder, testarrayoftablesOffset);
Monster.addEnemy(builder, enemyOffset);
Monster.addTestnestedflatbuffer(builder, testnestedflatbufferOffset);
Monster.addTestempty(builder, testemptyOffset);
Monster.addTestbool(builder, testbool);
Monster.addTesthashs32Fnv1(builder, testhashs32Fnv1);
Monster.addTesthashu32Fnv1(builder, testhashu32Fnv1);
Monster.addTesthashs64Fnv1(builder, testhashs64Fnv1);
Monster.addTesthashu64Fnv1(builder, testhashu64Fnv1);
Monster.addTesthashs32Fnv1a(builder, testhashs32Fnv1a);
Monster.addTesthashu32Fnv1a(builder, testhashu32Fnv1a);
Monster.addTesthashs64Fnv1a(builder, testhashs64Fnv1a);
Monster.addTesthashu64Fnv1a(builder, testhashu64Fnv1a);
Monster.addTestarrayofbools(builder, testarrayofboolsOffset);
Monster.addTestf(builder, testf);
Monster.addTestf2(builder, testf2);
Monster.addTestf3(builder, testf3);
Monster.addTestarrayofstring2(builder, testarrayofstring2Offset);
Monster.addTestarrayofsortedstruct(builder, testarrayofsortedstructOffset);
Monster.addFlex(builder, flexOffset);
Monster.addTest5(builder, test5Offset);
Monster.addVectorOfLongs(builder, vectorOfLongsOffset);
Monster.addVectorOfDoubles(builder, vectorOfDoublesOffset);
Monster.addParentNamespaceTest(builder, parentNamespaceTestOffset);
Monster.addVectorOfReferrables(builder, vectorOfReferrablesOffset);
Monster.addSingleWeakReference(builder, singleWeakReference);
Monster.addVectorOfWeakReferences(builder, vectorOfWeakReferencesOffset);
Monster.addVectorOfStrongReferrables(builder, vectorOfStrongReferrablesOffset);
Monster.addCoOwningReference(builder, coOwningReference);
Monster.addVectorOfCoOwningReferences(builder, vectorOfCoOwningReferencesOffset);
Monster.addNonOwningReference(builder, nonOwningReference);
Monster.addVectorOfNonOwningReferences(builder, vectorOfNonOwningReferencesOffset);
Monster.addAnyUniqueType(builder, anyUniqueType);
Monster.addAnyUnique(builder, anyUniqueOffset);
Monster.addAnyAmbiguousType(builder, anyAmbiguousType);
Monster.addAnyAmbiguous(builder, anyAmbiguousOffset);
Monster.addVectorOfEnums(builder, vectorOfEnumsOffset);
return Monster.endMonster(builder);
}
}
}
/**
Expand Down Expand Up @@ -2908,5 +2984,21 @@ static endTypeAliases(builder:flatbuffers.Builder):flatbuffers.Offset {
return offset;
};

static createTypeAliases(builder:flatbuffers.Builder, i8:number, u8:number, i16:number, u16:number, i32:number, u32:number, i64:flatbuffers.Long, u64:flatbuffers.Long, f32:number, f64:number, v8Offset:flatbuffers.Offset, vf64Offset:flatbuffers.Offset):flatbuffers.Offset {
TypeAliases.startTypeAliases(builder);
TypeAliases.addI8(builder, i8);
TypeAliases.addU8(builder, u8);
TypeAliases.addI16(builder, i16);
TypeAliases.addU16(builder, u16);
TypeAliases.addI32(builder, i32);
TypeAliases.addU32(builder, u32);
TypeAliases.addI64(builder, i64);
TypeAliases.addU64(builder, u64);
TypeAliases.addF32(builder, f32);
TypeAliases.addF64(builder, f64);
TypeAliases.addV8(builder, v8Offset);
TypeAliases.addVf64(builder, vf64Offset);
return TypeAliases.endTypeAliases(builder);
}
}
}
5 changes: 5 additions & 0 deletions tests/namespace_test/namespace_test1_generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ static endTableInNestedNS(builder:flatbuffers.Builder):flatbuffers.Offset {
return offset;
};

static createTableInNestedNS(builder:flatbuffers.Builder, foo:number):flatbuffers.Offset {
TableInNestedNS.startTableInNestedNS(builder);
TableInNestedNS.addFoo(builder, foo);
return TableInNestedNS.endTableInNestedNS(builder);
}
}
}
/**
Expand Down
18 changes: 18 additions & 0 deletions tests/namespace_test/namespace_test2_generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ static endTableInFirstNS(builder:flatbuffers.Builder):flatbuffers.Offset {
return offset;
};

static createTableInFirstNS(builder:flatbuffers.Builder, fooTableOffset:flatbuffers.Offset, fooEnum:NS8755221360535654258.NamespaceA.NamespaceB.EnumInNestedNS, fooStructOffset:flatbuffers.Offset):flatbuffers.Offset {
TableInFirstNS.startTableInFirstNS(builder);
TableInFirstNS.addFooTable(builder, fooTableOffset);
TableInFirstNS.addFooEnum(builder, fooEnum);
TableInFirstNS.addFooStruct(builder, fooStructOffset);
return TableInFirstNS.endTableInFirstNS(builder);
}
}
}
/**
Expand Down Expand Up @@ -190,6 +197,12 @@ static endTableInC(builder:flatbuffers.Builder):flatbuffers.Offset {
return offset;
};

static createTableInC(builder:flatbuffers.Builder, referToA1Offset:flatbuffers.Offset, referToA2Offset:flatbuffers.Offset):flatbuffers.Offset {
TableInC.startTableInC(builder);
TableInC.addReferToA1(builder, referToA1Offset);
TableInC.addReferToA2(builder, referToA2Offset);
return TableInC.endTableInC(builder);
}
}
}
/**
Expand Down Expand Up @@ -253,5 +266,10 @@ static endSecondTableInA(builder:flatbuffers.Builder):flatbuffers.Offset {
return offset;
};

static createSecondTableInA(builder:flatbuffers.Builder, referToCOffset:flatbuffers.Offset):flatbuffers.Offset {
SecondTableInA.startSecondTableInA(builder);
SecondTableInA.addReferToC(builder, referToCOffset);
return SecondTableInA.endSecondTableInA(builder);
}
}
}
13 changes: 13 additions & 0 deletions tests/union_vector/union_vector_generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ static endAttacker(builder:flatbuffers.Builder):flatbuffers.Offset {
return offset;
};

static createAttacker(builder:flatbuffers.Builder, swordAttackDamage:number):flatbuffers.Offset {
Attacker.startAttacker(builder);
Attacker.addSwordAttackDamage(builder, swordAttackDamage);
return Attacker.endAttacker(builder);
}
}
/**
* @constructor
Expand Down Expand Up @@ -400,4 +405,12 @@ static finishMovieBuffer(builder:flatbuffers.Builder, offset:flatbuffers.Offset)
builder.finish(offset, 'MOVI');
};

static createMovie(builder:flatbuffers.Builder, mainCharacterType:Character, mainCharacterOffset:flatbuffers.Offset, charactersTypeOffset:flatbuffers.Offset, charactersOffset:flatbuffers.Offset):flatbuffers.Offset {
Movie.startMovie(builder);
Movie.addMainCharacterType(builder, mainCharacterType);
Movie.addMainCharacter(builder, mainCharacterOffset);
Movie.addCharactersType(builder, charactersTypeOffset);
Movie.addCharacters(builder, charactersOffset);
return Movie.endMovie(builder);
}
}

0 comments on commit 30bae01

Please sign in to comment.