Skip to content

Commit

Permalink
tree: Remove SchemaBuilder (microsoft#22317)
Browse files Browse the repository at this point in the history
## Description

Remove SchemaBuilder and its usages.

Note that this does not (yet) remove SchemaBuilderBase or
SchemaBuilderInternal.
  • Loading branch information
CraigMacomber authored Aug 27, 2024
1 parent 49849bb commit 0432f6f
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 295 deletions.
1 change: 0 additions & 1 deletion packages/dds/tree/src/domains/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* Licensed under the MIT License.
*/

export { SchemaBuilder } from "./schemaBuilder.js";
export {
cursorToJsonObject,
jsonArray,
Expand Down
82 changes: 0 additions & 82 deletions packages/dds/tree/src/domains/schemaBuilder.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,3 @@ export abstract class LazyEntity<TSchema = unknown, TAnchor = unknown>
*/
protected abstract [forgetAnchorSymbol](): void;
}

/**
* Prevent Entities from inheriting members from Object.prototype including:
* '__defineGetter__', '__defineSetter__', '__lookupGetter__', '__lookupSetter__', '__proto__',
* 'hasOwnProperty', 'isPrototypeOf', 'valueOf', 'propertyIsEnumerable', 'toLocaleString' and 'toString'.
*
* This opens up more options for field names on struct nodes.
*/
Object.setPrototypeOf(LazyEntity.prototype, null);
37 changes: 5 additions & 32 deletions packages/dds/tree/src/feature-libraries/flex-tree/lazyNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
type FieldKey,
type ITreeSubscriptionCursor,
type TreeNavigationResult,
type TreeNodeSchemaIdentifier,
type Value,
inCursorField,
mapCursorFields,
Expand Down Expand Up @@ -55,7 +54,7 @@ export function makeTree(context: Context, cursor: ITreeSubscriptionCursor): Laz
return cached as LazyTreeNode;
}
const schema = context.schema.nodeSchema.get(cursor.type) ?? fail("missing schema");
return buildSubclass(context, schema, cursor, anchorNode, anchor);
return new LazyTreeNode(context, schema, cursor, anchorNode, anchor);
}

function cleanupTree(anchor: AnchorNode): void {
Expand All @@ -64,19 +63,6 @@ function cleanupTree(anchor: AnchorNode): void {
cached[disposeSymbol]();
}

function buildSubclass(
context: Context,
schema: FlexTreeNodeSchema,
cursor: ITreeSubscriptionCursor,
anchorNode: AnchorNode,
anchor: Anchor,
): LazyTreeNode {
return new LazyTreeNode(context, schema, cursor, anchorNode, anchor);

// TODO: there should be a common fallback that works for cases without a specialized implementation.
fail("unrecognized node kind");
}

/**
* Lazy implementation of {@link FlexTreeNode}.
*/
Expand All @@ -87,10 +73,6 @@ export class LazyTreeNode<TSchema extends FlexTreeNodeSchema = FlexTreeNodeSchem
public get [flexTreeMarker](): FlexTreeEntityKind.Node {
return FlexTreeEntityKind.Node;
}
/**
* Enumerable own property providing a more JS object friendly alternative to "schema".
*/
public readonly type: TreeNodeSchemaIdentifier;

// Using JS private here prevents it from showing up as a enumerable own property, or conflicting with struct fields.
readonly #removeDeleteCallback: () => void;
Expand All @@ -111,8 +93,6 @@ export class LazyTreeNode<TSchema extends FlexTreeNodeSchema = FlexTreeNodeSchem
this.context.schema.nodeSchema.get(this.schema.name) !== undefined,
0x784 /* There is no explicit schema for this node type. Ensure that the type is correct and the schema for it was added to the TreeStoredSchema */,
);

this.type = schema.name;
}

public is(schema: FlexTreeNodeSchema): boolean {
Expand Down Expand Up @@ -153,7 +133,10 @@ export class LazyTreeNode<TSchema extends FlexTreeNodeSchema = FlexTreeNodeSchem
}

public getBoxed(key: FieldKey): FlexTreeField {
return getBoxedField(this, key, this.schema.getFieldSchema(key));
const fieldSchema = this.schema.getFieldSchema(key);
return inCursorField(this[cursorSymbol], key, (cursor) => {
return makeField(this.context, fieldSchema, cursor);
});
}

public boxedIterator(): IterableIterator<FlexTreeField> {
Expand Down Expand Up @@ -216,13 +199,3 @@ export class LazyTreeNode<TSchema extends FlexTreeNodeSchema = FlexTreeNodeSchem
return mapCursorFields(this[cursorSymbol], (cursor) => cursor.getFieldKey()).values();
}
}

function getBoxedField(
objectNode: LazyTreeNode,
key: FieldKey,
fieldSchema: FlexFieldSchema,
): FlexTreeField {
return inCursorField(objectNode[cursorSymbol], key, (cursor) => {
return makeField(objectNode.context, fieldSchema, cursor);
});
}
1 change: 1 addition & 0 deletions packages/dds/tree/src/simple-tree/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,4 @@ export {
} from "./jsonSchema.js";
export { getJsonSchema } from "./getJsonSchema.js";
export { getSimpleSchema } from "./getSimpleSchema.js";
export { toStoredSchema, getStoredSchema } from "./toFlexSchema.js";
10 changes: 10 additions & 0 deletions packages/dds/tree/src/simple-tree/toFlexSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { UsageError } from "@fluidframework/telemetry-utils/internal";
import {
EmptyKey,
type TreeNodeSchemaIdentifier,
type TreeNodeStoredSchema,
type TreeStoredSchema,
} from "../core/index.js";
import {
Expand Down Expand Up @@ -105,6 +106,15 @@ export function getFlexSchema(root: TreeNodeSchema): FlexTreeNodeSchema {
return treeSchema.rootFieldSchema.monomorphicChildType ?? fail("root should be monomorphic");
}

/**
* Return a stored schema for the provided class schema.
*
* This also has the side effect of populating the cached view schema on the class based schema.
*/
export function getStoredSchema(root: TreeNodeSchema): TreeNodeStoredSchema {
return getFlexSchema(root).stored;
}

/**
* Normalizes an {@link ImplicitFieldSchema} into a {@link TreeFieldSchema}.
*/
Expand Down
19 changes: 0 additions & 19 deletions packages/dds/tree/src/test/domains/schemaBuilder.spec.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
type Value,
mapCursorField,
} from "../../../core/index.js";
import { SchemaBuilder, jsonObject, leaf } from "../../../domains/index.js";
import { jsonObject, leaf } from "../../../domains/index.js";
// eslint-disable-next-line import/no-internal-modules
import { BasicChunk } from "../../../feature-libraries/chunked-forest/basicChunk.js";
// eslint-disable-next-line import/no-internal-modules
Expand All @@ -38,7 +38,6 @@ import {
cursorForJsonableTreeField,
cursorForJsonableTreeNode,
defaultSchemaPolicy,
intoStoredSchemaCollection,
jsonableTreeFromCursor,
} from "../../../feature-libraries/index.js";
import { brand } from "../../../util/index.js";
Expand All @@ -49,15 +48,16 @@ import {
numberSequenceField,
} from "./fieldCursorTestUtilities.js";
import { polygonTree, testData } from "./uniformChunkTestData.js";
import { SchemaFactory, toStoredSchema } from "../../../simple-tree/index.js";

const builder = new SchemaBuilder({ scope: "chunkTree" });
const builder = new SchemaFactory("chunkTree");
const empty = builder.object("empty", {});
const valueField = SchemaBuilder.required(leaf.number);
const valueField = builder.required(builder.number);
const structValue = builder.object("structValue", { x: valueField });
const optionalField = builder.optional(leaf.number);
const optionalField = builder.optional(builder.number);
const structOptional = builder.object("structOptional", { x: optionalField });
const schemaView = builder.intoLibrary();
const schema = intoStoredSchemaCollection(schemaView);

const schema = toStoredSchema([empty, builder.number, structValue, structOptional]);

function expectEqual(a: ShapeInfo, b: ShapeInfo): void {
assert.deepEqual(a, b);
Expand Down Expand Up @@ -288,19 +288,24 @@ describe("chunkTree", () => {
expectEqual(info, new TreeShape(leaf.number.name, true, []));
});
it("empty", () => {
const info = tryShapeFromSchema(schema, defaultSchemaPolicy, empty.name, new Map());
expectEqual(info, new TreeShape(empty.name, false, []));
const info = tryShapeFromSchema(
schema,
defaultSchemaPolicy,
brand(empty.identifier),
new Map(),
);
expectEqual(info, new TreeShape(brand(empty.identifier), false, []));
});
it("structValue", () => {
const info = tryShapeFromSchema(
schema,
defaultSchemaPolicy,
structValue.name,
brand(structValue.identifier),
new Map(),
);
expectEqual(
info,
new TreeShape(structValue.name, false, [
new TreeShape(brand(structValue.identifier), false, [
[brand("x"), new TreeShape(leaf.number.name, true, []), 1],
]),
);
Expand All @@ -309,7 +314,7 @@ describe("chunkTree", () => {
const info = tryShapeFromSchema(
schema,
defaultSchemaPolicy,
structOptional.name,
brand(structOptional.identifier),
new Map(),
);
expectEqual(info, polymorphic);
Expand All @@ -321,7 +326,7 @@ describe("chunkTree", () => {
const info = tryShapeFromFieldSchema(
schema,
defaultSchemaPolicy,
valueField.stored,
toStoredSchema(valueField).rootFieldSchema,
brand("key"),
new Map(),
);
Expand All @@ -331,7 +336,7 @@ describe("chunkTree", () => {
const info = tryShapeFromFieldSchema(
schema,
defaultSchemaPolicy,
optionalField.stored,
toStoredSchema(optionalField).rootFieldSchema,
brand("key"),
new Map(),
);
Expand Down
Loading

0 comments on commit 0432f6f

Please sign in to comment.