예제 #1
0
        private string ReadFieldType(string resultName, ClassType classType, int depth)
        {
            if (classType.CollectionType == CollectionType.NotACollection)
            {
                if (manager.IsKnownClassOrBase(classType.Name))
                {
                    var readName = this.MakeReadValueMethod(classType.Name);
                    if (classType.GenericTypes.Any())
                    {
                        var generics = this.MakeGenericType(classType);
                        return($"{readName}<{generics}>(input)");
                    }
                    return($"{readName}(input)");
                }
                else
                {
                    var primitiveType = CodeGeneratorUtils.GetPrimitiveName(classType.Name);
                    return($"input.Read{primitiveType}()");
                }
            }
            else if (classType.CollectionType == CollectionType.Enum)
            {
                var primitiveType = CodeGeneratorUtils.GetPrimitiveName(classType.EnumUnderlayingType.Name);
                return($"({classType.Name.Value})input.Read{primitiveType}()");
            }
            else if (classType.CollectionType == CollectionType.List || classType.CollectionType == CollectionType.Array)
            {
                var genericType     = classType.GenericTypes.First();
                var genericTypeName = this.MakeGenericType(classType);
                resultName = CodeGeneratorUtils.ToCamelCase(resultName);

                if (manager.IsKnownClassOrBase(genericType.Name))
                {
                    var readName = this.MakeReadValueMethod(genericType.Name);
                    if (genericType.GenericTypes.Any())
                    {
                        var generics = this.MakeGenericType(genericType);
                        readName += $"<{generics}>";
                    }

                    writer.WriteLine($"var {resultName} = new {genericTypeName}(ReadList(input, {readName}));");
                }
                else if (TryGetReadListPrimitive(genericType.Name, out var readListType))
                {
                    writer.WriteLine($"var {resultName} = new {genericTypeName}(ReadList{readListType}(input));");
                }
                else
                {
                    var countName   = $"count{CodeGeneratorUtils.ToTitleCase(resultName)}";
                    var depthStr    = depth == 0 ? "" : depth.ToString();
                    var indexString = CodeGeneratorUtils.MakeIndexIterator(depth);
                    resultName = CodeGeneratorUtils.ToCamelCase(resultName);

                    writer.WriteLine($"var {countName} = input.ReadInt32();");
                    writer.WriteLine($"var {resultName} = new {genericTypeName}({countName});");
                    writer.WriteLine($"for (var {indexString} = 0; {indexString} < {countName}; {indexString}++)");
                    writer.WriteLine("{");
                    writer.Indent++;
                    writer.WriteLine($"{resultName}.Add({ReadFieldType(resultName + indexString.ToUpperInvariant(), genericType, depth + 1)});");
                    writer.Indent--;
                    writer.WriteLine("}");
                }
                return(resultName);
            }
            else if (classType.CollectionType == CollectionType.Dictionary)
            {
                var countName   = $"count{CodeGeneratorUtils.ToTitleCase(resultName)}";
                var keyType     = classType.GenericTypes[0];
                var valueType   = classType.GenericTypes[1];
                var depthStr    = depth == 0 ? "" : depth.ToString();
                var keyName     = $"key{depthStr}";
                var valueName   = $"value{depthStr}";
                var indexName   = CodeGeneratorUtils.MakeIndexIterator(depth);
                var genericName = this.MakeGenericType(classType);
                resultName = CodeGeneratorUtils.ToCamelCase(resultName);

                writer.WriteLine($"var {countName} = input.ReadInt32();");
                writer.WriteLine($"var {resultName} = new {genericName}();");
                writer.WriteLine($"for (var {indexName} = 0; {indexName} < {countName}; {indexName}++)");
                writer.WriteLine("{");
                writer.Indent++;
                writer.WriteLine($"var {keyName} = {ReadFieldType(keyName, keyType, depth + 1)};");
                writer.WriteLine($"var {valueName} = {ReadFieldType(valueName, valueType, depth + 1)};");
                writer.WriteLine($"{resultName}[{keyName}] = {valueName};");
                writer.Indent--;
                writer.WriteLine("}");

                return(resultName);
            }

            return("OH NO");
        }
예제 #2
0
        private string ReadFieldType(string input, string resultName, ClassType classType, int depth)
        {
            if (classType.CollectionType == CollectionType.NotACollection)
            {
                if (manager.IsKnownClassOrBase(classType.Name))
                {
                    var readName = this.MakeReadValueMethod(classType.Name);
                    if (classType.GenericTypes.Any())
                    {
                        var generics = this.MakeGenericType(classType);
                        return($"{readName}<{generics}>({input})");
                    }
                    return($"{readName}({input})");
                }
                else
                {
                    var primitiveType = CodeGeneratorUtils.GetPrimitiveName(classType.Name);
                    return($"{input}.Get{primitiveType}()");
                }
            }
            else if (classType.CollectionType == CollectionType.Enum)
            {
                var primitiveType = CodeGeneratorUtils.GetPrimitiveName(classType.EnumUnderlayingType.Name);
                return($"({classType.Name.Value}){input}.Get{primitiveType}()");
            }
            else if (classType.CollectionType == CollectionType.List || classType.CollectionType == CollectionType.Array)
            {
                var genericType     = classType.GenericTypes.First();
                var genericTypeName = this.MakeGenericType(classType);
                var depthStr        = depth == 0 ? "" : depth.ToString();
                resultName = CodeGeneratorUtils.ToCamelCase(resultName);
                var indexName = $"value{depthStr}";

                writer.WriteLine($"var {resultName}Json = {input};");
                if (manager.IsKnownClassOrBase(genericType.Name))
                {
                    var readName = this.MakeReadValueMethod(genericType.Name);
                    if (genericType.GenericTypes.Any())
                    {
                        var generics = this.MakeGenericType(genericType);
                        readName += $"<{generics}>";
                    }

                    writer.WriteLine($"var {resultName} = new {genericTypeName}(ReadList({resultName}Json, {readName}));");
                }
                else if (TryGetReadListPrimitive(genericType.Name, out var readListType))
                {
                    writer.WriteLine($"var {resultName} = new {genericTypeName}(ReadList{readListType}({resultName}Json));");
                }
                else
                {
                    writer.WriteLine($"var {resultName} = new {genericTypeName}();");
                    writer.WriteLine($"foreach (var {indexName} in {resultName}Json.EnumerateArray())");
                    writer.WriteLine("{");
                    writer.Indent++;
                    writer.WriteLine($"{resultName}.Add({ReadFieldType(indexName, resultName + (depth + 1), genericType, depth + 1)});");
                    writer.Indent--;
                    writer.WriteLine("}");
                }
                writer.WriteLine();

                return(resultName);
            }
            else if (classType.CollectionType == CollectionType.Dictionary)
            {
                var keyType     = classType.GenericTypes[0];
                var valueType   = classType.GenericTypes[1];
                var depthStr    = depth == 0 ? "" : depth.ToString();
                var indexName   = $"prop{depthStr}";
                var keyName     = $"key{depthStr}";
                var valueName   = $"value{depthStr}";
                var genericName = this.MakeGenericType(classType);
                resultName = CodeGeneratorUtils.ToCamelCase(resultName);


                writer.WriteLine($"var {resultName} = new {genericName}();");

                if (!TryGetBasicJsonType(classType.GenericTypes[0].Name, out var jsonType))
                {
                    var indexKeyName   = $"{indexName}.GetProperty(\"key\")";
                    var indexValueName = $"{indexName}.GetProperty(\"value\")";

                    writer.WriteLine($"foreach (var {indexName} in {input}.EnumerateArray())");
                    writer.WriteLine("{");
                    writer.Indent++;

                    writer.WriteLine($"var {keyName} = {ReadFieldType(indexKeyName, keyName, keyType, depth + 1)};");
                    writer.WriteLine($"var {valueName} = {ReadFieldType(indexValueName, valueName, valueType, depth + 1)};");

                    writer.WriteLine($"{resultName}[{keyName}] = {valueName};");

                    writer.Indent--;
                    writer.WriteLine("}");
                }
                else
                {
                    writer.WriteLine($"foreach (var {indexName} in {input}.EnumerateObject())");
                    writer.WriteLine("{");
                    writer.Indent++;

                    if (jsonType == "String")
                    {
                        writer.WriteLine($"var {keyName} = {indexName}.Name;");
                    }
                    else
                    {
                        var primitiveType = CodeGeneratorUtils.GetPrimitiveName(keyType.Name);
                        writer.WriteLine($"var {keyName} = Convert.To{primitiveType}({indexName}.Name);");
                    }
                    writer.WriteLine($"var {valueName} = {ReadFieldType($"{indexName}.Value", valueName, valueType, depth + 1)};");

                    writer.WriteLine($"{resultName}[{keyName}] = {valueName};");

                    writer.Indent--;
                    writer.WriteLine("}");
                }

                return(resultName);
            }

            return("OH NO");
        }