writeGeneralCodec method

  1. @override
void writeGeneralCodec(
  1. DartOptions generatorOptions,
  2. Root root,
  3. Indent indent, {
  4. required String dartPackageName,
})
override

Writes the custom codec to indent.

Implementation

@override
void writeGeneralCodec(
  DartOptions generatorOptions,
  Root root,
  Indent indent, {
  required String dartPackageName,
}) {
  void writeEncodeLogic(
      EnumeratedType customType, int nonSerializedClassCount) {
    indent.writeScoped('else if (value is ${customType.name}) {', '}', () {
      if (customType.offset(nonSerializedClassCount) < maximumCodecFieldKey) {
        indent.writeln(
            'buffer.putUint8(${customType.offset(nonSerializedClassCount)});');
        if (customType.type == CustomTypes.customClass) {
          indent.writeln('writeValue(buffer, value.encode());');
        } else if (customType.type == CustomTypes.customEnum) {
          indent.writeln('writeValue(buffer, value.index);');
        }
      } else {
        final String encodeString = customType.type == CustomTypes.customClass
            ? '.encode()'
            : '.index';
        indent.writeln(
            'final $_overflowClassName wrap = $_overflowClassName(type: ${customType.offset(nonSerializedClassCount) - maximumCodecFieldKey}, wrapped: value$encodeString);');
        indent.writeln('buffer.putUint8($maximumCodecFieldKey);');
        indent.writeln('writeValue(buffer, wrap.encode());');
      }
    }, addTrailingNewline: false);
  }

  void writeDecodeLogic(
      EnumeratedType customType, int nonSerializedClassCount) {
    indent.writeln('case ${customType.offset(nonSerializedClassCount)}: ');
    indent.nest(1, () {
      if (customType.type == CustomTypes.customClass) {
        if (customType.offset(nonSerializedClassCount) ==
            maximumCodecFieldKey) {
          indent.writeln(
              'final ${customType.name} wrapper = ${customType.name}.decode(readValue(buffer)!);');
          indent.writeln('return wrapper.unwrap();');
        } else {
          indent.writeln(
              'return ${customType.name}.decode(readValue(buffer)!);');
        }
      } else if (customType.type == CustomTypes.customEnum) {
        indent.writeln('final int? value = readValue(buffer) as int?;');
        indent.writeln(
            'return value == null ? null : ${customType.name}.values[value];');
      }
    });
  }

  final EnumeratedType overflowClass = EnumeratedType(
      _overflowClassName, maximumCodecFieldKey, CustomTypes.customClass);

  indent.newln();
  final List<EnumeratedType> enumeratedTypes =
      getEnumeratedTypes(root, excludeSealedClasses: true).toList();
  if (root.requiresOverflowClass) {
    _writeCodecOverflowUtilities(indent, enumeratedTypes);
  }
  indent.newln();
  indent.write('class $_pigeonMessageCodec extends StandardMessageCodec');
  indent.addScoped(' {', '}', () {
    indent.writeln('const $_pigeonMessageCodec();');
    indent.writeln('@override');
    indent.write('void writeValue(WriteBuffer buffer, Object? value) ');
    indent.addScoped('{', '}', () {
      indent.writeScoped('if (value is int) {', '}', () {
        indent.writeln('buffer.putUint8(4);');
        indent.writeln('buffer.putInt64(value);');
      }, addTrailingNewline: false);
      int nonSerializedClassCount = 0;
      enumerate(enumeratedTypes,
          (int index, final EnumeratedType customType) {
        if (customType.associatedClass?.isSealed ?? false) {
          nonSerializedClassCount += 1;
          return;
        }
        writeEncodeLogic(customType, nonSerializedClassCount);
      });
      indent.addScoped(' else {', '}', () {
        indent.writeln('super.writeValue(buffer, value);');
      });
    });
    indent.newln();
    indent.writeln('@override');
    indent.write('Object? readValueOfType(int type, ReadBuffer buffer) ');
    indent.addScoped('{', '}', () {
      indent.write('switch (type) ');
      indent.addScoped('{', '}', () {
        int nonSerializedClassCount = 0;
        for (final EnumeratedType customType in enumeratedTypes) {
          if (customType.associatedClass?.isSealed ?? false) {
            nonSerializedClassCount++;
          } else if (customType.offset(nonSerializedClassCount) <
              maximumCodecFieldKey) {
            writeDecodeLogic(customType, nonSerializedClassCount);
          }
        }
        if (root.requiresOverflowClass) {
          writeDecodeLogic(overflowClass, 0);
        }
        indent.writeln('default:');
        indent.nest(1, () {
          indent.writeln('return super.readValueOfType(type, buffer);');
        });
      });
    });
  });
  if (root.containsEventChannel) {
    indent.newln();
    indent.writeln(
        'const StandardMethodCodec $_pigeonMethodChannelCodec = StandardMethodCodec($_pigeonMessageCodec());');
  }
}