writeGeneralCodec method
void
writeGeneralCodec(
- DartOptions generatorOptions,
- Root root,
- Indent indent, {
- 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) {
indent.writeScoped('if (value is ${customType.name}) {', '} else ', () {
if (customType.enumeration < maximumCodecFieldKey) {
indent.writeln('buffer.putUint8(${customType.enumeration});');
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.enumeration - maximumCodecFieldKey}, wrapped: value$encodeString);');
indent.writeln('buffer.putUint8($maximumCodecFieldKey);');
indent.writeln('writeValue(buffer, wrap.encode());');
}
}, addTrailingNewline: false);
}
void writeDecodeLogic(EnumeratedType customType) {
indent.writeln('case ${customType.enumeration}: ');
indent.nest(1, () {
if (customType.type == CustomTypes.customClass) {
if (customType.enumeration == 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).toList();
if (root.requiresOverflowClass) {
_writeCodecOverflowUtilities(indent, enumeratedTypes);
}
indent.newln();
indent.write('class $_pigeonCodec extends StandardMessageCodec');
indent.addScoped(' {', '}', () {
indent.writeln('const $_pigeonCodec();');
if (enumeratedTypes.isNotEmpty) {
indent.writeln('@override');
indent.write('void writeValue(WriteBuffer buffer, Object? value) ');
indent.addScoped('{', '}', () {
enumerate(enumeratedTypes,
(int index, final EnumeratedType customType) {
writeEncodeLogic(customType);
});
indent.addScoped('{', '}', () {
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('{', '}', () {
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.readValueOfType(type, buffer);');
});
});
});
}
});
}