forked from microsoft/FluidFramework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tree: More schema cleanup (microsoft#22322)
## Description Reduce use of SchemaBuilderInternal. Fix missing sort when persisting field schema found while editing test schema: this is not a correctness issue, but should improve consistency and thus blob reuse in the encoded format.
- Loading branch information
1 parent
c3f64a9
commit 961c9e8
Showing
18 changed files
with
226 additions
and
271 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/*! | ||
* Copyright (c) Microsoft Corporation and contributors. All rights reserved. | ||
* Licensed under the MIT License. | ||
*/ | ||
|
||
import { | ||
type TreeStoredSchema, | ||
rootFieldKey, | ||
type MapTree, | ||
type TreeNodeSchemaIdentifier, | ||
} from "../core/index.js"; | ||
import { leaf, singleJsonCursor } from "../domains/index.js"; | ||
import { FieldKinds, cursorForMapTreeField } from "../feature-libraries/index.js"; | ||
import type { ITreeCheckout } from "../shared-tree/index.js"; | ||
import { toStoredSchema } from "../simple-tree/index.js"; | ||
import { brand, type JsonCompatible } from "../util/index.js"; | ||
import { checkoutWithContent, JsonUnion } from "./utils.js"; | ||
// eslint-disable-next-line import/no-internal-modules | ||
import { normalizeAllowedTypes } from "../simple-tree/schemaTypes.js"; | ||
|
||
// This file provides utilities for testing sequence fields using documents where the root is the sequence being tested. | ||
// This pattern is not expressible using the public simple-tree API, and is only for testing internal details. | ||
|
||
const rootJsonSequenceSchema: TreeStoredSchema = { | ||
nodeSchema: toStoredSchema(JsonUnion).nodeSchema, | ||
rootFieldSchema: { | ||
kind: FieldKinds.sequence.identifier, | ||
types: new Set( | ||
[...normalizeAllowedTypes(JsonUnion)].map((s) => | ||
brand<TreeNodeSchemaIdentifier>(s.identifier), | ||
), | ||
), | ||
}, | ||
}; | ||
|
||
/** | ||
* Helper function to insert node at a given index. | ||
* | ||
* @param tree - The tree on which to perform the insert. | ||
* @param index - The index in the root field at which to insert. | ||
* @param value - The value of the inserted nodes. | ||
*/ | ||
export function insert(tree: ITreeCheckout, index: number, ...values: string[]): void { | ||
const fieldEditor = tree.editor.sequenceField({ field: rootFieldKey, parent: undefined }); | ||
fieldEditor.insert( | ||
index, | ||
cursorForMapTreeField( | ||
values.map((value): MapTree => ({ fields: new Map(), type: leaf.string.name, value })), | ||
), | ||
); | ||
} | ||
|
||
/** | ||
* Removes `count` items from the root field of `tree`. | ||
*/ | ||
export function remove(tree: ITreeCheckout, index: number, count: number): void { | ||
const field = tree.editor.sequenceField({ parent: undefined, field: rootFieldKey }); | ||
field.remove(index, count); | ||
} | ||
|
||
/** | ||
* Creates a sequence field at the root. | ||
*/ | ||
export function makeTreeFromJsonSequence(json: JsonCompatible[]): ITreeCheckout { | ||
const cursors = json.map(singleJsonCursor); | ||
const tree = checkoutWithContent({ | ||
schema: rootJsonSequenceSchema, | ||
initialTree: cursors, | ||
}); | ||
return tree; | ||
} |
Oops, something went wrong.