toBytes method

Future<Uint8List> toBytes()

Converts the selector to IPLD bytes for Graphsync protocol

Implementation

Future<Uint8List> toBytes() async {
  final node = IPLDNode()
    ..kind = Kind.MAP
    ..mapValue = (IPLDMap()
      ..entries.addAll([
        MapEntry()
          ..key = '.tag'
          ..value = (IPLDNode()
            ..kind = Kind.STRING
            ..stringValue = type.toString()),
        if (criteria.isNotEmpty)
          MapEntry()
            ..key = 'criteria'
            ..value = _encodeCriteria(criteria),
        if (maxDepth != null)
          MapEntry()
            ..key = 'maxDepth'
            ..value = (IPLDNode()
              ..kind = Kind.INTEGER
              ..intValue = Int64(maxDepth!)),
        if (subSelectors != null)
          MapEntry()
            ..key = 'selectors'
            ..value = (IPLDNode()
              ..kind = Kind.LIST
              ..listValue = (IPLDList()
                ..values.addAll(
                  await Future.wait(
                    subSelectors!.map(
                      (s) => s.toBytes().then(
                        (bytes) =>
                            EnhancedCBORHandler.decodeCborWithTags(bytes),
                      ),
                    ),
                  ),
                ))),
        if (fieldPath != null)
          MapEntry()
            ..key = 'path'
            ..value = (IPLDNode()
              ..kind = Kind.STRING
              ..stringValue = fieldPath!),
        if (stopAtLink != null)
          MapEntry()
            ..key = 'stopAtLink'
            ..value = (IPLDNode()
              ..kind = Kind.BOOL
              ..boolValue = stopAtLink!),
      ]));

  return await EnhancedCBORHandler.encodeCbor(node);
}