Allow for array extension in dynamic attributes, disallow attribute overrides

This commit is contained in:
Zef Hemel
2024-07-30 11:46:42 +02:00
parent 704e318229
commit b2e7bc8494
2 changed files with 63 additions and 32 deletions
+31 -1
View File
@@ -3,7 +3,7 @@ import { IndexedDBKvPrimitives } from "../data/indexeddb_kv_primitives.ts";
import { cleanupEmptyObjects, DataStore } from "../data/datastore.ts";
import { DenoKvPrimitives } from "../data/deno_kv_primitives.ts";
import { KvPrimitives } from "../data/kv_primitives.ts";
import { assertEquals } from "$std/testing/asserts.ts";
import { assertEquals, assertThrows } from "$std/testing/asserts.ts";
import { PrefixedKvPrimitives } from "../data/prefixed_kv_primitives.ts";
import { Query } from "../../plug-api/types.ts";
@@ -124,6 +124,28 @@ async function test(db: KvPrimitives) {
]],
},
},
// Test extending existing array attributes
{
where: ["boolean", true],
attributes: {
"listAttribute": ["array", [["string", "newValue1"]]],
"nested.listAttribute": ["array", [["string", "newValue1"]]],
},
},
{
where: ["boolean", true],
attributes: {
"listAttribute": ["array", [["string", "newValue2"]]],
"nested.listAttribute": ["array", [["string", "newValue2"]]],
},
},
// Test not being able to override existing attributes
{
where: ["boolean", true],
attributes: {
"lastName": ["string", "Shouldn't be set"],
},
},
{
where: ["=", ["attr", "tags"], ["string", "person"]],
attributes: {
@@ -143,12 +165,16 @@ async function test(db: KvPrimitives) {
const pristineCopy = JSON.parse(JSON.stringify(obj));
datastore.enrichObject(obj);
// console.log("Enrhiched", obj);
assertEquals(obj.fullName, "Pete Smith");
assertEquals(obj.lastName, "Smith");
assertEquals(obj.pageDecoration, {
prefix: { bla: { doh: "🧑 Pete Smith" } },
});
assertEquals(obj.existingObjAttribute.something, true);
assertEquals(obj.existingObjAttribute.another, "value");
assertEquals(obj.listAttribute, ["newValue1", "newValue2"]);
assertEquals(obj.nested.listAttribute, ["newValue1", "newValue2"]);
// And now let's clean it again
datastore.cleanEnrichedObject(obj);
@@ -162,6 +188,10 @@ async function test(db: KvPrimitives) {
attributes: {},
},
];
assertThrows(() => {
datastore.enrichObject({});
});
}
Deno.test("Test Deno KV DataStore", async () => {
+32 -31
View File
@@ -136,7 +136,8 @@ export class DataStore {
}
/**
* Enriches the object with the attributes defined in the object enrichers on the fly
* Enriches the object with the attributes defined in the object enrichers on the fly.
* Will add a `$dynamicAttributes` array to the object to keep track of the dynamic attributes set (for cleanup)
* @param object
* @returns
*/
@@ -192,7 +193,22 @@ export class DataStore {
`Enricher dynamic attribute expression cannot be an async function: ${expression}`,
);
}
objectValue[selectorParts[selectorParts.length - 1]] = value;
const lastPart = selectorParts[selectorParts.length - 1];
if (objectValue[lastPart] !== undefined) {
// The attribute already exists, we should merge the values if we can, or ignore the new value
if (Array.isArray(objectValue[lastPart]) && Array.isArray(value)) {
// If the attribute already exists and is an array, we should merge the arrays
objectValue[lastPart] = [...objectValue[lastPart], ...value];
} else {
// We can't merge the values, so we just ignore the new value
}
} else { // New attribute
objectValue[lastPart] = value;
if (!object.$dynamicAttributes) {
object.$dynamicAttributes = [];
}
object.$dynamicAttributes.push(attributeSelector);
}
}
}
}
@@ -204,44 +220,29 @@ export class DataStore {
* @returns
*/
cleanEnrichedObject(object: any) {
// Check if this object looks like an object value
if (!object || typeof object !== "object") {
// Check if this is an enriched object
if (!object || !object.$dynamicAttributes) {
// Skip
return;
}
for (const enricher of this.objectEnrichers) {
if (
evalQueryExpression(
enricher.where,
object,
{}, // We will not support variables in enrichers for now
this.functionMap,
)
) {
// The `where` matches so we should clean this object from the dynamic attributes
for (
const [attributeSelector, _expression] of Object.entries(
enricher.attributes,
)
) {
// Recursively travel to the attribute based on the selector, which may contain .'s to go deeper
let objectValue = object;
const selectorParts = attributeSelector.split(".");
for (const part of selectorParts.slice(0, -1)) {
if (typeof objectValue[part] !== "object") {
// This shouldn't happen, but let's back out
break;
}
objectValue = objectValue[part];
}
delete objectValue[selectorParts[selectorParts.length - 1]];
for (const attributeSelector of object.$dynamicAttributes) {
// Recursively travel to the attribute based on the selector, which may contain .'s to go deeper
let objectValue = object;
const selectorParts = attributeSelector.split(".");
for (const part of selectorParts.slice(0, -1)) {
if (typeof objectValue[part] !== "object") {
// This shouldn't happen, but let's back out
break;
}
objectValue = objectValue[part];
}
delete objectValue[selectorParts[selectorParts.length - 1]];
}
// Clean up empty objects, this is somewhat questionable, because it also means that if the user intentionally kept empty objects in there, these will be wiped
cleanupEmptyObjects(object);
delete object.$dynamicAttributes;
}
}