Skip to content
Merged
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
38 changes: 37 additions & 1 deletion tDataTypeTemplates/insertSelecetdLNodeType.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
mhaiSelection,
mmxuSelection,
ptocSelection,
lln0Selection
lln0Selection,
ptrcSelection
} from "./insertSelectedLNodeType.testdata.js";

import {
Expand All @@ -22,6 +23,31 @@ import {
import { insertSelectedLNodeType } from "./insertSelectedLNodeType.js";
import { CdcChildren, DaDescription, LNodeDescription, nsdToJson } from "./nsdToJson.js";


function cyrb64(str: string): string {
/* eslint-disable no-bitwise */
let h1 = 0xdeadbeef;
let h2 = 0x41c6ce57;
/* eslint-disable-next-line no-plusplus */
for (let i = 0, ch; i < str.length; i++) {
ch = str.charCodeAt(i);
h1 = Math.imul(h1 ^ ch, 2654435761);
h2 = Math.imul(h2 ^ ch, 1597334677);
}
h1 =
Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^
Math.imul(h2 ^ (h2 >>> 13), 3266489909);
h2 =
Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^
Math.imul(h1 ^ (h1 >>> 13), 3266489909);
return (
(h2 >>> 0).toString(16).padStart(8, "0") +
(h1 >>> 0).toString(16).padStart(8, "0")
);
/* eslint-enable no-bitwise */
}


const incompleteMmxu = findElement(missingMmxuTypes) as XMLDocument;
const imcompleteLtrk = findElement(incompleteLtrkTypes) as XMLDocument;
const incompleteAtcc = findElement(incompleteAtccTypes) as XMLDocument;
Expand Down Expand Up @@ -239,5 +265,15 @@ describe("insertLNodeTypeSelection", () => {

expect(edits.length).to.equal(5);
expect((edits[3].node as Element).querySelector('DA[name="dataNs"] > Val')?.textContent).to.equal("TestNameSpace-1-d-1234567890");
});

it('set user defined LNodeType.id', () => {
const id = cyrb64("TestFile");
const edits = insertSelectedLNodeType(missingDataTypes, ptrcSelection, { class: "PTRC", id });

expect(edits.length).to.equal(4);
expect((edits[1].node as Element).getAttribute("id")).to.equal("96ba6481aba181d8");
expect((edits[2].node as Element).getAttribute("id")).to.equal("Beh$oscd$_c6ed035c8137b35a");
expect((edits[3].node as Element).getAttribute("id")).to.equal("stVal$oscd$_48ba16345b8e7f5b");
})
});
14 changes: 14 additions & 0 deletions tDataTypeTemplates/insertSelectedLNodeType.testdata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,3 +533,17 @@ export const lln0Selection = {
"dataNs": {}
},
}

export const ptrcSelection = {
"Beh": {
"q": {},
"stVal": {
"blocked": {},
"off": {},
"on": {},
"test": {},
"test/blocked": {}
},
"t": {}
},
}
9 changes: 5 additions & 4 deletions tDataTypeTemplates/insertSelectedLNodeType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ function data(lnData: any, path: string[]): any {
export function insertSelectedLNodeType(
doc: XMLDocument,
selection: TreeSelection,
logicalnode: { class: string, desc?: string, data?: LNodeDescription },
logicalnode: { class: string, desc?: string, id?: string, data?: LNodeDescription },
): Insert[] {
const types = new Set<string>();
const elements: Templates = {
Expand All @@ -193,9 +193,9 @@ export function insertSelectedLNodeType(
return !alreadyCreate && !alreadyExist;
}

function identify(element: Element, name: string): string {
function identify(element: Element, name: string, userId?: string): string {
const hash = hashElement(element);
const id = `${name}$oscd$_${hash}`;
const id = userId ?? `${name}$oscd$_${hash}`;

element.setAttribute("id", id);
if (isUnknownId(id)) {
Expand Down Expand Up @@ -406,7 +406,8 @@ export function insertSelectedLNodeType(
lnType.append(doElement);
});

identify(lnType, lnClass);
// write LNodeType.id user defined id or content hash
identify(lnType, lnClass, logicalnode.id);

const dataTypeTemplates: Element =
(doc.querySelector(":root > DataTypeTemplates") as Element) ||
Expand Down