Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
 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");
 }
Ejemplo n.º 6
0
 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},");
     }
 }
Ejemplo n.º 7
0
 private static string ReadingCode(PropertyFieldInfo p, string statementRead)
 {
     if (p.IsOptional)
     {
         return($"{p.MemberName} = (packer.ReadBool() ? {statementRead} : null),");
     }
     else
     {
         return($"{p.MemberName} = {statementRead},");
     }
 }
Ejemplo n.º 8
0
 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");
 }
Ejemplo n.º 9
0
        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);
        }
Ejemplo n.º 10
0
        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);
        }
Ejemplo n.º 11
0
        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);
        }
Ejemplo n.º 12
0
        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);
        }
Ejemplo n.º 13
0
 public bool Matches(PropertyFieldInfo p)
 {
     return(p.MemberType.Name == "Dictionary`2" && p.MemberType.IsGenericType);
 }
Ejemplo n.º 14
0
 public bool Matches(PropertyFieldInfo p)
 {
     return(p.MemberType.IsArray && p.MemberType.GetArrayRank() == 1 && p.MemberType.HasElementType);
 }
Ejemplo n.º 15
0
 public string ReadingStatement(PropertyFieldInfo p)
 {
     return($"CollectionPacker2.ReadArray{p.GenericParams()}(serializer, cache)");
 }
Ejemplo n.º 16
0
 public string ReadingAsyncStatement(PropertyFieldInfo p)
 {
     return($"CollectionPacker2.ReadStringAsync(serializer, cache, cancellation)");
 }
Ejemplo n.º 17
0
 public string WrittingAsyncStatement(PropertyFieldInfo p)
 {
     return($"CollectionPacker2.WriteStringAsync(value.{p.MemberName}, serializer, cache, cancellation);");
 }
Ejemplo n.º 18
0
 public string ReadingStatement(PropertyFieldInfo p)
 {
     return($"CollectionPacker2.ReadString(serializer, cache)");
 }
Ejemplo n.º 19
0
 public string WrittingStatement(PropertyFieldInfo p)
 {
     return($"CollectionPacker2.WriteDict{p.GenericParams()}(value.{p.MemberName}, serializer, cache);");
 }
Ejemplo n.º 20
0
 public string ReadingAsyncStatement(PropertyFieldInfo p)
 {
     return($"CollectionPacker2.ReadNullableAsync{p.GenericParams()}(serializer, cancellation)");
 }
Ejemplo n.º 21
0
 public bool Matches(PropertyFieldInfo p)
 {
     return(p.MemberType.Name == "Nullable`1" && p.MemberType.IsGenericType);
 }
Ejemplo n.º 22
0
 public string ReadingAsyncStatement(PropertyFieldInfo p)
 {
     return($"CollectionPacker2.ReadEnumAsync<{p.MemberType.CompilableClassName()}>(serializer, cancellation)");
 }
Ejemplo n.º 23
0
 public bool Matches(PropertyFieldInfo p)
 {
     return(p.MemberType.IsEnum);
 }
Ejemplo n.º 24
0
 public string ReadingStatement(PropertyFieldInfo p)
 {
     return($"{p.MemberType.GeneratedClassName2()}.Read(serializer, cache)");
 }
Ejemplo n.º 25
0
 public string WrittingAsyncStatement(PropertyFieldInfo p)
 {
     return($"CollectionPacker2.WriteEnumAsync<{p.MemberType.CompilableClassName()}>(value.{p.MemberName}, serializer, cancellation);");
 }
Ejemplo n.º 26
0
 public string ReadingAsyncStatement(PropertyFieldInfo p)
 {
     return($"{p.MemberType.GeneratedClassName2()}.ReadAsync(serializer, cache, cancellation)");
 }
Ejemplo n.º 27
0
 public string ReadingStatement(PropertyFieldInfo p)
 {
     return($"CollectionPacker2.ReadNullable{p.GenericParams()}(serializer)");
 }
Ejemplo n.º 28
0
 public string WrittingAsyncStatement(PropertyFieldInfo p)
 {
     return($"{p.MemberType.GeneratedClassName2()}.WriteAsync(value.{p.MemberName}, serializer, cache, cancellation);");
 }
Ejemplo n.º 29
0
 public string WrittingAsyncStatement(PropertyFieldInfo p)
 {
     return($"CollectionPacker2.WriteNullableAsync{p.GenericParams()}(value.{p.MemberName}, serializer, cancellation);");
 }
Ejemplo n.º 30
0
 public bool Matches(PropertyFieldInfo p)
 {
     return(p.MemberType == typeof(string));
 }