compose static method

DC compose(
  1. dynamic value,
  2. DistributedConnection connection, [
  3. bool prependType = true
])
Compose a variable Value to compose. DistributedConnection is required to check locality. If True, prepend the DataType at the beginning of the output.

Implementation

static DC compose(dynamic value, DistributedConnection connection,
    [bool prependType = true]) {
  if (value is Function(DistributedConnection))
    value = Function.apply(value, [connection]);
  else if (value is DistributedPropertyContext)
    value = value.method?.call(connection);

  var type = getDataType(value, connection);
  var rt = new BinaryList();

  switch (type) {
    case DataType.Void:
      // nothing to do;
      break;

    case DataType.String:
      var st = DC.stringToBytes(value);
      rt
        ..addInt32(st.length)
        ..addDC(st);
      break;

    case DataType.Resource:
      rt.addUint32((value as DistributedResource).id as int);
      break;

    case DataType.DistributedResource:
      rt.addUint32((value as IResource).instance?.id as int);
      break;

    case DataType.Structure:
      rt.addDC(composeStructure(value, connection, true, true, true));
      break;

    case DataType.VarArray:
      rt.addDC(composeVarArray(value, connection, true));
      break;

    case DataType.Record:
      rt.addDC(composeRecord(value, connection, true, true));
      break;

    case DataType.ResourceArray:
      rt.addDC(composeResourceArray(value, connection, true));
      break;

    case DataType.StructureArray:
      rt.addDC(composeStructureArray(value, connection, true));
      break;

    case DataType.RecordArray:
      rt.addDC(composeRecordArray(value, connection, true));
      break;

    default:
      rt.add(type, value);
      if (DataType.isArray(type)) rt.insertInt32(0, rt.length);
      break;
  }

  if (prependType) rt.insertUint8(0, type);

  return rt.toDC();
}