writeGeneralCodec method
void
writeGeneralCodec(
- KotlinOptions generatorOptions,
- Root root,
- Indent indent, {
- required String dartPackageName,
override
Writes the custom codec to indent
.
Implementation
@override
void writeGeneralCodec(
KotlinOptions generatorOptions,
Root root,
Indent indent, {
required String dartPackageName,
}) {
final List<EnumeratedType> enumeratedTypes =
getEnumeratedTypes(root).toList();
void writeEncodeLogic(EnumeratedType customType) {
final String encodeString =
customType.type == CustomTypes.customClass ? 'toList()' : 'raw';
final String valueString = customType.enumeration < maximumCodecFieldKey
? 'value.$encodeString'
: 'wrap.toList()';
final int enumeration = customType.enumeration < maximumCodecFieldKey
? customType.enumeration
: maximumCodecFieldKey;
indent.writeScoped('is ${customType.name} -> {', '}', () {
if (customType.enumeration >= maximumCodecFieldKey) {
indent.writeln(
'val wrap = ${generatorOptions.fileSpecificClassNameComponent}$_overflowClassName(type = ${customType.enumeration - maximumCodecFieldKey}, wrapped = value.$encodeString)');
}
indent.writeln('stream.write($enumeration)');
indent.writeln('writeValue(stream, $valueString)');
});
}
void writeDecodeLogic(EnumeratedType customType) {
indent.write('${customType.enumeration}.toByte() -> ');
indent.addScoped('{', '}', () {
if (customType.type == CustomTypes.customClass) {
indent.write('return (readValue(buffer) as? List<Any?>)?.let ');
indent.addScoped('{', '}', () {
indent.writeln('${customType.name}.fromList(it)');
});
} else if (customType.type == CustomTypes.customEnum) {
indent.write('return (readValue(buffer) as Int?)?.let ');
indent.addScoped('{', '}', () {
indent.writeln('${customType.name}.ofRaw(it)');
});
}
});
}
final EnumeratedType overflowClass = EnumeratedType(
'${generatorOptions.fileSpecificClassNameComponent}$_overflowClassName',
maximumCodecFieldKey,
CustomTypes.customClass);
if (root.requiresOverflowClass) {
_writeCodecOverflowUtilities(
generatorOptions,
root,
indent,
enumeratedTypes,
dartPackageName: dartPackageName,
);
}
indent.write(
'private object ${generatorOptions.fileSpecificClassNameComponent}$_codecName : StandardMessageCodec() ');
indent.addScoped('{', '}', () {
indent.write(
'override fun readValueOfType(type: Byte, buffer: ByteBuffer): Any? ');
indent.addScoped('{', '}', () {
indent.write('return ');
if (root.classes.isNotEmpty || root.enums.isNotEmpty) {
indent.add('when (type) ');
indent.addScoped('{', '}', () {
for (final EnumeratedType customType in enumeratedTypes) {
if (customType.enumeration < maximumCodecFieldKey) {
writeDecodeLogic(customType);
}
}
if (root.requiresOverflowClass) {
writeDecodeLogic(overflowClass);
}
indent.writeln('else -> super.readValueOfType(type, buffer)');
});
} else {
indent.writeln('super.readValueOfType(type, buffer)');
}
});
indent.write(
'override fun writeValue(stream: ByteArrayOutputStream, value: Any?) ');
indent.writeScoped('{', '}', () {
if (root.classes.isNotEmpty || root.enums.isNotEmpty) {
indent.write('when (value) ');
indent.addScoped('{', '}', () {
enumeratedTypes.forEach(writeEncodeLogic);
indent.writeln('else -> super.writeValue(stream, value)');
});
} else {
indent.writeln('super.writeValue(stream, value)');
}
});
});
indent.newln();
}