Newer
Older
import { DecodeFn } from "../../types";
import { searchAll } from "../util";
import {
EncodeFn,
SetLookupUpdateObject,
SetLookupUpdateParamSetting,
} from "../../types";
import * as util from "../util";
const decode: DecodeFn = (msg, decodeOptions) => {
if (decodeOptions?.raw) return [msg];
const affected = searchAll(msg, "updatedInstResults");
return [affected];
};
const isObject = (v) =>
typeof v === "object" && v.required !== undefined && v.value !== undefined;
const makePairs = (path: string, value): [string, any, boolean][] =>
path.endsWith(".") && typeof value === "object" && value !== null
? Object.entries(value as any[]).map(([k, v]) =>
isObject(v)
? [k, v.value.toString(), "required" in v && Boolean(v.required)]
: [k, v.toString(), true]
)
: [[path.split(".").pop() || "", (value || "").toString(), true]];
const encode: EncodeFn = ({ value, path: initialPath }) => {
const [paths, values] = parseSetArgs(value, initialPath);
(values && values.some((it) => it.allowPartial)) || true;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const updateObjs = paths.map((path, i) => ({
lookup: "Set.UpdateObject" as SetLookupUpdateObject,
objPath: path.endsWith(".")
? path
: path.slice(0, path.lastIndexOf(".") + 1),
paramSettings: makePairs(path, values[i])
.filter(([k]) => k !== "allowPartial")
.map(([param, value, required]) => ({
lookup: "Set.UpdateParamSetting" as SetLookupUpdateParamSetting,
param,
value,
required,
})),
}));
return {
lookup: "Msg",
header: {
lookup: "Header",
msgId: util.uniq("SET@"),
msgType: "SET",
},
body: {
lookup: "Body",
request: {
lookup: "Request",
set: {
allowPartial,
updateObjs,
},
},
},
};
};
export default {
decode,
encode,
};