From 299aa1d331ac6eac98c18bb74a679ea302ee69fe Mon Sep 17 00:00:00 2001 From: rachwalk Date: Thu, 5 Oct 2023 16:03:16 +0200 Subject: [PATCH 1/3] Add Time type to exports for typescript types --- schemas/typescript/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/schemas/typescript/index.ts b/schemas/typescript/index.ts index 17d8755..d730afd 100644 --- a/schemas/typescript/index.ts +++ b/schemas/typescript/index.ts @@ -39,6 +39,7 @@ export * from "./SceneUpdate"; export * from "./SpherePrimitive"; export * from "./TextAnnotation"; export * from "./TextPrimitive"; +export * from "./Time"; export * from "./TriangleListPrimitive"; export * from "./Vector2"; export * from "./Vector3"; From c56334a4c2522bee15d6bd2ec5217b63b7b0eaac Mon Sep 17 00:00:00 2001 From: rachwalk Date: Fri, 6 Oct 2023 10:04:06 +0200 Subject: [PATCH 2/3] Change the exports to be included in autogeneration of typsecript exports --- internal/exportTypeScriptSchemas.ts | 3 ++- internal/schemas.ts | 42 +++++++++++++++++++++++++++++ internal/types.ts | 9 ++++++- schemas/typescript/index.ts | 1 - 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/internal/exportTypeScriptSchemas.ts b/internal/exportTypeScriptSchemas.ts index 5f6de5f..9fe85c3 100644 --- a/internal/exportTypeScriptSchemas.ts +++ b/internal/exportTypeScriptSchemas.ts @@ -4,7 +4,7 @@ import { TIME_TS, generateTypeScript, } from "./generateTypeScript"; -import { foxgloveEnumSchemas, foxgloveMessageSchemas } from "./schemas"; +import { foxgloveEnumSchemas, foxgloveMessageSchemas, foxglovePrimitiveSchemas } from "./schemas"; /** * Export schemas as TypeScript source code, keyed by the file base name (without `.ts` suffix). @@ -30,6 +30,7 @@ export function exportTypeScriptSchemas( const allSchemaNames = [ ...Object.values(foxgloveMessageSchemas), ...Object.values(foxgloveEnumSchemas), + ...Object.values(foxglovePrimitiveSchemas), ].sort((a, b) => a.name.localeCompare(b.name)); let indexTS = ""; for (const schema of allSchemaNames) { diff --git a/internal/schemas.ts b/internal/schemas.ts index 5e2d689..d552bd7 100644 --- a/internal/schemas.ts +++ b/internal/schemas.ts @@ -1449,6 +1449,48 @@ const LaserScan: FoxgloveMessageSchema = { ], }; +const Time: FoxglovePrimitiveSchema = { + type: "primitive", + name: "time", + description: "Timestamp in the format of second and nanoseconds since epoch", + fields: [ + { + name: "sec", + type: { type: "primitive", name: "float64" }, + description: "Seconds since epoch", + }, + { + name: "nsec", + type: { type: "primitive", name: "float64" }, + description: "nanoseconds", + }, + ] +} + +const Duration: FoxglovePrimitiveSchema = { + type: "primitive", + name: "time", + description: "Difference between two timestamps", + fields: [ + { + name: "sec", + type: { type: "primitive", name: "float64" }, + description: "Seconds since epoch", + }, + { + name: "nsec", + type: { type: "primitive", name: "float64" }, + description: "nanoseconds", + }, + ] +} + +export const foxglovePrimitiveSchemas = { + time: Time, + duration: Duration, +}; + + export const foxgloveMessageSchemas = { ArrowPrimitive, CameraCalibration, diff --git a/internal/types.ts b/internal/types.ts index 73f7e86..1b11442 100644 --- a/internal/types.ts +++ b/internal/types.ts @@ -7,6 +7,13 @@ export type FoxglovePrimitive = | "time" | "duration"; +export type FoxglovePrimitiveSchema = { + type: "primitive"; + name: string; + description: string; + fields: ReadonlyArray; +} + export type FoxgloveEnumSchema = { type: "enum"; name: string; @@ -41,4 +48,4 @@ export type FoxgloveMessageSchema = { fields: ReadonlyArray; }; -export type FoxgloveSchema = FoxgloveMessageSchema | FoxgloveEnumSchema; +export type FoxgloveSchema = FoxgloveMessageSchema | FoxgloveEnumSchema | FoxglovePrimitiveSchema; diff --git a/schemas/typescript/index.ts b/schemas/typescript/index.ts index d730afd..17d8755 100644 --- a/schemas/typescript/index.ts +++ b/schemas/typescript/index.ts @@ -39,7 +39,6 @@ export * from "./SceneUpdate"; export * from "./SpherePrimitive"; export * from "./TextAnnotation"; export * from "./TextPrimitive"; -export * from "./Time"; export * from "./TriangleListPrimitive"; export * from "./Vector2"; export * from "./Vector3"; From c642ab62eba905742bf8ad17e728404483680f06 Mon Sep 17 00:00:00 2001 From: rachwalk Date: Fri, 6 Oct 2023 11:11:25 +0200 Subject: [PATCH 3/3] Move Time and Duration generation into respective functions for generating --- internal/exportTypeScriptSchemas.ts | 12 ++++------ internal/generateFlatbufferSchema.ts | 11 +++++++++ internal/generateOmgIdl.ts | 14 ++++++++++++ internal/generateTypeScript.ts | 13 +++++++++++ internal/schemas.ts | 15 ++++++------ internal/types.ts | 2 +- scripts/updateGeneratedFiles.ts | 34 ++++++++++++++++++---------- 7 files changed, 72 insertions(+), 29 deletions(-) diff --git a/internal/exportTypeScriptSchemas.ts b/internal/exportTypeScriptSchemas.ts index 9fe85c3..c9b9f23 100644 --- a/internal/exportTypeScriptSchemas.ts +++ b/internal/exportTypeScriptSchemas.ts @@ -1,9 +1,4 @@ -import { - DURATION_TS, - GenerateTypeScriptOptions, - TIME_TS, - generateTypeScript, -} from "./generateTypeScript"; +import { GenerateTypeScriptOptions, generateTypeScript } from "./generateTypeScript"; import { foxgloveEnumSchemas, foxgloveMessageSchemas, foxglovePrimitiveSchemas } from "./schemas"; /** @@ -24,8 +19,9 @@ export function exportTypeScriptSchemas( schemas.set(schema.name, generateTypeScript(schema, options)); } - schemas.set("Duration", DURATION_TS); - schemas.set("Time", TIME_TS); + for (const schema of Object.values(foxglovePrimitiveSchemas)) { + schemas.set(schema.name, generateTypeScript(schema, options)); + } const allSchemaNames = [ ...Object.values(foxgloveMessageSchemas), diff --git a/internal/generateFlatbufferSchema.ts b/internal/generateFlatbufferSchema.ts index 149ea49..3270dd9 100644 --- a/internal/generateFlatbufferSchema.ts +++ b/internal/generateFlatbufferSchema.ts @@ -74,6 +74,17 @@ export function generateFlatbuffers( let definition; const imports = new Set(); switch (schema.type) { + case "primitive": + definition = ""; + if (schema.name === "Time") { + definition = TIME_FB; + } else if (schema.name === "Duration") { + definition = DURATION_FB; + } + if (definition === "") { + throw new Error(`Flatbuffer encountered an unexpected type: ${schema.name}`); + } + return definition; case "enum": { const fields = schema.values.map(({ name, value, description }) => { if (description != undefined) { diff --git a/internal/generateOmgIdl.ts b/internal/generateOmgIdl.ts index 0b81169..117c80d 100644 --- a/internal/generateOmgIdl.ts +++ b/internal/generateOmgIdl.ts @@ -42,6 +42,20 @@ export function generateOmgIdl(schema: FoxgloveSchema): string { let definition: string; switch (schema.type) { + case "primitive": + { + definition = ""; + if (schema.name === "Time") { + definition = TIME_IDL; + } else if (schema.name === "Duration") { + definition = DURATION_IDL; + } + if (definition === "") { + throw new Error(`Flatbuffer encountered an unexpected type: ${schema.name}`); + } + return definition; + } + break; case "enum": { const fields = schema.values.map(({ name, value, description }, index) => { const separator = index === schema.values.length - 1 ? "" : ","; diff --git a/internal/generateTypeScript.ts b/internal/generateTypeScript.ts index 4634a15..8dfdbb8 100644 --- a/internal/generateTypeScript.ts +++ b/internal/generateTypeScript.ts @@ -57,6 +57,19 @@ export function generateTypeScript( let definition: string; switch (schema.type) { + case "primitive": + definition = ""; + if (schema.name === "Time") { + definition = TIME_TS; + } else if (schema.name === "Duration") { + definition = DURATION_TS; + } + if (definition === "") { + throw new Error( + `TypeScriptGenerator encountered an unexpected primitive type ${schema.name}`, + ); + } + return definition; case "enum": { const fields = schema.values.map(({ name, value, description }) => { if (description != undefined) { diff --git a/internal/schemas.ts b/internal/schemas.ts index d552bd7..3ab1878 100644 --- a/internal/schemas.ts +++ b/internal/schemas.ts @@ -1,4 +1,4 @@ -import { FoxgloveEnumSchema, FoxgloveMessageSchema } from "./types"; +import { FoxgloveEnumSchema, FoxgloveMessageSchema, FoxglovePrimitiveSchema } from "./types"; const Color: FoxgloveMessageSchema = { type: "message", @@ -1464,8 +1464,8 @@ const Time: FoxglovePrimitiveSchema = { type: { type: "primitive", name: "float64" }, description: "nanoseconds", }, - ] -} + ], +}; const Duration: FoxglovePrimitiveSchema = { type: "primitive", @@ -1482,15 +1482,14 @@ const Duration: FoxglovePrimitiveSchema = { type: { type: "primitive", name: "float64" }, description: "nanoseconds", }, - ] -} + ], +}; export const foxglovePrimitiveSchemas = { - time: Time, - duration: Duration, + Time, + Duration, }; - export const foxgloveMessageSchemas = { ArrowPrimitive, CameraCalibration, diff --git a/internal/types.ts b/internal/types.ts index 1b11442..73bd4fd 100644 --- a/internal/types.ts +++ b/internal/types.ts @@ -12,7 +12,7 @@ export type FoxglovePrimitiveSchema = { name: string; description: string; fields: ReadonlyArray; -} +}; export type FoxgloveEnumSchema = { type: "enum"; diff --git a/scripts/updateGeneratedFiles.ts b/scripts/updateGeneratedFiles.ts index 7541634..97792f2 100644 --- a/scripts/updateGeneratedFiles.ts +++ b/scripts/updateGeneratedFiles.ts @@ -5,17 +5,16 @@ import rimraf from "rimraf"; import { generateRosMsg, generateRosMsgDefinition } from "../internal"; import { exportTypeScriptSchemas } from "../internal/exportTypeScriptSchemas"; -import { - BYTE_VECTOR_FB, - DURATION_FB, - TIME_FB, - generateFlatbuffers, -} from "../internal/generateFlatbufferSchema"; +import { BYTE_VECTOR_FB, generateFlatbuffers } from "../internal/generateFlatbufferSchema"; import { generateJsonSchema } from "../internal/generateJsonSchema"; import { generateMarkdown } from "../internal/generateMarkdown"; -import { DURATION_IDL, TIME_IDL, generateOmgIdl } from "../internal/generateOmgIdl"; +import { generateOmgIdl } from "../internal/generateOmgIdl"; import { generateProto } from "../internal/generateProto"; -import { foxgloveEnumSchemas, foxgloveMessageSchemas } from "../internal/schemas"; +import { + foxgloveEnumSchemas, + foxgloveMessageSchemas, + foxglovePrimitiveSchemas, +} from "../internal/schemas"; async function logProgress(message: string, body: () => Promise) { process.stderr.write(`${message}... `); @@ -85,8 +84,13 @@ async function main({ outDir, rosOutDir }: { outDir: string; rosOutDir: string } await logProgress("Generating FlatBuffer definitions", async () => { await fs.mkdir(path.join(outDir, "flatbuffer"), { recursive: true }); await fs.writeFile(path.join(outDir, "flatbuffer", "ByteVector.fbs"), BYTE_VECTOR_FB); - await fs.writeFile(path.join(outDir, "flatbuffer", "Time.fbs"), TIME_FB); - await fs.writeFile(path.join(outDir, "flatbuffer", "Duration.fbs"), DURATION_FB); + + for (const schema of Object.values(foxglovePrimitiveSchemas)) { + await fs.writeFile( + path.join(outDir, "flatbuffer", `${schema.name}.fbs`), + generateFlatbuffers(schema, []), + ); + } for (const schema of Object.values(foxgloveMessageSchemas)) { // want enums with their corresponding parent tables for usage @@ -110,8 +114,14 @@ async function main({ outDir, rosOutDir }: { outDir: string; rosOutDir: string } await logProgress("Generating OMG IDL definitions", async () => { await fs.mkdir(path.join(outDir, "omgidl", "foxglove"), { recursive: true }); - await fs.writeFile(path.join(outDir, "omgidl", "foxglove", "Time.idl"), TIME_IDL); - await fs.writeFile(path.join(outDir, "omgidl", "foxglove", "Duration.idl"), DURATION_IDL); + + for (const schema of Object.values(foxglovePrimitiveSchemas)) { + await fs.writeFile( + path.join(outDir, "omgidl", "foxglove", `${schema.name}.idl`), + generateOmgIdl(schema), + ); + } + for (const schema of Object.values(foxgloveMessageSchemas)) { await fs.writeFile( path.join(outDir, "omgidl", "foxglove", `${schema.name}.idl`),