writeGeneralCodec method
void
writeGeneralCodec(
- SwiftOptions generatorOptions,
- Root root,
- Indent indent, {
- required String dartPackageName,
override
Writes the custom codec to indent
.
Implementation
@override
void writeGeneralCodec(
SwiftOptions generatorOptions,
Root root,
Indent indent, {
required String dartPackageName,
}) {
final String codecName = _getCodecName(generatorOptions);
final String readerWriterName = '${codecName}ReaderWriter';
final String readerName = '${codecName}Reader';
final String writerName = '${codecName}Writer';
final List<EnumeratedType> enumeratedTypes =
getEnumeratedTypes(root).toList();
void writeDecodeLogic(EnumeratedType customType) {
indent.writeln('case ${customType.enumeration}:');
indent.nest(1, () {
if (customType.type == CustomTypes.customEnum) {
indent.writeln(
'let enumResultAsInt: Int? = nilOrValue(self.readValue() as? Int)');
indent.writeScoped('if let enumResultAsInt = enumResultAsInt {', '}',
() {
indent.writeln(
'return ${customType.name}(rawValue: enumResultAsInt)');
});
indent.writeln('return nil');
} else {
indent.writeln(
'return ${customType.name}.fromList(self.readValue() as! [Any?])');
}
});
}
final EnumeratedType overflowClass = EnumeratedType(
_overflowClassName, maximumCodecFieldKey, CustomTypes.customClass);
if (root.requiresOverflowClass) {
indent.newln();
_writeCodecOverflowUtilities(
generatorOptions, root, indent, enumeratedTypes,
dartPackageName: dartPackageName);
}
indent.newln();
// Generate Reader
indent.write('private class $readerName: FlutterStandardReader ');
indent.addScoped('{', '}', () {
if (enumeratedTypes.isNotEmpty) {
indent.write('override func readValue(ofType type: UInt8) -> Any? ');
indent.addScoped('{', '}', () {
indent.write('switch type ');
indent.addScoped('{', '}', nestCount: 0, () {
for (final EnumeratedType customType in enumeratedTypes) {
if (customType.enumeration < maximumCodecFieldKey) {
writeDecodeLogic(customType);
}
}
if (root.requiresOverflowClass) {
writeDecodeLogic(overflowClass);
}
indent.writeln('default:');
indent.nest(1, () {
indent.writeln('return super.readValue(ofType: type)');
});
});
});
}
});
// Generate Writer
indent.newln();
indent.write('private class $writerName: FlutterStandardWriter ');
indent.addScoped('{', '}', () {
if (enumeratedTypes.isNotEmpty) {
indent.write('override func writeValue(_ value: Any) ');
indent.addScoped('{', '}', () {
indent.write('');
for (final EnumeratedType customType in enumeratedTypes) {
indent.add('if let value = value as? ${customType.name} ');
indent.addScoped('{', '} else ', () {
final String encodeString =
customType.type == CustomTypes.customClass
? 'toList()'
: 'rawValue';
final String valueString =
customType.enumeration < maximumCodecFieldKey
? 'value.$encodeString'
: 'wrap.toList()';
final int enumeration =
customType.enumeration < maximumCodecFieldKey
? customType.enumeration
: maximumCodecFieldKey;
if (customType.enumeration >= maximumCodecFieldKey) {
indent.writeln(
'let wrap = $_overflowClassName(type: ${customType.enumeration - maximumCodecFieldKey}, wrapped: value.$encodeString)');
}
indent.writeln('super.writeByte($enumeration)');
indent.writeln('super.writeValue($valueString)');
}, addTrailingNewline: false);
}
indent.addScoped('{', '}', () {
indent.writeln('super.writeValue(value)');
});
});
}
});
indent.newln();
// Generate ReaderWriter
indent
.write('private class $readerWriterName: FlutterStandardReaderWriter ');
indent.addScoped('{', '}', () {
indent.write(
'override func reader(with data: Data) -> FlutterStandardReader ');
indent.addScoped('{', '}', () {
indent.writeln('return $readerName(data: data)');
});
indent.newln();
indent.write(
'override func writer(with data: NSMutableData) -> FlutterStandardWriter ');
indent.addScoped('{', '}', () {
indent.writeln('return $writerName(data: data)');
});
});
indent.newln();
// Generate Codec
indent.write(
'class $codecName: FlutterStandardMessageCodec, @unchecked Sendable ');
indent.addScoped('{', '}', () {
indent.writeln(
'static let shared = $codecName(readerWriter: $readerWriterName())');
});
indent.newln();
}