mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-26 18:42:39 +00:00
feat: Support union underlying type for TS/JS (#7961)
This commit is contained in:
@@ -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;
|
||||
|
||||
26
tests/ts/JavaScriptUnionUnderlyingTypeTest.js
Normal file
26
tests/ts/JavaScriptUnionUnderlyingTypeTest.js
Normal file
@@ -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()
|
||||
@@ -117,6 +117,11 @@ flatc(
|
||||
)
|
||||
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...")
|
||||
@@ -129,6 +134,7 @@ check_call(NODE_CMD + ["JavaScriptTest"])
|
||||
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"])
|
||||
|
||||
@@ -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"
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user