private async Task <string> WriteTypeSerializerMethodAsync( CodeWriter writer, IType type, ISet <string> generatedMethods) { IType actualType = type.IsNonNullType() ? type.InnerType() : type; string serializerName = SerializerNameUtils.CreateSerializerName(actualType); if (!generatedMethods.Add(serializerName)) { return(serializerName); } if (actualType.IsListType()) { IType elementType = type.ElementType(); string itemSerializer = await WriteTypeSerializerMethodAsync( writer, elementType, generatedMethods) .ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); await WriteTypeSerializerMethodHeaderAsync(writer, serializerName) .ConfigureAwait(false); await writer.WriteIndentedLineAsync("{").ConfigureAwait(false); using (writer.IncreaseIndent()) { if (!type.IsNonNullType()) { await WriteNonNullHandlingAsync(writer).ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); } await writer.WriteIndentedLineAsync( "IList source = (IList)value;") .ConfigureAwait(false); if (_languageVersion == LanguageVersion.CSharp_8_0) { await writer.WriteIndentedLineAsync( "object?[] result = new object?[source.Count];") .ConfigureAwait(false); } else { await writer.WriteIndentedLineAsync( "object[] result = new object[source.Count];") .ConfigureAwait(false); } await writer.WriteIndentedLineAsync( "for(int i = 0; i < source.Count; i++)") .ConfigureAwait(false); await writer.WriteIndentedLineAsync("{").ConfigureAwait(false); using (writer.IncreaseIndent()) { await writer.WriteIndentedLineAsync( $"result[i] = {itemSerializer}(source[i]);") .ConfigureAwait(false); } await writer.WriteIndentedLineAsync("}").ConfigureAwait(false); await writer.WriteIndentedLineAsync("return result;").ConfigureAwait(false); } await writer.WriteIndentedLineAsync("}").ConfigureAwait(false); } else { await WriteTypeSerializerMethodHeaderAsync(writer, serializerName) .ConfigureAwait(false); await writer.WriteIndentedLineAsync("{").ConfigureAwait(false); using (writer.IncreaseIndent()) { if (!type.IsNonNullType()) { await WriteNonNullHandlingAsync(writer).ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); } await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync( "return _") .ConfigureAwait(false); await writer.WriteAsync(GetFieldName(actualType.NamedType().Name)) .ConfigureAwait(false); await writer.WriteAsync( _languageVersion == LanguageVersion.CSharp_8_0 ? "Serializer!.Serialize(value);" : "Serializer.Serialize(value);") .ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); } await writer.WriteIndentedLineAsync("}").ConfigureAwait(false); } return(serializerName); }
private async Task WriteSerializeMethodAsync( CodeWriter writer, IInputClassDescriptor descriptor) { await writer.WriteIndentAsync().ConfigureAwait(false); if (_languageVersion == LanguageVersion.CSharp_8_0) { await writer.WriteAsync("public object? Serialize(object? value)") .ConfigureAwait(false); } else { await writer.WriteAsync("public object Serialize(object value)") .ConfigureAwait(false); } await writer.WriteLineAsync().ConfigureAwait(false); await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync('{').ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); using (writer.IncreaseIndent()) { await writer.WriteIndentedLineAsync( "if (_needsInitialization)") .ConfigureAwait(false); await writer.WriteIndentedLineAsync("{") .ConfigureAwait(false); using (writer.IncreaseIndent()) { await writer.WriteIndentedLineAsync( "throw new InvalidOperationException(") .ConfigureAwait(false); using (writer.IncreaseIndent()) { await writer.WriteIndentedLineAsync( "$\"The serializer for type `{Name}` has not been initialized.\");") .ConfigureAwait(false); } } await writer.WriteIndentedLineAsync("}") .ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); await WriteNonNullHandlingAsync(writer).ConfigureAwait(false); await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync($"var input = ({descriptor.Name})value;") .ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); await writer.WriteIndentAsync().ConfigureAwait(false); if (_languageVersion == LanguageVersion.CSharp_8_0) { await writer.WriteAsync("var map = new Dictionary<string, object?>();") .ConfigureAwait(false); } else { await writer.WriteAsync("var map = new Dictionary<string, object>();") .ConfigureAwait(false); } await writer.WriteLineAsync().ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); foreach (IInputFieldDescriptor field in descriptor.Fields) { IType type = field.Type.IsNonNullType() ? field.Type.InnerType() : field.Type; string serializerName = SerializerNameUtils.CreateSerializerName(type); await writer.WriteIndentedLineAsync( $"if (input.{GetPropertyName(field.Name)}.HasValue)") .ConfigureAwait(false); await writer.WriteIndentedLineAsync("{").ConfigureAwait(false); using (writer.IncreaseIndent()) { await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync($"map.Add(\"{field.Field.Name}\", ") .ConfigureAwait(false); await writer.WriteAsync($"{serializerName}(").ConfigureAwait(false); await writer.WriteAsync($"input.{GetPropertyName(field.Name)}.Value));") .ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); } await writer.WriteIndentedLineAsync("}").ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); } await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync("return map;").ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); } await writer.WriteIndentAsync().ConfigureAwait(false); await writer.WriteAsync('}').ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); await writer.WriteLineAsync().ConfigureAwait(false); await WriteTypeSerializerMethodsAsync(writer, descriptor).ConfigureAwait(false); }