refactor: replace array-based expressions with typed class hierarchy
Replace the old Lisp-like array expression representation (["AND", ["PARAM", ...], ...]) with a class hierarchy rooted in an abstract Expression class (Literal, Parameter, Binary, Unary, FunctionCall, Conditional). This gives compile-time type safety to expression construction, evaluation, and pattern matching. Key changes: - New lib/common/expression.ts defines the class hierarchy with map/evaluate/toString methods - Rewrite parser as hand-rolled recursive descent, removing the parsimmon dependency - Extend Path with a colon separator to unify parameter identity and attributes into a single address space (e.g. "Device.WiFi.SSID.1.Name:writable"). The colon field tracks how many trailing segments are attribute qualifiers. PathSet rewritten with separate param/attr segment indexes - Flatten device attribute representation in the UI to colon-delimited keys mirroring the new path syntax
This commit is contained in:
1 parent
75908e624f
commit
6f3ce6f7c8
71 files changed
+4045
-3449
No files matched your search
+36
-19
@@ -1,6 +1,6 @@
|
||||
import { ClosureComponent, Component, Children, Vnode } from "mithril";
|
||||
import { m } from "./components.ts";
|
||||
import config from "./config.ts";
|
||||
import { pageSize as PAGE_SIZE } from "./config.ts";
|
||||
import filterComponent from "./filter-component.ts";
|
||||
import * as overlay from "./overlay.ts";
|
||||
import * as store from "./store.ts";
|
||||
@@ -9,11 +9,9 @@ import putFormComponent from "./put-form-component.ts";
|
||||
import indexTableComponent from "./index-table-component.ts";
|
||||
import memoize from "../lib/common/memoize.ts";
|
||||
import * as smartQuery from "./smart-query.ts";
|
||||
import { map, parse, stringify } from "../lib/common/expression/parser.ts";
|
||||
import Expression from "../lib/common/expression.ts";
|
||||
|
||||
const PAGE_SIZE = config.ui.pageSize || 10;
|
||||
|
||||
const memoizedParse = memoize(parse);
|
||||
const memoizedParse = memoize((str) => Expression.parse(str));
|
||||
const memoizedJsonParse = memoize(JSON.parse);
|
||||
|
||||
const attributes = [
|
||||
@@ -27,10 +25,22 @@ const attributes = [
|
||||
{ id: "provisionArgs", label: "Arguments", type: "textarea" },
|
||||
];
|
||||
|
||||
const unpackSmartQuery = memoize((query) => {
|
||||
return map(query, (e) => {
|
||||
if (Array.isArray(e) && e[0] === "FUNC" && e[1] === "Q")
|
||||
return smartQuery.unpack("presets", e[2], e[3]);
|
||||
const unpackSmartQuery = memoize((query: Expression) => {
|
||||
return query.evaluate((e) => {
|
||||
if (e instanceof Expression.FunctionCall) {
|
||||
if (e.name === "Q") {
|
||||
if (
|
||||
e.args[0] instanceof Expression.Literal &&
|
||||
e.args[1] instanceof Expression.Literal
|
||||
) {
|
||||
return smartQuery.unpack(
|
||||
"presets",
|
||||
e.args[0].value as string,
|
||||
e.args[1].value as string,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
return e;
|
||||
});
|
||||
});
|
||||
@@ -71,7 +81,7 @@ function putActionHandler(action, _object, isNew): Promise<ValidationErrors> {
|
||||
|
||||
if (object.precondition) {
|
||||
try {
|
||||
object.precondition = stringify(memoizedParse(object.precondition));
|
||||
object.precondition = memoizedParse(object.precondition).toString();
|
||||
} catch {
|
||||
return void resolve({
|
||||
precondition: "Precondition must be valid expression",
|
||||
@@ -134,7 +144,7 @@ const getDownloadUrl = memoize((filter) => {
|
||||
const cols = {};
|
||||
for (const attr of attributes) cols[attr.label] = attr.id;
|
||||
return `api/presets.csv?${m.buildQueryString({
|
||||
filter: stringify(filter),
|
||||
filter: filter.toString(),
|
||||
columns: JSON.stringify(cols),
|
||||
})}`;
|
||||
});
|
||||
@@ -148,8 +158,11 @@ export function init(
|
||||
);
|
||||
}
|
||||
|
||||
const sort = args.hasOwnProperty("sort") ? "" + args["sort"] : "";
|
||||
const filter = args.hasOwnProperty("filter") ? "" + args["filter"] : "";
|
||||
let filter: Expression = null;
|
||||
let sort: Record<string, number> = null;
|
||||
if (args.hasOwnProperty("filter"))
|
||||
filter = Expression.parse(args["filter"] as string);
|
||||
if (args.hasOwnProperty("sort")) sort = JSON.parse(args["sort"] as string);
|
||||
return Promise.resolve({ filter, sort });
|
||||
}
|
||||
|
||||
@@ -165,7 +178,9 @@ export const component: ClosureComponent = (): Component => {
|
||||
}
|
||||
|
||||
function onFilterChanged(filter): void {
|
||||
const ops = { filter };
|
||||
const ops = {};
|
||||
if (!(filter instanceof Expression.Literal && filter.value))
|
||||
ops["filter"] = filter.toString();
|
||||
if (vnode.attrs["sort"]) ops["sort"] = vnode.attrs["sort"];
|
||||
m.route.set("/presets", ops);
|
||||
}
|
||||
@@ -197,10 +212,9 @@ export const component: ClosureComponent = (): Component => {
|
||||
m.route.set("/presets", ops);
|
||||
}
|
||||
|
||||
let filter = vnode.attrs["filter"]
|
||||
? memoizedParse(vnode.attrs["filter"])
|
||||
: true;
|
||||
filter = unpackSmartQuery(filter);
|
||||
const filter = unpackSmartQuery(
|
||||
vnode.attrs["filter"] ?? new Expression.Literal(true),
|
||||
);
|
||||
|
||||
const presets = store.fetch("presets", filter, {
|
||||
limit: vnode.state["showCount"] || PAGE_SIZE,
|
||||
@@ -220,7 +234,10 @@ export const component: ClosureComponent = (): Component => {
|
||||
"instances",
|
||||
]);
|
||||
|
||||
const provisions = store.fetch("provisions", true);
|
||||
const provisions = store.fetch(
|
||||
"provisions",
|
||||
new Expression.Literal(true),
|
||||
);
|
||||
if (provisions.fulfilled) {
|
||||
for (const p of provisions.value) {
|
||||
userDefinedProvisions.add(p["_id"]);
|
||||
|
||||
Reference in new issue
Block a user