Select Git revision
add.ts 1.65 KiB
import { DecodeFn, EncodeFn } from "../../types";
import * as util from "../util";
import {
AddLookupCreateObject,
AddLookupCreateParamSetting,
} from "../../types";
import { parseSetArgs } from "../../util";
const decode: DecodeFn = (msg) => {
const paths: string[] | undefined = util.searchAll(msg, "instantiatedPath");
if (paths && paths.length === 1) return [paths[0]];
return [paths];
};
const isObj = (v) =>
typeof v === "object" && v.required !== undefined && v.value !== undefined;
const makePair = (value): [string, any, boolean][] =>
value
? Object.entries(value as any[]).map(([k, v]) =>
isObj(v)
? [k, v.value.toString(), "required" in v && Boolean(v.required)]
: [k, v.toString(), true]
)
: [];
const encode: EncodeFn = ({ value, path }) => {
const [paths, values] = parseSetArgs(value, path);
const allowPartial =
(values && values.some((it) => it.allowPartial)) || false;
const createObjs = paths.map((path, i) => ({
lookup: "Add.CreateObject" as AddLookupCreateObject,
objPath: path,
paramSettings: makePair(values[i])
.filter(([k]) => k !== "allowPartial")
.map(([param, value, required]) => ({
lookup: "Add.CreateParamSetting" as AddLookupCreateParamSetting,
param,
value,
required,
})),
}));
return {
lookup: "Msg",
header: {
lookup: "Header",
msgId: util.uniq("ADD@"),
msgType: "ADD",
},
body: {
lookup: "Body",
request: {
lookup: "Request",
add: {
allowPartial,
createObjs,
},
},
},
};
};
export default {
decode,
encode,
};