Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

chore: Convert metabase-lib/v1 field-value utils to TypeScript (#79075)

Andrei Chernykhecbacf97205e
diff --git a/frontend/src/metabase-lib/v1/queries/utils/card.js b/frontend/src/metabase-lib/v1/queries/utils/card.js
deleted file mode 100644
index e69de29bb2d..00000000000
diff --git a/frontend/src/metabase-lib/v1/queries/utils/field.js b/frontend/src/metabase-lib/v1/queries/utils/field.js
deleted file mode 100644
index 91d5fdce347..00000000000
--- a/frontend/src/metabase-lib/v1/queries/utils/field.js
+++ /dev/null
@@ -1,37 +0,0 @@
-import _ from "underscore";
-
-// Metadata field "values" type is inconsistent
-// https://github.com/metabase/metabase/issues/3417
-export function getFieldValues(field) {
- const values = field && field.values;
- if (Array.isArray(values)) {
- if (values.length === 0 || Array.isArray(values[0])) {
- return values;
- } else {
- // console.warn("deprecated field values array!", values);
- return values.map((value) => [value]);
- }
- } else if (values && Array.isArray(values.values)) {
- // console.warn("deprecated field values object!", values);
-
- if (Array.isArray(values.human_readable_values)) {
- return _.zip(values.values, values.human_readable_values || {});
- } else if (Array.isArray(values.values)) {
- // TODO Atte Keinänen 7/12/17: I don't honestly know why we can have a field in `values` property.
- return getFieldValues(values);
- } else {
- // console.warn("missing field values", field);
- return [];
- }
- } else {
- // console.warn("missing field values", field);
- return [];
- }
-}
-
-// merge field values and remappings
-export function getRemappings(field) {
- const remappings = (field && field.remappings) || [];
- const fieldValues = getFieldValues(field);
- return [...fieldValues, ...remappings];
-}
diff --git a/frontend/src/metabase-lib/v1/queries/utils/field.ts b/frontend/src/metabase-lib/v1/queries/utils/field.ts
new file mode 100644
index 00000000000..e51fd9d6c4c
--- /dev/null
+++ b/frontend/src/metabase-lib/v1/queries/utils/field.ts
@@ -0,0 +1,62 @@
+import _ from "underscore";
+
+import type { FieldValue, RowValue } from "metabase-types/api";
+
+type RawFieldValuesObject = {
+ values?: RowValue[];
+ human_readable_values?: string[];
+};
+
+type RawFieldValues = RowValue[] | FieldValue[] | RawFieldValuesObject;
+
+type RawField = {
+ values?: RawFieldValues;
+ remappings?: FieldValue[];
+};
+
+// Already [[value], [value, label], ...] tuples vs deprecated flat scalars; an array-valued
+// first row is ambiguous either way (metabase#3417).
+function isFieldValueTuples(
+ values: RowValue[] | FieldValue[],
+): values is FieldValue[] {
+ return values.length === 0 || Array.isArray(values[0]);
+}
+
+// Metadata field "values" type is inconsistent
+// https://github.com/metabase/metabase/issues/3417
+export function getFieldValues(field?: RawField | null): FieldValue[] {
+ const values = field && field.values;
+
+ if (Array.isArray(values)) {
+ if (isFieldValueTuples(values)) {
+ return values;
+ }
+
+ return values.map((value): FieldValue => [value]);
+ }
+
+ if (values && Array.isArray(values.values)) {
+ if (Array.isArray(values.human_readable_values)) {
+ return _.zip(values.values, values.human_readable_values || {});
+ }
+
+ // TODO Atte Keinänen 7/12/17: I don't honestly know why we can have a field in `values` property.
+ return getFieldValues(values);
+ }
+
+ return [];
+}
+
+/**
+ * Merge field values and remappings
+ */
+export function getRemappings(
+ field?: RawField | null,
+): [RowValue, string | undefined][] {
+ const remappings = (field && field.remappings) || [];
+ const fieldValues = getFieldValues(field);
+ return [...fieldValues, ...remappings].map(([value, label]) => [
+ value,
+ label,
+ ]);
+}
diff --git a/frontend/src/metabase-lib/v1/queries/utils/field.unit.spec.ts b/frontend/src/metabase-lib/v1/queries/utils/field.unit.spec.ts
new file mode 100644
index 00000000000..60f03c114ec
--- /dev/null
+++ b/frontend/src/metabase-lib/v1/queries/utils/field.unit.spec.ts
@@ -0,0 +1,81 @@
+import type { FieldValue } from "metabase-types/api";
+
+import { getFieldValues, getRemappings } from "./field";
+
+describe("queries/utils/field", () => {
+ describe("getFieldValues", () => {
+ it("returns [value, label] tuples as-is", () => {
+ const values: FieldValue[] = [
+ [1, "One"],
+ [2, "Two"],
+ ];
+
+ expect(getFieldValues({ values })).toEqual(values);
+ });
+
+ it("keeps tuples without labels", () => {
+ const values: FieldValue[] = [[1, "One"], [2]];
+
+ expect(getFieldValues({ values })).toEqual(values);
+ });
+
+ it("wraps deprecated flat values into 1-tuples", () => {
+ expect(getFieldValues({ values: [1, 2, 3] })).toEqual([[1], [2], [3]]);
+ });
+
+ it("zips deprecated { values, human_readable_values } objects into tuples", () => {
+ expect(
+ getFieldValues({
+ values: { values: [1, 2], human_readable_values: ["One", "Two"] },
+ }),
+ ).toEqual([
+ [1, "One"],
+ [2, "Two"],
+ ]);
+ });
+
+ it("wraps deprecated { values } objects without labels into 1-tuples", () => {
+ expect(getFieldValues({ values: { values: [1, 2] } })).toEqual([
+ [1],
+ [2],
+ ]);
+ });
+
+ it("returns an empty list when values are missing or empty", () => {
+ expect(getFieldValues()).toEqual([]);
+ expect(getFieldValues(null)).toEqual([]);
+ expect(getFieldValues({})).toEqual([]);
+ expect(getFieldValues({ values: [] })).toEqual([]);
+ expect(getFieldValues({ values: {} })).toEqual([]);
+ });
+ });
+
+ describe("getRemappings", () => {
+ it("pads unlabeled values to [value, undefined] pairs", () => {
+ expect(getRemappings({ values: [[1], [2, "Two"]] })).toStrictEqual([
+ [1, undefined],
+ [2, "Two"],
+ ]);
+ });
+
+ it("appends remappings after field values", () => {
+ expect(
+ getRemappings({
+ values: [[1, "One"]],
+ remappings: [[2, "Two"]],
+ }),
+ ).toEqual([
+ [1, "One"],
+ [2, "Two"],
+ ]);
+ });
+
+ it("returns only remappings when field values are missing", () => {
+ expect(getRemappings({ remappings: [[1, "One"]] })).toEqual([[1, "One"]]);
+ });
+
+ it("returns an empty list for a missing field", () => {
+ expect(getRemappings()).toEqual([]);
+ });
+ });
+});
diff --git a/frontend/src/metabase/metadata/components/FieldSection/BehaviorSection/RemappingPicker/CustomMappingModal/CustomMappingModal.tsx b/frontend/src/metabase/metadata/components/FieldSection/BehaviorSection/RemappingPicker/CustomMappingModal/CustomMappingModal.tsx
index 053087b4414..55efbb82e3d 100644
--- a/frontend/src/metabase/metadata/components/FieldSection/BehaviorSection/RemappingPicker/CustomMappingModal/CustomMappingModal.tsx
+++ b/frontend/src/metabase/metadata/components/FieldSection/BehaviorSection/RemappingPicker/CustomMappingModal/CustomMappingModal.tsx
@@ -5,7 +5,7 @@ import { t } from "ttag";
import { Box, Button, Group, Modal, Text, TextInput } from "metabase/ui";
import S from "./CustomMappingModal.module.css";
-import type { ChangeOptions, Mapping } from "./types";
+import type { ChangeOptions, DraftMapping, Mapping } from "./types";
import {
areMappingsEqual,
fillMissingMappings,
@@ -14,7 +14,7 @@ import {
interface Props {
isOpen: boolean;
- value: Mapping;
+ value: DraftMapping;
onChange: (value: Mapping, options?: ChangeOptions) => void;
onClose: () => void;
}
@@ -108,7 +108,7 @@ export const CustomMappingModal = ({
key={index}
>
<Box component="td" p="sm" pl="xl">
- {original}
+ {String(original)}
</Box>
<Box component="td" p="sm" pr="xl">
diff --git a/frontend/src/metabase/metadata/components/FieldSection/BehaviorSection/RemappingPicker/CustomMappingModal/CustomMappingModal.unit.spec.tsx b/frontend/src/metabase/metadata/components/FieldSection/BehaviorSection/RemappingPicker/CustomMappingModal/CustomMappingModal.unit.spec.tsx
index bdc493bafcd..b3b09951a79 100644
--- a/frontend/src/metabase/metadata/components/FieldSection/BehaviorSection/RemappingPicker/CustomMappingModal/CustomMappingModal.unit.spec.tsx
+++ b/frontend/src/metabase/metadata/components/FieldSection/BehaviorSection/RemappingPicker/CustomMappingModal/CustomMappingModal.unit.spec.tsx
@@ -9,7 +9,7 @@ import {
} from "__support__/ui";
import { CustomMappingModal } from "./CustomMappingModal";
-import type { Mapping } from "./types";
+import type { DraftMapping, Mapping } from "./types";
const setup = ({
isOpen = true,
@@ -18,7 +18,7 @@ const setup = ({
onClose = jest.fn(),
}: {
isOpen?: boolean;
- value?: Mapping;
+ value?: DraftMapping;
onChange?: (value: Mapping) => void;
onClose?: () => void;
} = {}) => {
@@ -165,11 +165,10 @@ describe("CustomMappingModal", () => {
it("fills missing mappings with original values as strings", async () => {
const onChange = jest.fn();
setup({
- // Unjustified type cast. FIXME
- value: new Map([
+ value: new Map<number | null, string | undefined>([
[1, undefined],
[null, undefined],
- ]) as unknown as Mapping,
+ ]),
onChange,
});
diff --git a/frontend/src/metabase/metadata/components/FieldSection/BehaviorSection/RemappingPicker/CustomMappingModal/types.ts b/frontend/src/metabase/metadata/components/FieldSection/BehaviorSection/RemappingPicker/CustomMappingModal/types.ts
index 28a7244dde3..a79a71a7026 100644
--- a/frontend/src/metabase/metadata/components/FieldSection/BehaviorSection/RemappingPicker/CustomMappingModal/types.ts
+++ b/frontend/src/metabase/metadata/components/FieldSection/BehaviorSection/RemappingPicker/CustomMappingModal/types.ts
@@ -1,4 +1,9 @@
-export type Mapping = Map<number, string>;
+import type { RowValue } from "metabase-types/api";
+
+// Editor seed: labels are unset until filled by fillMissingMappings.
+export type DraftMapping = Map<RowValue, string | undefined>;
+
+export type Mapping = Map<RowValue, string>;
export interface ChangeOptions {
isAutomatic?: boolean;
diff --git a/frontend/src/metabase/metadata/components/FieldSection/BehaviorSection/RemappingPicker/CustomMappingModal/utils.ts b/frontend/src/metabase/metadata/components/FieldSection/BehaviorSection/RemappingPicker/CustomMappingModal/utils.ts
index 151b351f343..611305cf0fe 100644
--- a/frontend/src/metabase/metadata/components/FieldSection/BehaviorSection/RemappingPicker/CustomMappingModal/utils.ts
+++ b/frontend/src/metabase/metadata/components/FieldSection/BehaviorSection/RemappingPicker/CustomMappingModal/utils.ts
@@ -1,4 +1,4 @@
-import type { Mapping } from "./types";
+import type { DraftMapping, Mapping } from "./types";
export function areMappingsEqual(a: Mapping, b: Mapping): boolean {
return (
@@ -6,7 +6,7 @@ export function areMappingsEqual(a: Mapping, b: Mapping): boolean {
);
}
-export function fillMissingMappings(mappings: Mapping): Mapping {
+export function fillMissingMappings(mappings: DraftMapping): Mapping {
const remappings = new Map(
[...mappings].map(([original, mappedOrUndefined]) => {
// Use currently the original value as the "default custom mapping" as the current backend implementation
diff --git a/frontend/src/metabase/metadata/components/FieldSection/BehaviorSection/RemappingPicker/RemappingPicker.tsx b/frontend/src/metabase/metadata/components/FieldSection/BehaviorSection/RemappingPicker/RemappingPicker.tsx
index 5064f743100..7e6adce4dd6 100644
--- a/frontend/src/metabase/metadata/components/FieldSection/BehaviorSection/RemappingPicker/RemappingPicker.tsx
+++ b/frontend/src/metabase/metadata/components/FieldSection/BehaviorSection/RemappingPicker/RemappingPicker.tsx
@@ -27,7 +27,8 @@ import {
} from "metabase/ui";
import type MetadataDatabase from "metabase-lib/v1/metadata/Database";
import type MetadataTable from "metabase-lib/v1/metadata/Table";
-import type { Database, Field, FieldId } from "metabase-types/api";
+import { getRemappings } from "metabase-lib/v1/queries/utils/field";
+import type { Database, Field, FieldId, FieldValue } from "metabase-types/api";
import {
type ChangeOptions,
@@ -41,7 +42,6 @@ import {
import { NamingTip } from "./NamingTip";
import SubInputIllustration from "./illustrations/sub-input.svg?component";
import {
- getFieldRemappedValues,
getFkTargetTableEntityNameOrNull,
getOptions,
getValue,
@@ -106,7 +106,7 @@ export const RemappingPicker = ({
return getOptions(field, fieldValues?.values, fkTargetTable);
}, [field, fieldValues, fkTargetTable]);
const mapping = useMemo(() => {
- return getFieldRemappedValues(fieldValues?.values);
+ return new Map(getRemappings({ values: fieldValues?.values }));
}, [fieldValues?.values]);
const isFkMapping = value === "foreign" || isChoosingInitialFkTarget;
@@ -240,7 +240,11 @@ export const RemappingPicker = ({
async () => {
const { error } = await updateFieldValues({
id,
- values: Array.from(mapping),
+ values: Array.from(
+ mapping,
+ ([key, label]): FieldValue =>
+ label == null ? [key] : [key, label],
+ ),
});
sendUndoToast(error);
diff --git a/frontend/src/metabase/metadata/components/FieldSection/BehaviorSection/RemappingPicker/utils.ts b/frontend/src/metabase/metadata/components/FieldSection/BehaviorSection/RemappingPicker/utils.ts
index b12de58246e..8e48302476a 100644
--- a/frontend/src/metabase/metadata/components/FieldSection/BehaviorSection/RemappingPicker/utils.ts
+++ b/frontend/src/metabase/metadata/components/FieldSection/BehaviorSection/RemappingPicker/utils.ts
@@ -69,7 +69,7 @@ function getTableFields(table: Table | undefined): Field[] {
function hasMappableNumeralValues(
fieldValues: FieldValue[] | undefined,
): boolean {
- const remapping = getFieldRemappedValues(fieldValues);
+ const remapping = new Map(getRemappings({ values: fieldValues }));
// Only show the "custom" option if we have some values that can be mapped to user-defined custom values
// (for a field without user-defined remappings, every key of `field.remappings` has value `undefined`)
@@ -81,12 +81,6 @@ function hasMappableNumeralValues(
);
}
-export function getFieldRemappedValues(
- fieldValues: FieldValue[] | undefined,
-): Map<number, string> {
- return new Map(getRemappings({ values: fieldValues }));
-}
-
/**
* Adds 4 extra attributes to every Field, so that DataSelector does not break.
* DataSelector component expects metabase-lib/v1/metadata/Field objects (entity framework),
diff --git a/frontend/src/metabase/metadata/components/FieldSection/BehaviorSection/RemappingPicker/utils.unit.spec.ts b/frontend/src/metabase/metadata/components/FieldSection/BehaviorSection/RemappingPicker/utils.unit.spec.ts
new file mode 100644
index 00000000000..48e3a1a7b35
--- /dev/null
+++ b/frontend/src/metabase/metadata/components/FieldSection/BehaviorSection/RemappingPicker/utils.unit.spec.ts
@@ -0,0 +1,119 @@
+import { checkNotNull } from "metabase/utils/types";
+import type { FieldValue } from "metabase-types/api";
+import {
+ createMockField,
+ createMockFieldDimension,
+ createMockTable,
+} from "metabase-types/api/mocks";
+
+import {
+ getFkTargetTableEntityNameOrNull,
+ getOptions,
+ getValue,
+ hydrateTableFields,
+} from "./utils";
+
+describe("RemappingPicker/utils", () => {
+ describe("getOptions", () => {
+ const field = createMockField({ semantic_type: null });
+ const optionsFor = (fieldValues: FieldValue[] | undefined) =>
+ getOptions(field, fieldValues, undefined);
+
+ it("always offers the original value", () => {
+ expect(optionsFor(undefined)).toEqual(["original"]);
+ });
+
+ it("offers custom mapping when every value is numeric or null", () => {
+ expect(optionsFor([[1], [2], [3]])).toContain("custom");
+ expect(optionsFor([[1], [null]])).toContain("custom");
+ });
+
+ it("does not offer custom mapping for non-numeric or missing values", () => {
+ expect(optionsFor([["a"], ["b"]])).not.toContain("custom");
+ expect(optionsFor([[1], ["a"]])).not.toContain("custom");
+ expect(optionsFor([])).not.toContain("custom");
+ });
+
+ it("offers foreign key mapping only for FK fields with a target table", () => {
+ const fkField = createMockField({ semantic_type: "type/FK" });
+ const targetTable = createMockTable({ fields: [createMockField()] });
+
+ expect(getOptions(fkField, undefined, targetTable)).toContain("foreign");
+ expect(getOptions(fkField, undefined, undefined)).not.toContain(
+ "foreign",
+ );
+ expect(getOptions(field, undefined, targetTable)).not.toContain(
+ "foreign",
+ );
+ });
+ });
+
+ describe("getValue", () => {
+ it("returns 'original' for a field without dimensions", () => {
+ expect(getValue(createMockField())).toBe("original");
+ expect(getValue(createMockField({ dimensions: [] }))).toBe("original");
+ });
+
+ it("returns 'foreign' for an external dimension", () => {
+ const field = createMockField({
+ dimensions: [createMockFieldDimension({ type: "external" })],
+ });
+
+ expect(getValue(field)).toBe("foreign");
+ });
+
+ it("returns 'custom' for an internal dimension", () => {
+ const field = createMockField({
+ dimensions: [createMockFieldDimension({ type: "internal" })],
+ });
+
+ expect(getValue(field)).toBe("custom");
+ });
+ });
+
+ describe("getFkTargetTableEntityNameOrNull", () => {
+ it("returns the id of the target table's entity name field", () => {
+ const table = createMockTable({
+ fields: [
+ createMockField({ id: 3 }),
+ createMockField({ id: 7, semantic_type: "type/Name" }),
+ ],
+ });
+
+ expect(getFkTargetTableEntityNameOrNull(table)).toBe(7);
+ });
+
+ it("returns undefined when there is no entity name field", () => {
+ const table = createMockTable({ fields: [createMockField({ id: 3 })] });
+
+ expect(getFkTargetTableEntityNameOrNull(table)).toBe(undefined);
+ expect(getFkTargetTableEntityNameOrNull(undefined)).toBe(undefined);
+ });
+ });
+
+ describe("hydrateTableFields", () => {
+ it("adds DataSelector compatibility methods to every field", () => {
+ const table = createMockTable({
+ fields: [createMockField({ display_name: "Total" })],
+ });
+
+ const hydratedField = checkNotNull(
+ hydrateTableFields(table)?.fields?.[0],
+ );
+
+ expect(hydratedField).toEqual(
+ expect.objectContaining({
+ display_name: "Total",
+ displayName: expect.any(Function),
+ icon: expect.any(Function),
+ getPlainObject: expect.any(Function),
+ table,
+ }),
+ );
+ });
+
+ it("passes through an undefined table", () => {
+ expect(hydrateTableFields(undefined)).toBe(undefined);
+ });
+ });
+});