Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support union underlying type for TS/JS #7961

Merged
merged 1 commit into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions src/idl_gen_ts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ class TsGenerator : public BaseGenerator {
switch (type.base_type) {
case BASE_TYPE_BOOL:
case BASE_TYPE_CHAR: return "Int8";
case BASE_TYPE_UTYPE:
case BASE_TYPE_UTYPE: return GenType(GetUnionUnderlyingType(type));
case BASE_TYPE_UCHAR: return "Uint8";
case BASE_TYPE_SHORT: return "Int16";
case BASE_TYPE_USHORT: return "Uint16";
Expand Down Expand Up @@ -562,11 +562,26 @@ class TsGenerator : public BaseGenerator {
}
}

static Type GetUnionUnderlyingType(const Type &type)
{
if (type.enum_def != nullptr &&
type.enum_def->underlying_type.base_type != type.base_type) {
return type.enum_def->underlying_type;
} else {
return Type(BASE_TYPE_UCHAR);
}
}

static Type GetUnderlyingVectorType(const Type &vector_type)
{
return (vector_type.base_type == BASE_TYPE_UTYPE) ? GetUnionUnderlyingType(vector_type) : vector_type;
}

// Returns the method name for use with add/put calls.
std::string GenWriteMethod(const Type &type) {
// Forward to signed versions since unsigned versions don't exist
switch (type.base_type) {
case BASE_TYPE_UTYPE:
case BASE_TYPE_UTYPE: return GenWriteMethod(GetUnionUnderlyingType(type));
case BASE_TYPE_UCHAR: return GenWriteMethod(Type(BASE_TYPE_CHAR));
case BASE_TYPE_USHORT: return GenWriteMethod(Type(BASE_TYPE_SHORT));
case BASE_TYPE_UINT: return GenWriteMethod(Type(BASE_TYPE_INT));
Expand Down Expand Up @@ -1763,7 +1778,8 @@ class TsGenerator : public BaseGenerator {
auto vectortype = field.value.type.VectorType();
auto vectortypename =
GenTypeName(imports, struct_def, vectortype, false);
auto inline_size = InlineSize(vectortype);
auto type = GetUnderlyingVectorType(vectortype);
auto inline_size = InlineSize(type);
auto index = GenBBAccess() +
".__vector(this.bb_pos + offset) + index" +
MaybeScale(inline_size);
Expand Down Expand Up @@ -1994,8 +2010,9 @@ class TsGenerator : public BaseGenerator {

if (IsVector(field.value.type)) {
auto vector_type = field.value.type.VectorType();
auto alignment = InlineAlignment(vector_type);
auto elem_size = InlineSize(vector_type);
auto type = GetUnderlyingVectorType(vector_type);
auto alignment = InlineAlignment(type);
auto elem_size = InlineSize(type);

// Generate a method to create a vector from a JavaScript array
if (!IsStruct(vector_type)) {
Expand Down
2 changes: 1 addition & 1 deletion src/idl_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2718,7 +2718,7 @@ bool Parser::Supports64BitOffsets() const {
}

bool Parser::SupportsUnionUnderlyingType() const {
return (opts.lang_to_generate & ~IDLOptions::kCpp) == 0;
return (opts.lang_to_generate & ~(IDLOptions::kCpp | IDLOptions::kTs)) == 0;
}

Namespace *Parser::UniqueNamespace(Namespace *ns) {
Expand Down
2 changes: 1 addition & 1 deletion tests/parser_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ void ParseUnionTest() {
// Test union underlying type
const char *source = "table A {} table B {} union U : int {A, B} table C {test_union: U; test_vector_of_union: [U];}";
flatbuffers::Parser parser3;
parser3.opts.lang_to_generate = flatbuffers::IDLOptions::kCpp;
parser3.opts.lang_to_generate = flatbuffers::IDLOptions::kCpp | flatbuffers::IDLOptions::kTs;
TEST_EQ(parser3.Parse(source), true);

parser3.opts.lang_to_generate &= flatbuffers::IDLOptions::kJava;
Expand Down
26 changes: 26 additions & 0 deletions tests/ts/JavaScriptUnionUnderlyingTypeTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import assert from 'assert'
import * as flatbuffers from 'flatbuffers'
import {UnionUnderlyingType as Test} from './union_underlying_type_test.js'

function main() {
let a = new Test.AT();
a.a = 1;
let b = new Test.BT();
b.b = "foo";
let c = new Test.CT();
c.c = true;
let d = new Test.DT();
d.testUnionType = Test.ABC.A;
d.testUnion = a;
d.testVectorOfUnionType = [Test.ABC.A, Test.ABC.B, Test.ABC.C];
d.testVectorOfUnion = [a, b, c];

let fbb = new flatbuffers.Builder();
let offset = d.pack(fbb);
fbb.finish(offset);

let unpacked = Test.D.getRootAsD(fbb.dataBuffer()).unpack();
assert.equal(JSON.stringify(unpacked), JSON.stringify(d));
}

main()
6 changes: 6 additions & 0 deletions tests/ts/TypeScriptTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ def esbuild(input, output):
)
esbuild("typescript_keywords.ts", "typescript_keywords_generated.cjs")

flatc(
options=["--ts", "--reflect-names", "--gen-name-strings", "--gen-mutable", "--gen-object-api", "--ts-entry-points", "--ts-flat-files"],
schema="../union_underlying_type_test.fbs"
)

print("Running TypeScript Compiler...")
check_call(["tsc"])
print("Running TypeScript Compiler in old node resolution mode for no_import_ext...")
Expand All @@ -129,6 +134,7 @@ def esbuild(input, output):
check_call(NODE_CMD + ["JavaScriptUnionVectorTest"])
check_call(NODE_CMD + ["JavaScriptFlexBuffersTest"])
check_call(NODE_CMD + ["JavaScriptComplexArraysTest"])
check_call(NODE_CMD + ["JavaScriptUnionUnderlyingTypeTest"])

print("Running old v1 TypeScript Tests...")
check_call(NODE_CMD + ["JavaScriptTestv1.cjs", "./monster_test_generated.cjs"])
3 changes: 2 additions & 1 deletion tests/ts/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"optional_scalars/**/*.ts",
"namespace_test/**/*.ts",
"union_vector/**/*.ts",
"arrays_test_complex/**/*.ts"
"arrays_test_complex/**/*.ts",
"union_underlying_type_test.ts"
]
}