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

Add Time type to exports for typescript types #131

Closed
wants to merge 3 commits into from
Closed
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
15 changes: 6 additions & 9 deletions internal/exportTypeScriptSchemas.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import {
DURATION_TS,
GenerateTypeScriptOptions,
TIME_TS,
generateTypeScript,
} from "./generateTypeScript";
import { foxgloveEnumSchemas, foxgloveMessageSchemas } from "./schemas";
import { GenerateTypeScriptOptions, generateTypeScript } from "./generateTypeScript";
import { foxgloveEnumSchemas, foxgloveMessageSchemas, foxglovePrimitiveSchemas } from "./schemas";

/**
* Export schemas as TypeScript source code, keyed by the file base name (without `.ts` suffix).
Expand All @@ -24,12 +19,14 @@ 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),
...Object.values(foxgloveEnumSchemas),
...Object.values(foxglovePrimitiveSchemas),
].sort((a, b) => a.name.localeCompare(b.name));
let indexTS = "";
for (const schema of allSchemaNames) {
Expand Down
11 changes: 11 additions & 0 deletions internal/generateFlatbufferSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ export function generateFlatbuffers(
let definition;
const imports = new Set<string>();
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) {
Expand Down
14 changes: 14 additions & 0 deletions internal/generateOmgIdl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ? "" : ",";
Expand Down
13 changes: 13 additions & 0 deletions internal/generateTypeScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
43 changes: 42 additions & 1 deletion internal/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FoxgloveEnumSchema, FoxgloveMessageSchema } from "./types";
import { FoxgloveEnumSchema, FoxgloveMessageSchema, FoxglovePrimitiveSchema } from "./types";

const Color: FoxgloveMessageSchema = {
type: "message",
Expand Down Expand Up @@ -1449,6 +1449,47 @@
],
};

const Time: FoxglovePrimitiveSchema = {
type: "primitive",
name: "time",
description: "Timestamp in the format of second and nanoseconds since epoch",
fields: [
{

Check failure on line 1457 in internal/schemas.ts

View workflow job for this annotation

GitHub Actions / build

Type '{ name: string; type: { type: string; name: string; }; description: string; }' is not assignable to type 'FoxglovePrimitive'.
name: "sec",
type: { type: "primitive", name: "float64" },
description: "Seconds since epoch",
},
{

Check failure on line 1462 in internal/schemas.ts

View workflow job for this annotation

GitHub Actions / build

Type '{ name: string; type: { type: string; name: string; }; description: string; }' is not assignable to type 'FoxglovePrimitive'.
name: "nsec",
type: { type: "primitive", name: "float64" },
description: "nanoseconds",
},
],
};

const Duration: FoxglovePrimitiveSchema = {
type: "primitive",
name: "time",
description: "Difference between two timestamps",
fields: [
{

Check failure on line 1475 in internal/schemas.ts

View workflow job for this annotation

GitHub Actions / build

Type '{ name: string; type: { type: string; name: string; }; description: string; }' is not assignable to type 'FoxglovePrimitive'.
name: "sec",
type: { type: "primitive", name: "float64" },
description: "Seconds since epoch",
},
{

Check failure on line 1480 in internal/schemas.ts

View workflow job for this annotation

GitHub Actions / build

Type '{ name: string; type: { type: string; name: string; }; description: string; }' is not assignable to type 'FoxglovePrimitive'.
name: "nsec",
type: { type: "primitive", name: "float64" },
description: "nanoseconds",
},
],
};

export const foxglovePrimitiveSchemas = {
Time,
Duration,
};

export const foxgloveMessageSchemas = {
ArrowPrimitive,
CameraCalibration,
Expand Down
9 changes: 8 additions & 1 deletion internal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ export type FoxglovePrimitive =
| "time"
| "duration";

export type FoxglovePrimitiveSchema = {
type: "primitive";
name: string;
description: string;
fields: ReadonlyArray<FoxglovePrimitive>;
};

export type FoxgloveEnumSchema = {
type: "enum";
name: string;
Expand Down Expand Up @@ -41,4 +48,4 @@ export type FoxgloveMessageSchema = {
fields: ReadonlyArray<FoxgloveMessageField>;
};

export type FoxgloveSchema = FoxgloveMessageSchema | FoxgloveEnumSchema;
export type FoxgloveSchema = FoxgloveMessageSchema | FoxgloveEnumSchema | FoxglovePrimitiveSchema;
34 changes: 22 additions & 12 deletions scripts/updateGeneratedFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>) {
process.stderr.write(`${message}... `);
Expand Down Expand Up @@ -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
Expand All @@ -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`),
Expand Down
Loading