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 scopedFactory #23987

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions .changeset/grumpy-crabs-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"fluid-framework": minor
"@fluidframework/tree": minor
---
---
"section": tree
---

Add `SchemaFactory.scopedFactory`

Add `SchemaFactory.scopedFactory` method as an easy way to create a new `SchemaFactory` with a nested scope string.
1 change: 1 addition & 0 deletions packages/dds/tree/api-report/tree.alpha.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,7 @@ export class SchemaFactory<out TScope extends string | undefined = string | unde
readonly requiredRecursive: <const T extends unknown>(t: T, props?: Omit<FieldProps<unknown>, "defaultProvider"> | undefined) => FieldSchemaUnsafe<FieldKind.Required, T>;
static readonly requiredRecursive: <const T extends unknown>(t: T, props?: Omit<FieldProps<unknown>, "defaultProvider"> | undefined) => FieldSchemaUnsafe<FieldKind.Required, T>;
readonly scope: TScope;
scopedFactory<const T extends TName, TNameInner extends number | string = string>(name: T): SchemaFactory<ScopedSchemaName<TScope, T>, TNameInner>;
readonly string: TreeNodeSchemaNonClass<"com.fluidframework.leaf.string", NodeKind.Leaf, string, string, true, unknown, never, unknown>;
static readonly string: TreeNodeSchemaNonClass<"com.fluidframework.leaf.string", NodeKind.Leaf, string, string, true, unknown, never, unknown>;
}
Expand Down
1 change: 1 addition & 0 deletions packages/dds/tree/api-report/tree.beta.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ export class SchemaFactory<out TScope extends string | undefined = string | unde
readonly requiredRecursive: <const T extends unknown>(t: T, props?: Omit<FieldProps<unknown>, "defaultProvider"> | undefined) => FieldSchemaUnsafe<FieldKind.Required, T>;
static readonly requiredRecursive: <const T extends unknown>(t: T, props?: Omit<FieldProps<unknown>, "defaultProvider"> | undefined) => FieldSchemaUnsafe<FieldKind.Required, T>;
readonly scope: TScope;
scopedFactory<const T extends TName, TNameInner extends number | string = string>(name: T): SchemaFactory<ScopedSchemaName<TScope, T>, TNameInner>;
readonly string: TreeNodeSchemaNonClass<"com.fluidframework.leaf.string", NodeKind.Leaf, string, string, true, unknown, never, unknown>;
static readonly string: TreeNodeSchemaNonClass<"com.fluidframework.leaf.string", NodeKind.Leaf, string, string, true, unknown, never, unknown>;
}
Expand Down
1 change: 1 addition & 0 deletions packages/dds/tree/api-report/tree.legacy.alpha.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ export class SchemaFactory<out TScope extends string | undefined = string | unde
readonly requiredRecursive: <const T extends unknown>(t: T, props?: Omit<FieldProps<unknown>, "defaultProvider"> | undefined) => FieldSchemaUnsafe<FieldKind.Required, T>;
static readonly requiredRecursive: <const T extends unknown>(t: T, props?: Omit<FieldProps<unknown>, "defaultProvider"> | undefined) => FieldSchemaUnsafe<FieldKind.Required, T>;
readonly scope: TScope;
scopedFactory<const T extends TName, TNameInner extends number | string = string>(name: T): SchemaFactory<ScopedSchemaName<TScope, T>, TNameInner>;
readonly string: TreeNodeSchemaNonClass<"com.fluidframework.leaf.string", NodeKind.Leaf, string, string, true, unknown, never, unknown>;
static readonly string: TreeNodeSchemaNonClass<"com.fluidframework.leaf.string", NodeKind.Leaf, string, string, true, unknown, never, unknown>;
}
Expand Down
1 change: 1 addition & 0 deletions packages/dds/tree/api-report/tree.legacy.public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ export class SchemaFactory<out TScope extends string | undefined = string | unde
readonly requiredRecursive: <const T extends unknown>(t: T, props?: Omit<FieldProps<unknown>, "defaultProvider"> | undefined) => FieldSchemaUnsafe<FieldKind.Required, T>;
static readonly requiredRecursive: <const T extends unknown>(t: T, props?: Omit<FieldProps<unknown>, "defaultProvider"> | undefined) => FieldSchemaUnsafe<FieldKind.Required, T>;
readonly scope: TScope;
scopedFactory<const T extends TName, TNameInner extends number | string = string>(name: T): SchemaFactory<ScopedSchemaName<TScope, T>, TNameInner>;
readonly string: TreeNodeSchemaNonClass<"com.fluidframework.leaf.string", NodeKind.Leaf, string, string, true, unknown, never, unknown>;
static readonly string: TreeNodeSchemaNonClass<"com.fluidframework.leaf.string", NodeKind.Leaf, string, string, true, unknown, never, unknown>;
}
Expand Down
1 change: 1 addition & 0 deletions packages/dds/tree/api-report/tree.public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ export class SchemaFactory<out TScope extends string | undefined = string | unde
readonly requiredRecursive: <const T extends unknown>(t: T, props?: Omit<FieldProps<unknown>, "defaultProvider"> | undefined) => FieldSchemaUnsafe<FieldKind.Required, T>;
static readonly requiredRecursive: <const T extends unknown>(t: T, props?: Omit<FieldProps<unknown>, "defaultProvider"> | undefined) => FieldSchemaUnsafe<FieldKind.Required, T>;
readonly scope: TScope;
scopedFactory<const T extends TName, TNameInner extends number | string = string>(name: T): SchemaFactory<ScopedSchemaName<TScope, T>, TNameInner>;
readonly string: TreeNodeSchemaNonClass<"com.fluidframework.leaf.string", NodeKind.Leaf, string, string, true, unknown, never, unknown>;
static readonly string: TreeNodeSchemaNonClass<"com.fluidframework.leaf.string", NodeKind.Leaf, string, string, true, unknown, never, unknown>;
}
Expand Down
12 changes: 12 additions & 0 deletions packages/dds/tree/src/simple-tree/api/schemaFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,18 @@ export class SchemaFactory<
undefined
>;
}

/**
* Create a {@link SchemaFactory} with a {@link SchemaFactory.scope|scope} which is a combination of this factory's scope and the provided name.
* @remarks
* The main use-case for this is when creating a collection of related schema (for example using a function that creates multiple schema).
* Creating such related schema using a sub-scope helps ensure they won't collide with other schema in the parent scope.
*/
public scopedFactory<const T extends TName, TNameInner extends number | string = string>(
name: T,
): SchemaFactory<ScopedSchemaName<TScope, T>, TNameInner> {
return new SchemaFactory(this.scoped(name));
}
}

export function structuralName<const T extends string>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ describe("schemaCreationUtilities", () => {
it("enumFromStrings - construction tests", () => {
const schemaFactory = new SchemaFactory("com.myApp");

const ModeNodes = enumFromStrings(new SchemaFactory(`${schemaFactory.scope}.Mode`), [
"A",
"B",
"C",
]);
const ModeNodes = enumFromStrings(schemaFactory.scopedFactory("Mode"), ["A", "B", "C"]);
type ModeNodes = TreeNodeFromImplicitAllowedTypes<typeof ModeNodes.schema>;

type I0 = NodeFromSchema<(typeof ModeNodes.schema)[0]>;
Expand Down Expand Up @@ -128,7 +124,7 @@ describe("schemaCreationUtilities", () => {
b = "B",
}
// Define the schema for each member of the enum using a nested scope to group them together.
const ModeNodes = adaptEnum(new SchemaFactory(`${schemaFactory.scope}.Mode`), Mode);
const ModeNodes = adaptEnum(schemaFactory.scopedFactory("Mode"), Mode);
// Defined the types of the nodes which correspond to this the schema.
type ModeNodes = TreeNodeFromImplicitAllowedTypes<typeof ModeNodes.schema>;
// An example schema which has an enum as a child.
Expand Down Expand Up @@ -156,7 +152,7 @@ describe("schemaCreationUtilities", () => {
b = "b",
c = 6.3,
}
const ModeNodes = adaptEnum(new SchemaFactory(`${schemaFactory.scope}.Mode`), Mode);
const ModeNodes = adaptEnum(schemaFactory.scopedFactory("Mode"), Mode);
type ModeNodes = TreeNodeFromImplicitAllowedTypes<typeof ModeNodes.schema>;

const fromEnumValue = ModeNodes(Mode.a);
Expand Down Expand Up @@ -198,7 +194,7 @@ describe("schemaCreationUtilities", () => {
c = "C",
}
// Uses a nested schema factory, as recommended by adaptEnum's docs to ensure that pattern works.
const ModeNodes = adaptEnum(new SchemaFactory(`${schemaFactory.scope}.Mode`), Mode);
const ModeNodes = adaptEnum(schemaFactory.scopedFactory("Mode"), Mode);
type ModeNodes = TreeNodeFromImplicitAllowedTypes<typeof ModeNodes.schema>;

const fromEnumValue = ModeNodes(Mode.a);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,7 @@ export class SchemaFactory<out TScope extends string | undefined = string | unde
readonly requiredRecursive: <const T extends unknown>(t: T, props?: Omit<FieldProps<unknown>, "defaultProvider"> | undefined) => FieldSchemaUnsafe<FieldKind.Required, T>;
static readonly requiredRecursive: <const T extends unknown>(t: T, props?: Omit<FieldProps<unknown>, "defaultProvider"> | undefined) => FieldSchemaUnsafe<FieldKind.Required, T>;
readonly scope: TScope;
scopedFactory<const T extends TName, TNameInner extends number | string = string>(name: T): SchemaFactory<ScopedSchemaName<TScope, T>, TNameInner>;
readonly string: TreeNodeSchemaNonClass<"com.fluidframework.leaf.string", NodeKind.Leaf, string, string, true, unknown, never, unknown>;
static readonly string: TreeNodeSchemaNonClass<"com.fluidframework.leaf.string", NodeKind.Leaf, string, string, true, unknown, never, unknown>;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,7 @@ export class SchemaFactory<out TScope extends string | undefined = string | unde
readonly requiredRecursive: <const T extends unknown>(t: T, props?: Omit<FieldProps<unknown>, "defaultProvider"> | undefined) => FieldSchemaUnsafe<FieldKind.Required, T>;
static readonly requiredRecursive: <const T extends unknown>(t: T, props?: Omit<FieldProps<unknown>, "defaultProvider"> | undefined) => FieldSchemaUnsafe<FieldKind.Required, T>;
readonly scope: TScope;
scopedFactory<const T extends TName, TNameInner extends number | string = string>(name: T): SchemaFactory<ScopedSchemaName<TScope, T>, TNameInner>;
readonly string: TreeNodeSchemaNonClass<"com.fluidframework.leaf.string", NodeKind.Leaf, string, string, true, unknown, never, unknown>;
static readonly string: TreeNodeSchemaNonClass<"com.fluidframework.leaf.string", NodeKind.Leaf, string, string, true, unknown, never, unknown>;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,7 @@ export class SchemaFactory<out TScope extends string | undefined = string | unde
readonly requiredRecursive: <const T extends unknown>(t: T, props?: Omit<FieldProps<unknown>, "defaultProvider"> | undefined) => FieldSchemaUnsafe<FieldKind.Required, T>;
static readonly requiredRecursive: <const T extends unknown>(t: T, props?: Omit<FieldProps<unknown>, "defaultProvider"> | undefined) => FieldSchemaUnsafe<FieldKind.Required, T>;
readonly scope: TScope;
scopedFactory<const T extends TName, TNameInner extends number | string = string>(name: T): SchemaFactory<ScopedSchemaName<TScope, T>, TNameInner>;
readonly string: TreeNodeSchemaNonClass<"com.fluidframework.leaf.string", NodeKind.Leaf, string, string, true, unknown, never, unknown>;
static readonly string: TreeNodeSchemaNonClass<"com.fluidframework.leaf.string", NodeKind.Leaf, string, string, true, unknown, never, unknown>;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,7 @@ export class SchemaFactory<out TScope extends string | undefined = string | unde
readonly requiredRecursive: <const T extends unknown>(t: T, props?: Omit<FieldProps<unknown>, "defaultProvider"> | undefined) => FieldSchemaUnsafe<FieldKind.Required, T>;
static readonly requiredRecursive: <const T extends unknown>(t: T, props?: Omit<FieldProps<unknown>, "defaultProvider"> | undefined) => FieldSchemaUnsafe<FieldKind.Required, T>;
readonly scope: TScope;
scopedFactory<const T extends TName, TNameInner extends number | string = string>(name: T): SchemaFactory<ScopedSchemaName<TScope, T>, TNameInner>;
readonly string: TreeNodeSchemaNonClass<"com.fluidframework.leaf.string", NodeKind.Leaf, string, string, true, unknown, never, unknown>;
static readonly string: TreeNodeSchemaNonClass<"com.fluidframework.leaf.string", NodeKind.Leaf, string, string, true, unknown, never, unknown>;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,7 @@ export class SchemaFactory<out TScope extends string | undefined = string | unde
readonly requiredRecursive: <const T extends unknown>(t: T, props?: Omit<FieldProps<unknown>, "defaultProvider"> | undefined) => FieldSchemaUnsafe<FieldKind.Required, T>;
static readonly requiredRecursive: <const T extends unknown>(t: T, props?: Omit<FieldProps<unknown>, "defaultProvider"> | undefined) => FieldSchemaUnsafe<FieldKind.Required, T>;
readonly scope: TScope;
scopedFactory<const T extends TName, TNameInner extends number | string = string>(name: T): SchemaFactory<ScopedSchemaName<TScope, T>, TNameInner>;
readonly string: TreeNodeSchemaNonClass<"com.fluidframework.leaf.string", NodeKind.Leaf, string, string, true, unknown, never, unknown>;
static readonly string: TreeNodeSchemaNonClass<"com.fluidframework.leaf.string", NodeKind.Leaf, string, string, true, unknown, never, unknown>;
}
Expand Down
Loading