Merge commit '7c1ae250acb4120322b01a666993ed63c795ef21' into master

Update flatbuffers to latest master.

This adds an upstream flatbuffer_ts_library rule and changes the
typescript codegen, so ends up touching every typescript file we have
that uses flatbuffers. But it does clean up some of the hacks we had to
make to get that to work.

Had to hack the flatbuffer_ts_library a bit because I forgot to make
imports work nicely for cross-repo things.

And of course, the original motivation for this is that it gives us
proper handling of transitive dependencies for
flatbuffer_cc_library, if we start using the deps attribute!

Had to modify the codegen for the new declaration_file attribute in
flatbuffer schemas to make it just be the filename rather than including
the path. Modifying that to make it use a workspace-relative path was
looking obnoxious. It's definitely feasible, but since we don't actually
need that attribute, just make it be a filename for now.

Change-Id: I523a758cafa512fa2a686c9705d23337a26798ca
Signed-off-by: James Kuszmaul <james.kuszmaul@bluerivertech.com>
Signed-off-by: James Kuszmaul <jabukuszmaul+collab@gmail.com>
diff --git a/aos/events/logging/logger_test.cc b/aos/events/logging/logger_test.cc
index ec7ed10..e9f44b2 100644
--- a/aos/events/logging/logger_test.cc
+++ b/aos/events/logging/logger_test.cc
@@ -3393,9 +3393,9 @@
 }
 
 constexpr std::string_view kCombinedConfigSha1(
-    "b8114cd99e9c606b7517d8f478bf16513aef550f23f317720667314cb261f689");
+    "b33e522a2d0d746f5e514c3a9799987d9c79b2cc45c66f0a91c7a879d5248f0e");
 constexpr std::string_view kSplitConfigSha1(
-    "60943d0a46afa6028c8ae276e7b4900b35892f1f53b1818056e7d04463709c5b");
+    "0f36fe738e357c65082ced1b5d8179e70cb9e2ac6638e748279619db523aec1c");
 
 INSTANTIATE_TEST_SUITE_P(
     All, MultinodeLoggerTest,
diff --git a/aos/network/BUILD b/aos/network/BUILD
index ac9d96e..6143286 100644
--- a/aos/network/BUILD
+++ b/aos/network/BUILD
@@ -30,10 +30,10 @@
 flatbuffer_ts_library(
     name = "connect_ts_fbs",
     srcs = ["connect.fbs"],
-    includes = [
-        "//aos:configuration_fbs_includes",
-    ],
     target_compatible_with = ["@platforms//os:linux"],
+    deps = [
+        "//aos:configuration_ts_fbs",
+    ],
 )
 
 flatbuffer_cc_library(
@@ -411,11 +411,11 @@
 flatbuffer_ts_library(
     name = "web_proxy_ts_fbs",
     srcs = ["web_proxy.fbs"],
-    includes = [
-        ":connect_fbs_includes",
-        "//aos:configuration_fbs_includes",
-    ],
     target_compatible_with = ["@platforms//os:linux"],
+    deps = [
+        ":connect_ts_fbs",
+        "//aos:configuration_ts_fbs",
+    ],
 )
 
 cc_library(
diff --git a/aos/network/www/BUILD b/aos/network/www/BUILD
index 1b00af7..442e8be 100644
--- a/aos/network/www/BUILD
+++ b/aos/network/www/BUILD
@@ -24,6 +24,7 @@
         "//aos:configuration_ts_fbs",
         "//aos/network:connect_ts_fbs",
         "//aos/network:web_proxy_ts_fbs",
+        "@com_github_google_flatbuffers//reflection:reflection_ts_fbs",
         "@com_github_google_flatbuffers//ts:flatbuffers_ts",
     ],
 )
@@ -38,6 +39,7 @@
     deps = [
         ":proxy",
         "//aos/events:ping_ts_fbs",
+        "@com_github_google_flatbuffers//ts:flatbuffers_ts",
     ],
 )
 
@@ -87,6 +89,7 @@
     deps = [
         "//aos:configuration_ts_fbs",
         "//aos:json_to_flatbuffer_fbs_ts",
+        "@com_github_google_flatbuffers//reflection:reflection_ts_fbs",
         "@com_github_google_flatbuffers//ts:flatbuffers_ts",
     ],
 )
@@ -124,6 +127,7 @@
         "//aos:configuration_ts_fbs",
         "//aos/network:connect_ts_fbs",
         "//aos/network:web_proxy_ts_fbs",
+        "@com_github_google_flatbuffers//reflection:reflection_ts_fbs",
         "@com_github_google_flatbuffers//ts:flatbuffers_ts",
     ],
 )
diff --git a/aos/network/www/aos_plotter.ts b/aos/network/www/aos_plotter.ts
index 9e6ef1b..b7d8771 100644
--- a/aos/network/www/aos_plotter.ts
+++ b/aos/network/www/aos_plotter.ts
@@ -23,23 +23,13 @@
 // The demo_plot.ts script has a basic example of using this library, with all
 // the required boilerplate, as well as some extra examples about how to
 // add axis labels and the such.
-import * as configuration from 'org_frc971/aos/configuration_generated';
+import {Channel, Configuration} from 'org_frc971/aos/configuration_generated';
 import {Line, Plot, Point} from 'org_frc971/aos/network/www/plotter';
-import * as proxy from 'org_frc971/aos/network/www/proxy';
-import * as web_proxy from 'org_frc971/aos/network/web_proxy_generated';
-import * as reflection from 'org_frc971/aos/network/www/reflection'
-import * as flatbuffers_builder from 'org_frc971/external/com_github_google_flatbuffers/ts/builder';
-import {ByteBuffer} from 'org_frc971/external/com_github_google_flatbuffers/ts/byte-buffer';
-
-import Channel = configuration.aos.Channel;
-import Connection = proxy.Connection;
-import Configuration = configuration.aos.Configuration;
-import Schema = configuration.reflection.Schema;
-import Parser = reflection.Parser;
-import Table = reflection.Table;
-import SubscriberRequest = web_proxy.aos.web_proxy.SubscriberRequest;
-import ChannelRequest = web_proxy.aos.web_proxy.ChannelRequest;
-import TransferMethod = web_proxy.aos.web_proxy.TransferMethod;
+import {Connection} from 'org_frc971/aos/network/www/proxy';
+import {SubscriberRequest, ChannelRequest, TransferMethod} from 'org_frc971/aos/network/web_proxy_generated';
+import {Parser, Table} from 'org_frc971/aos/network/www/reflection'
+import {Schema} from 'org_frc971/external/com_github_google_flatbuffers/reflection/reflection_generated';
+import {ByteBuffer} from 'flatbuffers';
 
 export class TimestampedMessage {
   constructor(
diff --git a/aos/network/www/config_handler.ts b/aos/network/www/config_handler.ts
index 73b6b89..a7c2149 100644
--- a/aos/network/www/config_handler.ts
+++ b/aos/network/www/config_handler.ts
@@ -1,16 +1,8 @@
-import * as configuration from 'org_frc971/aos/configuration_generated';
+import {ByteBuffer} from 'flatbuffers';
+import {Configuration} from 'org_frc971/aos/configuration_generated';
 import {Connection} from 'org_frc971/aos/network/www/proxy';
-import * as flatbuffers_builder from 'org_frc971/external/com_github_google_flatbuffers/ts/builder';
-import * as web_proxy from 'org_frc971/aos/network/web_proxy_generated';
+
 import {Parser, Table} from './reflection'
-import {ByteBuffer} from 'org_frc971/external/com_github_google_flatbuffers/ts/byte-buffer';
-
-
-import Configuration = configuration.aos.Configuration;
-import Channel = configuration.aos.Channel;
-import SubscriberRequest = web_proxy.aos.web_proxy.SubscriberRequest;
-import ChannelRequest = web_proxy.aos.web_proxy.ChannelRequest;
-import TransferMethod = web_proxy.aos.web_proxy.TransferMethod;
 
 export class ConfigHandler {
   private readonly root_div = document.createElement('div');
diff --git a/aos/network/www/ping_handler.ts b/aos/network/www/ping_handler.ts
index 8d4b3c6..60e7c4a 100644
--- a/aos/network/www/ping_handler.ts
+++ b/aos/network/www/ping_handler.ts
@@ -1,11 +1,12 @@
-import {aos} from 'org_frc971/aos/events/ping_generated';
+import {Ping} from 'org_frc971/aos/events/ping_generated';
+import {ByteBuffer} from 'flatbuffers';
 
 export function HandlePing(data: Uint8Array) {
-  const fbBuffer = new flatbuffers.ByteBuffer(data);
-  const ping = aos.examples.Ping.getRootAsPing(fbBuffer);
+  const fbBuffer = new ByteBuffer(data);
+  const ping = Ping.getRootAsPing(fbBuffer);
 
   document.getElementById('val').innerHTML = ping.value().toString();
-  document.getElementById('time').innerHTML = ping.sendTime().low.toString();
+  document.getElementById('time').innerHTML = ping.sendTime().toString();
 }
 
 export function SetupDom() {
@@ -22,5 +23,5 @@
 }
 
 export function GetId() {
-  return aos.examples.Ping.getFullyQualifiedName();
+  return Ping.getFullyQualifiedName();
 }
diff --git a/aos/network/www/proxy.ts b/aos/network/www/proxy.ts
index 5415400..cf8e972 100644
--- a/aos/network/www/proxy.ts
+++ b/aos/network/www/proxy.ts
@@ -1,21 +1,7 @@
-import * as configuration from 'org_frc971/aos/configuration_generated';
-import * as web_proxy from 'org_frc971/aos/network/web_proxy_generated';
-import {Builder} from 'org_frc971/external/com_github_google_flatbuffers/ts/builder';
-import {ByteBuffer} from 'org_frc971/external/com_github_google_flatbuffers/ts/byte-buffer';
-
-import ChannelFb = configuration.aos.Channel;
-import Configuration = configuration.aos.Configuration;
-import Schema = configuration.reflection.Schema;
-import MessageHeader = web_proxy.aos.web_proxy.MessageHeader;
-import WebSocketIce = web_proxy.aos.web_proxy.WebSocketIce;
-import WebSocketMessage = web_proxy.aos.web_proxy.WebSocketMessage;
-import Payload = web_proxy.aos.web_proxy.Payload;
-import WebSocketSdp = web_proxy.aos.web_proxy.WebSocketSdp;
-import SdpType = web_proxy.aos.web_proxy.SdpType;
-import SubscriberRequest = web_proxy.aos.web_proxy.SubscriberRequest;
-import ChannelRequestFb = web_proxy.aos.web_proxy.ChannelRequest;
-import TransferMethod = web_proxy.aos.web_proxy.TransferMethod;
-import ChannelState = web_proxy.aos.web_proxy.ChannelState;
+import {Builder, ByteBuffer, Offset} from 'flatbuffers';
+import {Channel as ChannelFb, Configuration} from 'org_frc971/aos/configuration_generated';
+import {ChannelRequest as ChannelRequestFb, ChannelState, MessageHeader, Payload, SdpType, SubscriberRequest, TransferMethod, WebSocketIce, WebSocketMessage, WebSocketSdp} from 'org_frc971/aos/network/web_proxy_generated';
+import {Schema} from 'org_frc971/external/com_github_google_flatbuffers/reflection/reflection_generated';
 
 // There is one handler for each DataChannel, it maintains the state of
 // multi-part messages and delegates to a callback when the message is fully
@@ -32,11 +18,10 @@
 
   handleMessage(e: MessageEvent): void {
     const fbBuffer = new ByteBuffer(new Uint8Array(e.data));
-    const messageHeader = MessageHeader.getRootAsMessageHeader(
-        fbBuffer as unknown as flatbuffers.ByteBuffer);
-    const time = messageHeader.monotonicSentTime().toFloat64() * 1e-9;
+    const messageHeader = MessageHeader.getRootAsMessageHeader(fbBuffer);
+    const time = Number(messageHeader.monotonicSentTime()) * 1e-9;
 
-    const stateBuilder = new Builder(512) as unknown as flatbuffers.Builder;
+    const stateBuilder = new Builder(512);
     ChannelState.startChannelState(stateBuilder);
     ChannelState.addQueueIndex(stateBuilder, messageHeader.queueIndex());
     ChannelState.addPacketIndex(stateBuilder, messageHeader.packetIndex());
@@ -167,8 +152,8 @@
       throw new Error(
           'Must call subscribeToChannel after we\'ve received the config.');
     }
-    const builder = new Builder(512) as unknown as flatbuffers.Builder;
-    const channels: flatbuffers.Offset[] = [];
+    const builder = new Builder(512);
+    const channels: Offset[] = [];
     for (const channel of this.subscribedChannels) {
       const nameFb = builder.createString(channel.channel.name);
       const typeFb = builder.createString(channel.channel.type);
@@ -208,8 +193,7 @@
   // all other messages are sent on specific DataChannels.
   onConfigMessage(data: Uint8Array): void {
     const fbBuffer = new ByteBuffer(data);
-    this.configInternal = Configuration.getRootAsConfiguration(
-        fbBuffer as unknown as flatbuffers.ByteBuffer);
+    this.configInternal = Configuration.getRootAsConfiguration(fbBuffer);
     for (const handler of Array.from(this.configHandlers)) {
       handler(this.configInternal);
     }
@@ -234,10 +218,9 @@
     const sdpMidString = builder.createString(candidate.sdpMid);
 
     const iceFb = WebSocketIce.createWebSocketIce(
-        builder as unknown as flatbuffers.Builder, candidateString,
-        sdpMidString, candidate.sdpMLineIndex);
+        builder, candidateString, sdpMidString, candidate.sdpMLineIndex);
     const messageFb = WebSocketMessage.createWebSocketMessage(
-        builder as unknown as flatbuffers.Builder, Payload.WebSocketIce, iceFb);
+        builder, Payload.WebSocketIce, iceFb);
     builder.finish(messageFb);
     const array = builder.asUint8Array();
     this.webSocketConnection.send(array.buffer.slice(array.byteOffset));
@@ -254,9 +237,9 @@
     const offerString = builder.createString(description.sdp);
 
     const webSocketSdp = WebSocketSdp.createWebSocketSdp(
-        builder as unknown as flatbuffers.Builder, SdpType.OFFER, offerString);
+        builder, SdpType.OFFER, offerString);
     const message = WebSocketMessage.createWebSocketMessage(
-        builder as unknown as flatbuffers.Builder, Payload.WebSocketSdp,
+        builder, Payload.WebSocketSdp,
         webSocketSdp);
     builder.finish(message);
     const array = builder.asUint8Array();
@@ -287,8 +270,7 @@
   onWebSocketMessage(e: MessageEvent): void {
     const buffer = new Uint8Array(e.data)
     const fbBuffer = new ByteBuffer(buffer);
-    const message = WebSocketMessage.getRootAsWebSocketMessage(
-        fbBuffer as unknown as flatbuffers.ByteBuffer);
+    const message = WebSocketMessage.getRootAsWebSocketMessage(fbBuffer);
     switch (message.payloadType()) {
       case Payload.WebSocketSdp:
         const sdpFb = message.payload(new WebSocketSdp());
diff --git a/aos/network/www/reflection.ts b/aos/network/www/reflection.ts
index 18d4996..a765eea 100644
--- a/aos/network/www/reflection.ts
+++ b/aos/network/www/reflection.ts
@@ -6,9 +6,9 @@
 // constructed flatbuffers.
 // See reflection_test_main.ts for sample usage.
 
-import {reflection, aos} from 'org_frc971/aos/configuration_generated';
-import {ByteBuffer} from 'org_frc971/external/com_github_google_flatbuffers/ts/byte-buffer';
-import {Long} from 'org_frc971/external/com_github_google_flatbuffers/ts/long';
+import * as aos from 'org_frc971/aos/configuration_generated';
+import * as reflection from '../../../external/com_github_google_flatbuffers/reflection/reflection_generated';
+import {ByteBuffer} from 'flatbuffers';
 
 // Returns the size, in bytes, of the given type. For vectors/strings/etc.
 // returns the size of the offset.
@@ -236,7 +236,7 @@
   }
 
   // Returns the Object definition associated with the given type index.
-  getType(typeIndex: number): reflection.ObjectGenerated {
+  getType(typeIndex: number): reflection.Object_ {
     if (typeIndex === -1) {
       return this.schema.rootTable();
     }
@@ -249,7 +249,7 @@
   // Retrieves the Field schema for the given field name within a given
   // type index.
   getField(fieldName: string, typeIndex: number): reflection.Field {
-    const schema: reflection.ObjectGenerated = this.getType(typeIndex);
+    const schema: reflection.Object_ = this.getType(typeIndex);
     const numFields = schema.fieldsLength();
     for (let ii = 0; ii < numFields; ++ii) {
       const field = schema.fields(ii);
@@ -269,7 +269,7 @@
   // value for the field and return that.
   // For 64-bit fields, returns a flatbuffer Long rather than a standard number.
   readScalar(table: Table, fieldName: string, readDefaults: boolean = false):
-      number|Long|null {
+      number|BigInt|null {
     return this.readScalarLambda(
         table.typeIndex, fieldName, readDefaults)(table);
   }
@@ -279,7 +279,7 @@
   // can be obtained using table.typeIndex.
   readScalarLambda(
       typeIndex: number, fieldName: string,
-      readDefaults: boolean = false): (t: Table) => number | Long | null {
+      readDefaults: boolean = false): (t: Table) => number | BigInt | null {
     const field = this.getField(fieldName, typeIndex);
     const fieldType = field.type();
     const isStruct = this.getType(typeIndex).isStruct();
@@ -301,15 +301,7 @@
           return null;
         }
         if (isInteger(fieldType.baseType())) {
-          if (isLong(fieldType.baseType())) {
-            return field.defaultInteger();
-          } else {
-            if (field.defaultInteger().high != 0) {
-              throw new Error(
-                  '<=4 byte integer types should not use 64-bit default values.');
-            }
-            return field.defaultInteger().low;
-          }
+          return field.defaultInteger();
         } else {
           return field.defaultReal();
         }
@@ -374,14 +366,14 @@
       return new Table(table.bb, fieldType.index(), objectStart);
     };
   }
-  // Reads a vector of scalars (like readScalar, may return a vector of Long's
+  // Reads a vector of scalars (like readScalar, may return a vector of BigInt's
   // instead). Also, will return null if the vector is not set.
-  readVectorOfScalars(table: Table, fieldName: string): number[]|Long[]|null {
+  readVectorOfScalars(table: Table, fieldName: string): number[]|BigInt[]|null {
     return this.readVectorOfScalarsLambda(table.typeIndex, fieldName)(table);
   }
 
   readVectorOfScalarsLambda(typeIndex: number, fieldName: string):
-      (t: Table) => number[] | Long[] | null {
+      (t: Table) => number[] | BigInt[] | null {
     const field = this.getField(fieldName, typeIndex);
     const fieldType = field.type();
     if (fieldType.baseType() !== reflection.BaseType.Vector) {
diff --git a/aos/network/www/reflection_test_main.ts b/aos/network/www/reflection_test_main.ts
index 5dead7b..8fb041e 100644
--- a/aos/network/www/reflection_test_main.ts
+++ b/aos/network/www/reflection_test_main.ts
@@ -1,20 +1,10 @@
-import * as configuration from 'org_frc971/aos/configuration_generated'
-import * as testing from 'org_frc971/aos/json_to_flatbuffer_generated'
-import * as flatbuffers_builder from 'org_frc971/external/com_github_google_flatbuffers/ts/builder';
-import {ByteBuffer} from 'org_frc971/external/com_github_google_flatbuffers/ts/byte-buffer';
-import {Long} from 'org_frc971/external/com_github_google_flatbuffers/ts/long';
+import {Builder, ByteBuffer} from 'flatbuffers';
+import {Configuration} from 'org_frc971/aos/configuration_generated'
+import {BaseType, Configuration as TestTable, FooStruct, Location, Map, VectorOfStrings, VectorOfVectorOfString} from 'org_frc971/aos/json_to_flatbuffer_generated'
 
 import {Connection} from './proxy';
 import {Parser, Table} from './reflection'
 
-import Configuration = configuration.aos.Configuration;
-import BaseType = testing.aos.testing.BaseType;
-import TestTable = testing.aos.testing.Configuration;
-import FooStruct = testing.aos.testing.FooStruct;
-import Location = testing.aos.testing.Location;
-import Map = testing.aos.testing.Map;
-import VectorOfStrings = testing.aos.testing.VectorOfStrings;
-import VectorOfVectorOfString = testing.aos.testing.VectorOfVectorOfString;
 // This file runs a basic test to confirm that the typescript flatbuffer
 // reflection library is working correctly. It currently is not run
 // automatically--to run it, run the web_proxy_demo sh_binary target, open the
@@ -35,8 +25,7 @@
 // that the start/end results are the same. This is largely meant to ensure
 // that we are exercising most of the logic associated with parsing flatbuffers.
 function DoTest(config: Configuration): void {
-  const builder =
-      new flatbuffers_builder.Builder() as unknown as flatbuffers.Builder;
+  const builder = new Builder();
   {
     TestTable.startVectorFooStructVector(builder, 3);
     const fooStruct0 = FooStruct.createFooStruct(builder, 66, 118);
@@ -76,8 +65,7 @@
     TestTable.addVectorFooStruct(builder, vectorFooStruct);
     TestTable.addVectorFooDouble(builder, doubleVector);
     TestTable.addFooDouble(builder, 11.14);
-    TestTable.addFooLong(
-        builder, new Long(100, 1) as unknown as flatbuffers.Long);
+    TestTable.addFooLong(builder, BigInt('1000000000000'));
     TestTable.addFooEnum(builder, BaseType.Array);
   }
 
@@ -85,8 +73,7 @@
   const array = builder.asUint8Array();
   const fbBuffer = new ByteBuffer(array);
 
-  const parsedFb = TestTable.getRootAsConfiguration(
-      fbBuffer as unknown as flatbuffers.ByteBuffer);
+  const parsedFb = TestTable.getRootAsConfiguration(fbBuffer);
 
   let testSchema = null;
   for (let ii = 0; ii < config.channelsLength(); ++ii) {
@@ -107,8 +94,7 @@
   assertEqual(11.14, parsedFb.fooDouble());
   assertEqual(testObject['foo_double'], parsedFb.fooDouble());
   assertEqual(testObject['foo_enum'], parsedFb.fooEnum());
-  assertEqual(testObject['foo_long'].low, parsedFb.fooLong().low);
-  assertEqual(testObject['foo_long'].high, parsedFb.fooLong().high);
+  assertEqual(testObject['foo_long'], parsedFb.fooLong());
   assertEqual(testObject['foo_ulong'], undefined);
   assertEqual(testObject['locations'], undefined);
 
@@ -163,7 +149,7 @@
     throw new Error('Couldn\'t find Configuration schema in config.');
   }
   let configParser = new Parser(configSchema);
-  const configTable = Table.getRootTable(config.bb as unknown as ByteBuffer);
+  const configTable = Table.getRootTable(config.bb);
   console.log('Received config:');
   console.log(configParser.toObject(configTable, true));