private List <string> GenerateReadMethod(Type type) { var code = new List <string>(); var ReturnClassName = type.CompilableClassName(); code.Add($"public static {ReturnClassName} Read(Serializer serializer)"); code.Add("{"); code.Add("var packer = serializer.Packer;"); code.Add($"return new {ReturnClassName}"); code.Add("{"); foreach (var p in PropertyFieldInfo.FindProperties(type)) { code.Add(GeneratePropertyRead(p)); } code.Add("};"); code.Add("}"); code.Add($"public static async Task<{ReturnClassName}> ReadAsync(Serializer serializer,"); code.Add("CancellationToken cancellation)"); code.Add("{"); code.Add("var packer = serializer.Packer;"); code.Add($"return new {ReturnClassName}"); code.Add("{"); foreach (var p in PropertyFieldInfo.FindProperties(type)) { code.Add(GeneratePropertyAsyncRead(p)); } code.Add("};"); code.Add("}"); return(code); }
private static List <string> ReadingAsyncCode( PropertyFieldInfo p, string statementRead, bool needCheckReference) { var code = new List <string>(); if (!needCheckReference) { code.Add($"result.{p.MemberName} = await {statementRead};"); return(code); } if (p.IsOptional) { code.Add($"result.{p.MemberName} = await {statementRead};"); } else { code.Add($"var value = await {statementRead};"); code.Add("if (value == null ) {"); code.Add($"throw new InvalidDataException(\"required member {p.MemberName} meets null data\");"); code.Add("}"); code.Add($"result.{p.MemberName} = value;"); } return(code); }
private static List <string> WrittingAsyncCode( PropertyFieldInfo p, string statementWrite, bool needCheckReference) { var code = new List <string>(); if (!needCheckReference) { code.Add($"await {statementWrite}"); return(code); } if (p.IsOptional) { code.Add($"await {statementWrite}"); } else { code.Add($"if (value.{p.MemberName} == null)"); code.Add("{"); code.Add($"throw new ArgumentNullException(\"{p.MemberName}\");"); code.Add("}"); code.Add($"await {statementWrite}"); } return(code); }
private List <string> GenerateWriteMethod(Type type) { var code = new List <string>(); code.Add($"public static void Write({type.CompilableClassName()} value,"); code.Add("Serializer2 serializer, Dictionary<Object,int> cache)"); code.Add("{"); code.Add("var packer = serializer.Packer;"); code.Add($"if (value == null)"); code.Add("{ packer.WriteInt32(0); return; }"); code.Add("{"); code.Add("if (cache.TryGetValue(value, out int id)) {"); code.Add("packer.WriteInt32(id);"); code.Add("return;"); code.Add("}}"); code.Add("{"); code.Add("var id = cache.Count + 1;"); code.Add("cache[value] = id;"); code.Add("packer.WriteInt32(id);"); code.Add("}"); foreach (var p in PropertyFieldInfo.FindProperties(type)) { code.Add("{"); code.AddRange(GeneratePropertyWrite(p)); code.Add("}"); } code.Add("}"); code.Add($"public static async Task WriteAsync({type.CompilableClassName()} value,"); code.Add("Serializer2 serializer, Dictionary<Object,int> cache, CancellationToken cancellation)"); code.Add("{"); code.Add("var packer = serializer.Packer;"); code.Add($"if (value == null)"); code.Add("{ await packer.WriteInt32Async(0, cancellation); return; }"); code.Add("{"); code.Add("if (cache.TryGetValue(value, out int id)) {"); code.Add("await packer.WriteInt32Async(id, cancellation);"); code.Add("return;"); code.Add("}}"); code.Add("{"); code.Add("var id = cache.Count + 1;"); code.Add("cache[value] = id;"); code.Add("await packer.WriteInt32Async(id, cancellation);"); code.Add("}"); foreach (var p in PropertyFieldInfo.FindProperties(type)) { code.Add("{"); code.AddRange(GeneratePropertyAsyncWrite(p)); code.Add("}"); } code.Add("}"); return(code); }
private string GeneratePropertyRead(PropertyFieldInfo p) { foreach (var gen in _statementGenerators) { if (gen.Matches(p)) { return(ReadingCode(p, gen.ReadingStatement(p))); } } throw new NotSupportedException($"{p.MemberType} of {p.MemberName} is not a supported type"); }
private static string ReadingAsyncCode(PropertyFieldInfo p, string statementRead) { if (p.IsOptional) { return($"{p.MemberName} = ((await packer.ReadBoolAsync(cancellation)) ? await {statementRead} : null),"); } else { return($"{p.MemberName} = await {statementRead},"); } }
private static string ReadingCode(PropertyFieldInfo p, string statementRead) { if (p.IsOptional) { return($"{p.MemberName} = (packer.ReadBool() ? {statementRead} : null),"); } else { return($"{p.MemberName} = {statementRead},"); } }
private List <string> GeneratePropertyAsyncRead(PropertyFieldInfo p) { foreach (var gen in _statementGenerators) { if (gen.Matches(p)) { return(ReadingAsyncCode(p, gen.ReadingAsyncStatement(p), gen.NeedCheckReference)); } } throw new NotSupportedException($"{p.MemberType} of {p.MemberName} is not a supported type"); }
private static List <string> WrittingAsyncCode(PropertyFieldInfo p, string statementWrite) { var code = new List <string>(); if (p.IsOptional) { code.Add($"if (value.{p.MemberName} == null) await packer.WriteBoolAsync(false, cancellation);"); code.Add("else { await packer.WriteBoolAsync(true, cancellation);"); code.Add($"await {statementWrite}"); code.Add("}"); } else { code.Add($"await {statementWrite}"); } return(code); }
private static List <string> WrittingCode(PropertyFieldInfo p, string statementWrite) { var code = new List <string>(); if (p.IsOptional) { code.Add($"if (value.{p.MemberName} == null) packer.WriteBool(false);"); code.Add("else { packer.WriteBool(true);"); code.Add(statementWrite); code.Add("}"); } else { code.Add(statementWrite); } return(code); }
private List <string> GenerateReadMethod(Type type) { var code = new List <string>(); var ReturnClassName = type.CompilableClassName(); code.Add($"public static {ReturnClassName} Read(Serializer2 serializer, Dictionary<int, Object> cache)"); code.Add("{"); code.Add("var packer = serializer.Packer;"); code.Add("var refId = packer.ReadInt32();"); code.Add("if (refId == 0 ) return null;"); code.Add($"if (cache.TryGetValue(refId, out object ret)) return ({ReturnClassName})ret;"); code.Add($"var result = new {ReturnClassName}();"); code.Add("cache[refId] = result;"); foreach (var p in PropertyFieldInfo.FindProperties(type)) { code.Add("{"); code.AddRange(GeneratePropertyRead(p)); code.Add("};"); } code.Add("return result;"); code.Add("}"); code.Add($"public static async Task<{ReturnClassName}> ReadAsync(Serializer2 serializer,"); code.Add("Dictionary<int, Object> cache, CancellationToken cancellation)"); code.Add("{"); code.Add("var packer = serializer.Packer;"); code.Add("var refId = await packer.ReadInt32Async(cancellation);"); code.Add("if (refId == 0 ) return null;"); code.Add($"if (cache.TryGetValue(refId, out object ret)) return ({ReturnClassName})ret;"); code.Add($"var result = new {ReturnClassName}();"); code.Add("cache[refId] = result;"); foreach (var p in PropertyFieldInfo.FindProperties(type)) { code.Add("{"); code.AddRange(GeneratePropertyAsyncRead(p)); code.Add("};"); } code.Add("return result;"); code.Add("}"); return(code); }
private List <string> GenerateWriteMethod(Type type) { var code = new List <string>(); code.Add($"public static void Write({type.CompilableClassName()} value, Serializer serializer)"); code.Add("{"); code.Add("var packer = serializer.Packer;"); foreach (var p in PropertyFieldInfo.FindProperties(type)) { code.AddRange(GeneratePropertyWrite(p)); } code.Add("}"); code.Add($"public static async Task WriteAsync({type.CompilableClassName()} value,"); code.Add("Serializer serializer, CancellationToken cancellation)"); code.Add("{"); code.Add("var packer = serializer.Packer;"); foreach (var p in PropertyFieldInfo.FindProperties(type)) { code.AddRange(GeneratePropertyAsyncWrite(p)); } code.Add("}"); return(code); }
public bool Matches(PropertyFieldInfo p) { return(p.MemberType.Name == "Dictionary`2" && p.MemberType.IsGenericType); }
public bool Matches(PropertyFieldInfo p) { return(p.MemberType.IsArray && p.MemberType.GetArrayRank() == 1 && p.MemberType.HasElementType); }
public string ReadingStatement(PropertyFieldInfo p) { return($"CollectionPacker2.ReadArray{p.GenericParams()}(serializer, cache)"); }
public string ReadingAsyncStatement(PropertyFieldInfo p) { return($"CollectionPacker2.ReadStringAsync(serializer, cache, cancellation)"); }
public string WrittingAsyncStatement(PropertyFieldInfo p) { return($"CollectionPacker2.WriteStringAsync(value.{p.MemberName}, serializer, cache, cancellation);"); }
public string ReadingStatement(PropertyFieldInfo p) { return($"CollectionPacker2.ReadString(serializer, cache)"); }
public string WrittingStatement(PropertyFieldInfo p) { return($"CollectionPacker2.WriteDict{p.GenericParams()}(value.{p.MemberName}, serializer, cache);"); }
public string ReadingAsyncStatement(PropertyFieldInfo p) { return($"CollectionPacker2.ReadNullableAsync{p.GenericParams()}(serializer, cancellation)"); }
public bool Matches(PropertyFieldInfo p) { return(p.MemberType.Name == "Nullable`1" && p.MemberType.IsGenericType); }
public string ReadingAsyncStatement(PropertyFieldInfo p) { return($"CollectionPacker2.ReadEnumAsync<{p.MemberType.CompilableClassName()}>(serializer, cancellation)"); }
public bool Matches(PropertyFieldInfo p) { return(p.MemberType.IsEnum); }
public string ReadingStatement(PropertyFieldInfo p) { return($"{p.MemberType.GeneratedClassName2()}.Read(serializer, cache)"); }
public string WrittingAsyncStatement(PropertyFieldInfo p) { return($"CollectionPacker2.WriteEnumAsync<{p.MemberType.CompilableClassName()}>(value.{p.MemberName}, serializer, cancellation);"); }
public string ReadingAsyncStatement(PropertyFieldInfo p) { return($"{p.MemberType.GeneratedClassName2()}.ReadAsync(serializer, cache, cancellation)"); }
public string ReadingStatement(PropertyFieldInfo p) { return($"CollectionPacker2.ReadNullable{p.GenericParams()}(serializer)"); }
public string WrittingAsyncStatement(PropertyFieldInfo p) { return($"{p.MemberType.GeneratedClassName2()}.WriteAsync(value.{p.MemberName}, serializer, cache, cancellation);"); }
public string WrittingAsyncStatement(PropertyFieldInfo p) { return($"CollectionPacker2.WriteNullableAsync{p.GenericParams()}(value.{p.MemberName}, serializer, cancellation);"); }
public bool Matches(PropertyFieldInfo p) { return(p.MemberType == typeof(string)); }