Skip to content
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
18 changes: 18 additions & 0 deletions web-common/src/features/entity-management/entity-mappers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@ describe("entity-mappers", () => {
expect(getNameFromFile("/path/to/data/adbids.csv.tgz")).toBe("adbids");
});

it("keeps dots in YAML resource names", () => {
expect(getNameFromFile("/dashboards/dashboard.canvas.yaml")).toBe(
"dashboard.canvas",
);
});

it("keeps dots in YML resource names", () => {
expect(getNameFromFile("/dashboards/dashboard.canvas.yml")).toBe(
"dashboard.canvas",
);
});

it("keeps dots in SQL resource names", () => {
expect(getNameFromFile("/models/orders.latest.sql")).toBe(
"orders.latest",
);
});

it("no folder", () => {
expect(getNameFromFile("adbids.csv")).toBe("adbids");
});
Expand Down
16 changes: 14 additions & 2 deletions web-common/src/features/entity-management/entity-mappers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ResourceKind } from "@rilldata/web-common/features/entity-management/resource-selectors";
import { EntityType } from "@rilldata/web-common/features/entity-management/types";
import { RESOURCE_FILE_EXTENSIONS } from "./file-path-utils";

export function getFilePathFromPagePath(path: string): string {
const pathSplits = path.split("/");
Expand Down Expand Up @@ -64,9 +65,20 @@ export function getFileAPIPathFromNameAndType(
}

export function getNameFromFile(fileName: string): string {
// TODO: do we need a library here?
const splits = fileName.split("/");
const extensionSplits = splits[splits.length - 1]?.split(".");
const basename = splits[splits.length - 1] ?? "";

// Rill resource names are inferred by removing only the final resource file
// extension, so dotted names like `dashboard.canvas.yaml` stay intact.
for (const extension of RESOURCE_FILE_EXTENSIONS) {
if (basename.endsWith(extension)) {
return basename.slice(0, -extension.length);
}
}
Comment on lines +71 to +77

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in bbaa274. I centralized the resource extension list in file-path-utils and made extractFileExtension resource-aware, so dotted resource files return .yaml/.yml/.sql while non-resource data files keep the existing compound-extension behavior. Added regression coverage for dotted resource extension extraction and kind inference.


// Non-resource data files keep the legacy behavior of removing compound
// extensions, e.g. `adbids.csv.tgz` -> `adbids`.
const extensionSplits = basename.split(".");
return extensionSplits[0];
}

Expand Down
12 changes: 10 additions & 2 deletions web-common/src/features/entity-management/file-path-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const FILE_PATH_SPLIT_REGEX = /\//;

export const RESOURCE_FILE_EXTENSIONS = [".yaml", ".yml", ".sql"] as const;

export function extractFileName(filePath: string): string {
let fileName = filePath.split(FILE_PATH_SPLIT_REGEX).slice(-1)[0];
const lastIndexOfDot = fileName.lastIndexOf(".");
Expand All @@ -17,8 +19,14 @@ export function extractFileName(filePath: string): string {

export function extractFileExtension(filePath: string): string {
const fileName = filePath.split(FILE_PATH_SPLIT_REGEX).slice(-1)[0];
const lastIndexOfDot = fileName.indexOf(".");
return lastIndexOfDot >= 0 ? fileName.substring(lastIndexOfDot) : "";

const resourceExtension = RESOURCE_FILE_EXTENSIONS.find((extension) =>
fileName.endsWith(extension),
);
if (resourceExtension) return resourceExtension;

const firstIndexOfDot = fileName.indexOf(".");
return firstIndexOfDot >= 0 ? fileName.substring(firstIndexOfDot) : "";
}

export function splitFolderAndFileName(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ describe("inferResourceName", () => {
`rows:\n type: invalid\ntype: canvas`,
ResourceKind.Canvas,
],
[
"implicit kind for dotted yaml",
"dashboards/dashboard.canvas.yaml",
`type: canvas\nrows: []`,
ResourceKind.Canvas,
],
[
"implicit kind for dotted sql",
"models/orders.latest.sql",
`select * from orders`,
ResourceKind.Model,
],
];

testCases.forEach(([title, path, contents, expected]) => {
Expand Down
3 changes: 3 additions & 0 deletions web-common/src/features/sources/extract-table-name.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ const TestCases = [
"table_v1_parquet",
".v1.parquet.gz",
),
...generateTestCases("dashboard.canvas.yaml", "dashboard_canvas", ".yaml"),
...generateTestCases("dashboard.canvas.yml", "dashboard_canvas", ".yml"),
...generateTestCases("orders.latest.sql", "orders_latest", ".sql"),
];

describe("extract-table-name", () => {
Expand Down
Loading