@revisium/schema-toolkit 0.17.0 → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -70,6 +70,8 @@ var NullNodeImpl = class {
70
70
  }
71
71
  addChild(_node) {
72
72
  }
73
+ insertChild(_index, _node) {
74
+ }
73
75
  removeChild(_name) {
74
76
  return false;
75
77
  }
@@ -260,6 +262,8 @@ var BaseNode = class {
260
262
  }
261
263
  addChild(_node) {
262
264
  }
265
+ insertChild(_index, _node) {
266
+ }
263
267
  removeChild(_name) {
264
268
  return false;
265
269
  }
@@ -303,6 +307,7 @@ var ObjectNode = class _ObjectNode extends BaseNode {
303
307
  makeObservable(this, {
304
308
  _children: "observable.shallow",
305
309
  addChild: "action",
310
+ insertChild: "action",
306
311
  removeChild: "action",
307
312
  replaceChild: "action"
308
313
  });
@@ -338,6 +343,12 @@ var ObjectNode = class _ObjectNode extends BaseNode {
338
343
  addChild(node) {
339
344
  this._children.push(node);
340
345
  }
346
+ insertChild(index, node) {
347
+ if (index < 0 || index > this._children.length) {
348
+ throw new Error(`Index out of bounds: ${index}`);
349
+ }
350
+ this._children.splice(index, 0, node);
351
+ }
341
352
  removeChild(name) {
342
353
  const index = this._children.findIndex((child) => child.name() === name);
343
354
  if (index === -1) {
@@ -853,6 +864,17 @@ var SchemaTreeImpl = class _SchemaTreeImpl {
853
864
  parent.addChild(node);
854
865
  this.rebuildIndex();
855
866
  }
867
+ insertChildAt(parentId, index, node) {
868
+ const parent = this.nodeById(parentId);
869
+ if (parent.isNull()) {
870
+ return;
871
+ }
872
+ if (parent.isArray()) {
873
+ throw new Error("Cannot add child to array node. Use setItems instead.");
874
+ }
875
+ parent.insertChild(index, node);
876
+ this.rebuildIndex();
877
+ }
856
878
  removeNodeAt(path) {
857
879
  if (path.isEmpty()) {
858
880
  return false;
@@ -2220,7 +2242,9 @@ var PatchEnricher = class {
2220
2242
  constructor(currentTree, baseTree) {
2221
2243
  this.currentTree = currentTree;
2222
2244
  this.baseTree = baseTree;
2245
+ this.extractors = this.buildExtractors();
2223
2246
  }
2247
+ extractors;
2224
2248
  enrich(patch) {
2225
2249
  const fieldName = this.getFieldNameFromPath(patch.path);
2226
2250
  if (patch.op === "add") {
@@ -2237,18 +2261,13 @@ var PatchEnricher = class {
2237
2261
  enrichAddPatch(patch, fieldName) {
2238
2262
  const currentNode = this.getNodeAtPath(this.currentTree, patch.path);
2239
2263
  if (!currentNode) {
2240
- return { patch, fieldName, metadataChanges: [] };
2264
+ return { patch, fieldName, propertyChanges: [] };
2241
2265
  }
2242
- const metadata = this.computeAddMetadata(currentNode);
2243
- return {
2244
- patch,
2245
- fieldName,
2246
- metadataChanges: this.computeMetadataChanges(metadata),
2247
- ...metadata
2248
- };
2266
+ const propertyChanges = this.computeAddPropertyChanges(currentNode);
2267
+ return { patch, fieldName, propertyChanges };
2249
2268
  }
2250
2269
  enrichRemovePatch(patch, fieldName) {
2251
- return { patch, fieldName, metadataChanges: [] };
2270
+ return { patch, fieldName, propertyChanges: [] };
2252
2271
  }
2253
2272
  enrichMovePatch(patch, fieldName) {
2254
2273
  const fromPath = patch.from;
@@ -2256,98 +2275,98 @@ var PatchEnricher = class {
2256
2275
  const movesIntoArray = this.movesIntoArrayBoundary(fromPath, patch.path);
2257
2276
  const baseNode = this.getNodeAtPath(this.baseTree, fromPath);
2258
2277
  const currentNode = this.getNodeAtPath(this.currentTree, patch.path);
2259
- const formulaChange = this.computeFormulaChange(baseNode, currentNode);
2260
- const metadataChanges = [];
2261
- if (formulaChange) {
2262
- metadataChanges.push("formula");
2263
- }
2278
+ const propertyChanges = this.computePropertyChanges(baseNode, currentNode);
2264
2279
  return {
2265
2280
  patch,
2266
2281
  fieldName,
2267
- metadataChanges,
2282
+ propertyChanges,
2268
2283
  isRename: isRename || void 0,
2269
- movesIntoArray: movesIntoArray || void 0,
2270
- formulaChange
2284
+ movesIntoArray: movesIntoArray || void 0
2271
2285
  };
2272
2286
  }
2273
2287
  enrichReplacePatch(patch, fieldName) {
2274
2288
  const baseNode = this.getNodeAtPath(this.baseTree, patch.path);
2275
2289
  const currentNode = this.getNodeAtPath(this.currentTree, patch.path);
2276
2290
  const isArrayMetadataPatch = baseNode?.isArray() && currentNode?.isArray();
2277
- const formulaChange = this.computeFormulaChange(baseNode, currentNode);
2278
- const defaultChange = isArrayMetadataPatch ? void 0 : this.computeDefaultChange(baseNode, currentNode);
2279
- const descriptionChange = this.computeDescriptionChange(baseNode, currentNode);
2280
- const deprecatedChange = this.computeDeprecatedChange(baseNode, currentNode);
2281
- const foreignKeyChange = this.computeForeignKeyChange(baseNode, currentNode);
2282
- const contentMediaTypeChange = this.computeContentMediaTypeChange(baseNode, currentNode);
2283
- const metadataChanges = this.computeMetadataChanges({
2284
- formulaChange,
2285
- defaultChange,
2286
- descriptionChange,
2287
- deprecatedChange,
2288
- foreignKeyChange,
2289
- contentMediaTypeChange
2290
- });
2291
+ const skipProperties = isArrayMetadataPatch ? ["default"] : [];
2292
+ const propertyChanges = this.computePropertyChanges(
2293
+ baseNode,
2294
+ currentNode,
2295
+ { skipProperties }
2296
+ );
2291
2297
  return {
2292
2298
  patch,
2293
2299
  fieldName,
2294
- metadataChanges,
2295
- typeChange: this.computeTypeChange(baseNode, currentNode, isArrayMetadataPatch),
2296
- formulaChange,
2297
- defaultChange,
2298
- descriptionChange,
2299
- deprecatedChange,
2300
- foreignKeyChange,
2301
- contentMediaTypeChange
2300
+ propertyChanges,
2301
+ typeChange: this.computeTypeChange(baseNode, currentNode, isArrayMetadataPatch)
2302
2302
  };
2303
2303
  }
2304
- computeAddMetadata(node) {
2305
- const result = {};
2306
- const formula = node.formula();
2307
- if (formula) {
2308
- result.formulaChange = {
2309
- fromFormula: void 0,
2310
- toFormula: this.getFormulaExpression(formula, this.currentTree, node.id()),
2311
- fromVersion: void 0,
2312
- toVersion: formula.version()
2313
- };
2314
- }
2315
- const defaultValue = node.defaultValue();
2316
- if (defaultValue !== void 0 && isPrimitiveDefault(defaultValue)) {
2317
- result.defaultChange = {
2318
- fromDefault: void 0,
2319
- toDefault: defaultValue
2320
- };
2321
- }
2322
- const meta = node.metadata();
2323
- if (meta.description) {
2324
- result.descriptionChange = {
2325
- fromDescription: void 0,
2326
- toDescription: meta.description
2327
- };
2328
- }
2329
- if (meta.deprecated) {
2330
- result.deprecatedChange = {
2331
- fromDeprecated: void 0,
2332
- toDeprecated: meta.deprecated
2333
- };
2334
- }
2335
- const foreignKey = node.foreignKey();
2336
- if (foreignKey) {
2337
- result.foreignKeyChange = {
2338
- fromForeignKey: void 0,
2339
- toForeignKey: foreignKey
2340
- };
2341
- }
2342
- const contentMediaType = node.contentMediaType();
2343
- if (contentMediaType) {
2344
- result.contentMediaTypeChange = {
2345
- fromContentMediaType: void 0,
2346
- toContentMediaType: contentMediaType
2347
- };
2304
+ buildExtractors() {
2305
+ return [
2306
+ {
2307
+ property: "formula",
2308
+ extract: (node, tree) => {
2309
+ const formula = node?.formula();
2310
+ if (!formula || !node) {
2311
+ return void 0;
2312
+ }
2313
+ return FormulaSerializer.serializeExpression(tree, node.id(), formula, { strict: false });
2314
+ },
2315
+ compare: (from, to) => from === to
2316
+ },
2317
+ {
2318
+ property: "default",
2319
+ extract: (node) => {
2320
+ const value = node?.defaultValue();
2321
+ return isPrimitiveDefault(value) ? value : void 0;
2322
+ }
2323
+ },
2324
+ {
2325
+ property: "description",
2326
+ extract: (node) => node?.metadata().description
2327
+ },
2328
+ {
2329
+ property: "deprecated",
2330
+ extract: (node) => node?.metadata().deprecated
2331
+ },
2332
+ {
2333
+ property: "foreignKey",
2334
+ extract: (node) => node?.foreignKey()
2335
+ },
2336
+ {
2337
+ property: "contentMediaType",
2338
+ extract: (node) => node?.contentMediaType()
2339
+ },
2340
+ {
2341
+ property: "ref",
2342
+ extract: (node) => node?.ref()
2343
+ },
2344
+ {
2345
+ property: "title",
2346
+ extract: (node) => node?.metadata().title
2347
+ }
2348
+ ];
2349
+ }
2350
+ computePropertyChanges(baseNode, currentNode, options) {
2351
+ const skipSet = options?.skipProperties;
2352
+ const result = [];
2353
+ for (const extractor of this.extractors) {
2354
+ if (skipSet?.includes(extractor.property)) {
2355
+ continue;
2356
+ }
2357
+ const from = extractor.extract(baseNode, this.baseTree);
2358
+ const to = extractor.extract(currentNode, this.currentTree);
2359
+ const areEqual = extractor.compare ? extractor.compare(from, to) : from === to;
2360
+ if (!areEqual) {
2361
+ result.push({ property: extractor.property, from, to });
2362
+ }
2348
2363
  }
2349
2364
  return result;
2350
2365
  }
2366
+ computeAddPropertyChanges(node) {
2367
+ const allChanges = this.computePropertyChanges(null, node);
2368
+ return allChanges.filter((change) => change.to !== void 0);
2369
+ }
2351
2370
  computeTypeChange(baseNode, currentNode, ignoreItems) {
2352
2371
  if (!baseNode || !currentNode) {
2353
2372
  return void 0;
@@ -2366,98 +2385,6 @@ var PatchEnricher = class {
2366
2385
  }
2367
2386
  return void 0;
2368
2387
  }
2369
- computeFormulaChange(baseNode, currentNode) {
2370
- const baseFormula = baseNode?.formula();
2371
- const currentFormula = currentNode?.formula();
2372
- const baseExpr = baseFormula && baseNode ? this.getFormulaExpression(baseFormula, this.baseTree, baseNode.id()) : void 0;
2373
- const currentExpr = currentFormula && currentNode ? this.getFormulaExpression(currentFormula, this.currentTree, currentNode.id()) : void 0;
2374
- const baseVersion = baseFormula?.version();
2375
- const currentVersion = currentFormula?.version();
2376
- if (baseExpr !== currentExpr || baseVersion !== currentVersion) {
2377
- return {
2378
- fromFormula: baseExpr,
2379
- toFormula: currentExpr,
2380
- fromVersion: baseVersion,
2381
- toVersion: currentVersion
2382
- };
2383
- }
2384
- return void 0;
2385
- }
2386
- getFormulaExpression(formula, tree, nodeId) {
2387
- return FormulaSerializer.serializeExpression(tree, nodeId, formula, { strict: false });
2388
- }
2389
- computeDefaultChange(baseNode, currentNode) {
2390
- const baseDefault = baseNode?.defaultValue();
2391
- const currentDefault = currentNode?.defaultValue();
2392
- const safeBaseDefault = isPrimitiveDefault(baseDefault) ? baseDefault : void 0;
2393
- const safeCurrentDefault = isPrimitiveDefault(
2394
- currentDefault
2395
- ) ? currentDefault : void 0;
2396
- if (safeBaseDefault !== safeCurrentDefault) {
2397
- return {
2398
- fromDefault: safeBaseDefault,
2399
- toDefault: safeCurrentDefault
2400
- };
2401
- }
2402
- return void 0;
2403
- }
2404
- computeDescriptionChange(baseNode, currentNode) {
2405
- const baseDesc = baseNode?.metadata().description;
2406
- const currentDesc = currentNode?.metadata().description;
2407
- if (baseDesc !== currentDesc) {
2408
- return { fromDescription: baseDesc, toDescription: currentDesc };
2409
- }
2410
- return void 0;
2411
- }
2412
- computeDeprecatedChange(baseNode, currentNode) {
2413
- const baseDeprecated = baseNode?.metadata().deprecated;
2414
- const currentDeprecated = currentNode?.metadata().deprecated;
2415
- if (baseDeprecated !== currentDeprecated) {
2416
- return { fromDeprecated: baseDeprecated, toDeprecated: currentDeprecated };
2417
- }
2418
- return void 0;
2419
- }
2420
- computeForeignKeyChange(baseNode, currentNode) {
2421
- const baseFk = baseNode?.foreignKey();
2422
- const currentFk = currentNode?.foreignKey();
2423
- if (baseFk !== currentFk) {
2424
- return { fromForeignKey: baseFk, toForeignKey: currentFk };
2425
- }
2426
- return void 0;
2427
- }
2428
- computeContentMediaTypeChange(baseNode, currentNode) {
2429
- const baseMediaType = baseNode?.contentMediaType();
2430
- const currentMediaType = currentNode?.contentMediaType();
2431
- if (baseMediaType !== currentMediaType) {
2432
- return {
2433
- fromContentMediaType: baseMediaType,
2434
- toContentMediaType: currentMediaType
2435
- };
2436
- }
2437
- return void 0;
2438
- }
2439
- computeMetadataChanges(changes) {
2440
- const result = [];
2441
- if (changes.formulaChange) {
2442
- result.push("formula");
2443
- }
2444
- if (changes.defaultChange) {
2445
- result.push("default");
2446
- }
2447
- if (changes.descriptionChange) {
2448
- result.push("description");
2449
- }
2450
- if (changes.deprecatedChange) {
2451
- result.push("deprecated");
2452
- }
2453
- if (changes.foreignKeyChange) {
2454
- result.push("foreignKey");
2455
- }
2456
- if (changes.contentMediaTypeChange) {
2457
- result.push("contentMediaType");
2458
- }
2459
- return result;
2460
- }
2461
2388
  getNodeType(node) {
2462
2389
  if (node.isArray()) {
2463
2390
  const items = node.items();
@@ -2998,5 +2925,5 @@ exports.runInAction = runInAction;
2998
2925
  exports.setReactivityProvider = setReactivityProvider;
2999
2926
  exports.validateFormulas = validateFormulas;
3000
2927
  exports.validateSchema = validateSchema;
3001
- //# sourceMappingURL=chunk-ZG7QX4I3.cjs.map
3002
- //# sourceMappingURL=chunk-ZG7QX4I3.cjs.map
2928
+ //# sourceMappingURL=chunk-WJXVLFZN.cjs.map
2929
+ //# sourceMappingURL=chunk-WJXVLFZN.cjs.map