Browse Source

增加对可空枚举的支持。

Felix 2 years ago
parent
commit
0b3e038289
3 changed files with 55 additions and 28 deletions
  1. 23 1
      ModeType.cs
  2. 32 27
      ModeTypeGenerator.cs
  3. BIN
      WingInterfaceLibrary.dll

+ 23 - 1
ModeType.cs

@@ -166,7 +166,6 @@ namespace FlutterCodeGenerator
         }
     }
 
-
     public class BoolModeType : SimpleModelType
     {
         public BoolModeType(string name) : base(typeof(bool), name)
@@ -317,6 +316,10 @@ namespace FlutterCodeGenerator
                 {
                     source.AppendLine($"\t\t\t{child.Name_Lower}: {child.GetFlutterTypeName()}.values.firstWhere((e) => e.index == map['{child.Name_Upper}']),");
                 }
+                else if (child is EnumNullableModelType)
+                {
+                    source.AppendLine($"\t\t\t{child.Name_Lower}: map['{child.Name_Upper}'] != null ? {child.GetFlutterTypeName()}.values.firstWhere((e) => e.index == map['{child.Name_Upper}']) : null,");
+                }
                 else if (child is ArrayModelType)
                 {
                     var arrayTypeName = child.ParameterType.FullName.Replace("[]", string.Empty);
@@ -465,6 +468,25 @@ namespace FlutterCodeGenerator
         }
     }
 
+    public class EnumNullableModelType : ComplexModelType
+    {
+        public EnumNullableModelType(Type type, string name) : base(type, name)
+        {
+        }
+
+        public override string GetDartString()
+        {
+            var dartString = new StringBuilder();
+            dartString.AppendLine($"enum {GetFlutterTypeName()} {{");
+            foreach (var enumvalue in Enum.GetNames(ParameterType))
+            {
+                dartString.AppendLine($"\t{enumvalue},");
+            }
+            dartString.AppendLine("}");
+            return dartString.ToString();
+        }
+    }
+
     public class ArrayModelType : SimpleModelType
     {
         public ModelType Child;

+ 32 - 27
ModeTypeGenerator.cs

@@ -97,33 +97,6 @@ namespace FlutterCodeGenerator
                     var modelType = new ArrayModelType(type, argName);
                     return modelType;
                 }
-                else if (type.BaseType != typeof(object))
-                {
-                    _tempTypes[type] = duplicatedTime;
-                    var modelType = new UserDefinedDerivedModelType(type, argName);
-                    if (!CodeGenerator.ConflictModelTypeList.ContainsKey(modelType.ParameterType.Name))
-                    {
-                        CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name] = new Dictionary<ModelType, int>() { { modelType, 1 } };
-                    }
-                    else
-                    {
-                        if (!CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].Any(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
-                        {
-                            var index = CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].Count() + 1;
-                            modelType.Index = index;
-                            CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name][modelType] = index;
-                        }
-                        else
-                        {
-                            modelType.Index = CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].FirstOrDefault(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
-                        }
-                    }
-                    if (!ServiceMap.TemporaryList.Any(x => x.ParameterType == type))
-                    {
-                        ServiceMap.TemporaryList.Add(modelType);
-                    }
-                    return modelType;
-                }
                 else if (type.IsGenericType)
                 {
                     if (type.Name == "List`1")
@@ -136,6 +109,11 @@ namespace FlutterCodeGenerator
                         var modelType = new DictionaryModelType(type, argName);
                         return modelType;
                     }
+                    else if (type.Name == "Nullable`1")
+                    {
+                        var modelType = new EnumNullableModelType(type.GetGenericArguments()[0], argName);
+                        return modelType;
+                    }
                     else
                     {
                         _tempTypes[type] = duplicatedTime;
@@ -164,6 +142,33 @@ namespace FlutterCodeGenerator
                         return modelType;
                     }
                 }
+                else if (type.BaseType != typeof(object))
+                {
+                    _tempTypes[type] = duplicatedTime;
+                    var modelType = new UserDefinedDerivedModelType(type, argName);
+                    if (!CodeGenerator.ConflictModelTypeList.ContainsKey(modelType.ParameterType.Name))
+                    {
+                        CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name] = new Dictionary<ModelType, int>() { { modelType, 1 } };
+                    }
+                    else
+                    {
+                        if (!CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].Any(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
+                        {
+                            var index = CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].Count() + 1;
+                            modelType.Index = index;
+                            CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name][modelType] = index;
+                        }
+                        else
+                        {
+                            modelType.Index = CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].FirstOrDefault(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
+                        }
+                    }
+                    if (!ServiceMap.TemporaryList.Any(x => x.ParameterType == type))
+                    {
+                        ServiceMap.TemporaryList.Add(modelType);
+                    }
+                    return modelType;
+                }
                 else
                 {
                     _tempTypes[type] = duplicatedTime;

BIN
WingInterfaceLibrary.dll