Parcourir la source

代码优化。

Felix il y a 2 ans
Parent
commit
06e022fc81
2 fichiers modifiés avec 75 ajouts et 2 suppressions
  1. 10 0
      MethodMap.cs
  2. 65 2
      ServiceMap.cs

+ 10 - 0
MethodMap.cs

@@ -71,6 +71,11 @@ namespace FlutterCodeGenerator
                         dartString.AppendLine($"\t\tvar result = (rpcRst as List).cast<{simpleModelType.GetFlutterTypeName()}>().toList();");
                         dartString.AppendLine("\t\treturn result;");
                     }
+                    else if (genericModelType.GenericArgumentModelType is EnumModeType enumModelType)
+                    {
+                        dartString.AppendLine($"\t\tvar result = (rpcRst as List).map((e)=>{enumModelType.GetFlutterTypeName()}.values.firstWhere((i) => i.index == e)).toList();");
+                        dartString.AppendLine("\t\treturn result;");
+                    }
                     else if (genericModelType.GenericArgumentModelType is UserDefinedModeType userDefinedComplexModeType)
                     {
                         dartString.AppendLine($"\t\tvar result = (rpcRst as List).map((e)=>{userDefinedComplexModeType.GetFlutterTypeName()}.fromJson(e as Map<String, dynamic>)).toList();");
@@ -89,6 +94,11 @@ namespace FlutterCodeGenerator
                         dartString.AppendLine($"\t\tvar result = (rpcRst as List).cast<{simpleModelType.GetFlutterTypeName()}>().toList();");
                         dartString.AppendLine("\t\treturn result;");
                     }
+                    else if (arrayModelType.Child is EnumModeType enumModelType)
+                    {
+                        dartString.AppendLine($"\t\tvar result = (rpcRst as List).map((e)=>{enumModelType.GetFlutterTypeName()}.values.firstWhere((i) => i.index == e)).toList();");
+                        dartString.AppendLine("\t\treturn result;");
+                    }
                     else if (arrayModelType.Child is UserDefinedModeType userDefinedComplexModeType)
                     {
                         dartString.AppendLine($"\t\tvar result = (rpcRst as List).map((e)=>{userDefinedComplexModeType.GetFlutterTypeName()}.fromJson(e as Map<String, dynamic>)).toList();");

+ 65 - 2
ServiceMap.cs

@@ -62,7 +62,7 @@ namespace FlutterCodeGenerator
             var serviceModelDartString = new StringBuilder();
             foreach (var importService in importServiceList)
             {
-                serviceModelDartString.AppendLine($"import '{LetterConverterHelper.FirstCharToLower(importService[0..^7]) }.m.dart';");
+                serviceModelDartString.AppendLine($"import '{LetterConverterHelper.FirstCharToLower(importService[0..^7])}.m.dart';");
             }
             if (importServiceList.Count > 0)
             {
@@ -107,6 +107,69 @@ namespace FlutterCodeGenerator
                         }
                     }
                 }
+                var returnModelType = methodMap.ReturnParameterModelType;
+                if (returnModelType is ComplexModelType)
+                {
+                    importService = CodeGenerator.AlreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == returnModelType.ParameterType.Name && x.Key.ParameterType.Namespace == returnModelType.ParameterType.Namespace).Value;
+                    if (importService == null)
+                    {
+                        throw new Exception("Import Service is null");
+                    }
+                    if (importService != _serviceName && !importServiceList.Contains(importService))
+                    {
+                        importServiceList.Add(importService);
+                    }
+                }
+                else if (returnModelType is ListModelType listModelType)
+                {
+                    var subReturnType = listModelType.GenericArgumentModelType;
+                    if (subReturnType is ComplexModelType)
+                    {
+                        importService = CodeGenerator.AlreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == subReturnType.ParameterType.Name && x.Key.ParameterType.Namespace == subReturnType.ParameterType.Namespace).Value;
+                        if (importService == null)
+                        {
+                            throw new Exception("Import Service is null");
+                        }
+                        if (importService != _serviceName && !importServiceList.Contains(importService))
+                        {
+                            importServiceList.Add(importService);
+                        }
+                    }
+                }
+                else if (returnModelType is ArrayModelType arrayModelType)
+                {
+                    var subReturnType = arrayModelType.Child;
+                    if (subReturnType is ComplexModelType)
+                    {
+                        importService = CodeGenerator.AlreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == subReturnType.ParameterType.Name && x.Key.ParameterType.Namespace == subReturnType.ParameterType.Namespace).Value;
+                        if (importService == null)
+                        {
+                            throw new Exception("Import Service is null");
+                        }
+                        if (importService != _serviceName && !importServiceList.Contains(importService))
+                        {
+                            importServiceList.Add(importService);
+                        }
+                    }
+                }
+                else if (returnModelType is DictionaryModelType dictionaryModelType)
+                {
+                    foreach (var subReturnType in dictionaryModelType.GenericArgumentModelTypeList)
+                    {
+                        if (subReturnType is ComplexModelType)
+                        {
+                            importService = CodeGenerator.AlreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == subReturnType.ParameterType.Name && x.Key.ParameterType.Namespace == subReturnType.ParameterType.Namespace).Value;
+                            if (importService == null)
+                            {
+                                throw new Exception("Import Service is null");
+                            }
+                            if (importService != _serviceName && !importServiceList.Contains(importService))
+                            {
+                                importServiceList.Add(importService);
+                            }
+                        }
+                    }
+                }
             }
 
             var complexReturnTypeNameList = GetUserDefinedComplexReturnTypeList();
@@ -134,7 +197,7 @@ namespace FlutterCodeGenerator
 
             foreach (var importService in importServiceList)
             {
-                serviceDartString.AppendLine($"import '{LetterConverterHelper.FirstCharToLower(importService[0..^7]) }.m.dart';");
+                serviceDartString.AppendLine($"import '{LetterConverterHelper.FirstCharToLower(importService[0..^7])}.m.dart';");
             }
             if (importServiceList.Count > 0)
             {