How to use the capnp-ts/lib/util.format function in capnp-ts

To help you get started, we’ve selected a few capnp-ts examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jdiaz5513 / capnp-ts / packages / capnpc-ts / src / generators.ts View on Github external
.getSlot()
        .getType()
        .getList()
        .getElementType()
        .which();
      let listClass = ConcreteListType[whichElementType];

      if (
        whichElementType === s.Type.LIST ||
        whichElementType === s.Type.STRUCT
      ) {
        listClass = `${fullClassName}._${properName}`;
      } else if (listClass === void 0) {
        /* istanbul ignore next */
        throw new Error(
          format(E.GEN_UNSUPPORTED_LIST_ELEMENT_TYPE, whichElementType)
        );
      }

      const listClassIdentifier = ts.createIdentifier(listClass);

      getArgs = [offsetLiteral, listClassIdentifier, THIS];

      if (defaultValue) getArgs.push(defaultValue);

      adopt = true;
      disown = true;
      /** __S.getList(0, MyStruct._Foo, this) */
      get = ts.createCall(
        ts.createPropertyAccess(STRUCT, "getList"),
        __,
        getArgs
github jdiaz5513 / capnp-ts / packages / capnpc-ts / src / generators.ts View on Github external
);
      /** __S.setText(0, value, this) */
      set = ts.createCall(ts.createPropertyAccess(STRUCT, "setText"), __, [
        offsetLiteral,
        VALUE,
        THIS
      ]);

      break;

    case s.Type.VOID:
      break;

    case "group":
      if (hadExplicitDefault) {
        throw new Error(format(E.GEN_EXPLICIT_DEFAULT_NON_PRIMITIVE, "group"));
      }

      const groupType = ts.createIdentifier(jsType);

      /** __S.getAs(Foo, this); */
      get = ts.createCall(ts.createPropertyAccess(STRUCT, "getAs"), __, [
        groupType,
        THIS
      ]);
      init = get;

      break;

    default:
      // TODO Maybe this should be an error?
github jdiaz5513 / capnp-ts / packages / capnpc-ts / src / generators.ts View on Github external
);
      has = true;
      /** __S.initData(0, length, this) */
      init = ts.createCall(ts.createPropertyAccess(STRUCT, "initData"), __, [
        offsetLiteral,
        LENGTH,
        THIS
      ]);
      set = copyFromValue;

      break;

    case s.Type.INTERFACE:
      if (hadExplicitDefault) {
        throw new Error(
          format(E.GEN_EXPLICIT_DEFAULT_NON_PRIMITIVE, "INTERFACE")
        );
      }

      /** __S.getPointerAs(0, Foo, this) */
      get = ts.createCall(ts.createPropertyAccess(STRUCT, "getPointerAs"), __, [
        offsetLiteral,
        ts.createIdentifier(jsType),
        THIS
      ]);
      set = copyFromValue;

      break;

    case s.Type.LIST:
      const whichElementType = field
        .getSlot()
github jdiaz5513 / capnp-ts / packages / capnpc-ts / src / file.ts View on Github external
export function lookupNode(
  ctx: CodeGeneratorFileContext,
  lookup: { getId(): capnp.Uint64 } | capnp.Uint64
): s.Node {
  const id = lookup instanceof capnp.Uint64 ? lookup : lookup.getId();
  const node = ctx.nodes.find(n => n.getId().equals(id));

  if (node === undefined) throw new Error(format(E.GEN_NODE_LOOKUP_FAIL, id));

  return node;
}
github jdiaz5513 / capnp-ts / packages / capnpc-ts / src / generators.ts View on Github external
members: ts.ClassElement[],
  node: s.Node,
  field: s.Field
): void {
  let jsType: string;
  let whichType: s.Type_Which | string;

  if (field.isSlot()) {
    const slotType = field.getSlot().getType();
    jsType = getJsType(ctx, slotType, false);
    whichType = slotType.which();
  } else if (field.isGroup()) {
    jsType = getFullClassName(lookupNode(ctx, field.getGroup().getTypeId()));
    whichType = "group";
  } else {
    throw new Error(format(E.GEN_UNKNOWN_STRUCT_FIELD, field.which()));
  }

  const jsTypeReference = ts.createTypeReferenceNode(jsType, __);
  const discriminantOffset = node.getStruct().getDiscriminantOffset();
  const name = field.getName();
  const properName = util.c2t(name);
  const hadExplicitDefault =
    field.isSlot() && field.getSlot().getHadExplicitDefault();
  const discriminantValue = field.getDiscriminantValue();
  const fullClassName = getFullClassName(node);
  const union = discriminantValue !== s.Field.NO_DISCRIMINANT;
  const offset = (field.isSlot() && field.getSlot().getOffset()) || 0;
  const offsetLiteral = ts.createNumericLiteral(offset.toString());
  /** __S.getPointer(0, this) */
  const getPointer = ts.createCall(
    ts.createPropertyAccess(STRUCT, "getPointer"),
github jdiaz5513 / capnp-ts / packages / capnpc-ts / src / generators.ts View on Github external
case s.Type_Which.INT32:
    case s.Type_Which.INT64:
    case s.Type_Which.INT8:
    case s.Type_Which.UINT16:
    case s.Type_Which.UINT32:
    case s.Type_Which.UINT64:
    case s.Type_Which.UINT8:
      initializer = ts.createCall(ts.createPropertyAccess(CAPNP, p.mask), __, [
        createValueExpression(slot.getDefaultValue())
      ]);

      break;

    default:
      throw new Error(
        format(E.GEN_UNKNOWN_DEFAULT, s.Type_Which[whichSlotType])
      );
  }

  return ts.createPropertyAssignment(`default${util.c2t(name)}`, initializer);
}
github jdiaz5513 / capnp-ts / packages / capnpc-ts / src / file.ts View on Github external
case s.Type.STRUCT:
      const c = getFullClassName(lookupNode(ctx, type.getStruct().getTypeId()));

      return constructor ? `capnp.StructCtor<${c}>` : c;

    case s.Type.UINT64:
      return "capnp.Uint64";

    case s.Type.TEXT:
      return "string";

    case s.Type.VOID:
      return "capnp.Void";

    default:
      throw new Error(format(E.GEN_UNKNOWN_TYPE, whichType));
  }
}