Skip to content
Snippets Groups Projects
Select Git revision
  • 624e9810355fe313655fdaf278668622bf7fc53e
  • devel default protected
  • client-d
  • release-7.2 protected
  • master protected
  • 0.4.11
  • 0.4.10
  • 0.4.9
  • 0.4.8
  • 0.4.7
  • 0.4.6
  • 0.4.5
  • 0.4.4
  • 0.4.3
  • 0.4.2
  • 0.4.1
  • 0.4.0
  • 0.3.10
  • 0.3.9
  • 0.3.8
  • 0.3.6
  • 0.3.5
  • 0.3.4
  • 0.3.3
  • 0.3.1
25 results

add.ts

Blame
  • 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,
    };