Skip to content

Commit

Permalink
InferredView fixes and enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
imanjra authored and benjaminpkane committed Jan 5, 2024
1 parent 4470ad8 commit dd8118d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { generateSchema } from "../utils";
import DynamicIO from "./DynamicIO";

export default function InferredView(props) {
const { data, schema } = props;
const readOnly = schema?.readOnly || schema?.view?.readOnly;
const generatedSchema = generateSchema(data || schema?.default, {
label: schema?.view?.label,
readOnly,
const { schema = {} } = props;
const { view = {}, default: defaultValue, readOnly } = schema;
const generatedSchema = generateSchema(defaultValue, {
label: view.label,
readOnly: readOnly || view.readOnly,
});

return <DynamicIO {...props} schema={generatedSchema} />;
const schemaWithDefault = { ...generatedSchema, default: defaultValue };
return <DynamicIO {...props} schema={schemaWithDefault} />;
}
22 changes: 15 additions & 7 deletions app/packages/core/src/plugins/SchemaIO/utils/generate-schema.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Auto generate SchemaIO compatible schema based on a value

import { isPrimitiveType } from "@fiftyone/utilities";
import { isNullish, isPrimitiveType } from "@fiftyone/utilities";

// todo: add support for OneOfView, TupleView, and MapView
export function generateSchema(value, options?) {
export function generateSchema(value: any, options?: GenerateSchemaOptions) {
const type = getType(value);
const { label, readOnly } = options;
const { label, readOnly } = options || {};
if (type === "array") {
const dominantType = getDominantType(value);
const isValueTable = isTable(value);
Expand All @@ -16,10 +16,9 @@ export function generateSchema(value, options?) {
label,
component: "TableView",
columns: getTableColumns(value),
readOnly: true,
},
};
} else if (isPrimitiveType(dominantType) && readOnly) {
} else if (isPrimitiveType(dominantType || "") && readOnly) {
return {
type,
view: {
Expand Down Expand Up @@ -53,7 +52,7 @@ export function generateSchema(value, options?) {
readOnly,
},
};
} else {
} else if (isPrimitiveType(type || "")) {
return {
type,
view: {
Expand All @@ -66,12 +65,16 @@ export function generateSchema(value, options?) {
readOnly,
},
};
} else {
// todo: improve unknown type - TupleView? CodeView?
return { type, view: { label, component: "UnsupportedView", readOnly } };
}
}

function getType(value) {
if (value !== undefined || value !== null)
if (!isNullish(value)) {
return Array.isArray(value) ? "array" : typeof value;
}
}

function getDominantType(array) {
Expand Down Expand Up @@ -119,3 +122,8 @@ function getTableColumns(array) {
const [firstItem] = array;
return Object.keys(firstItem).map((key) => ({ key, label: key }));
}

type GenerateSchemaOptions = {
label?: string;
readOnly?: boolean;
};

0 comments on commit dd8118d

Please sign in to comment.