ModeTypeGenerator.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace FlutterCodeGenerator
  5. {
  6. public class ModelTypeGenerator
  7. {
  8. public static Dictionary<Type, int> _tempTypes = new Dictionary<Type, int>();
  9. public static ModelType Create(Type type, string argName, bool clearTypes = false)
  10. {
  11. if (clearTypes)
  12. {
  13. _tempTypes.Clear();
  14. }
  15. var usedComplexModelTypeList = GenerateDataCache.Instance.GetCurrentServiceMap().UsedComplexModelTypeList;
  16. var conflictModelTypeList = GenerateDataCache.Instance.ConflictModelTypeList;
  17. if ((_tempTypes.ContainsKey(type) && _tempTypes[type] < 2) || !_tempTypes.ContainsKey(type))
  18. {
  19. var duplicatedTime = 0;
  20. if (_tempTypes.ContainsKey(type))
  21. {
  22. duplicatedTime = _tempTypes[type];
  23. }
  24. duplicatedTime++;
  25. if (type == typeof(int) || type == typeof(long) || type == typeof(short) || type == typeof(uint) || type == typeof(ulong) || type == typeof(ushort))
  26. {
  27. return new IntModeType(argName);
  28. }
  29. else if (type == typeof(int?) || type == typeof(long?) || type == typeof(short?) || type == typeof(uint?) || type == typeof(ulong?) || type == typeof(ushort?))
  30. {
  31. return new IntNullableModeType(argName);
  32. }
  33. else if (type == typeof(float) || type == typeof(double))
  34. {
  35. return new DoubleModeType(argName);
  36. }
  37. else if (type == typeof(float?) || type == typeof(double?))
  38. {
  39. return new DoubleNullableModeType(argName);
  40. }
  41. else if (type == typeof(bool))
  42. {
  43. return new BoolModeType(argName);
  44. }
  45. else if (type == typeof(bool?))
  46. {
  47. return new BoolNullableModeType(argName);
  48. }
  49. else if (type == typeof(string) || type == typeof(char) || type.BaseType?.Name == "BaseParamsString" || type.BaseType?.Name == "BaseString")
  50. {
  51. return new StringModeType(argName);
  52. }
  53. else if (type == typeof(DateTime) || type == typeof(DateTime?))
  54. {
  55. return new DateTimeModeType(argName);
  56. }
  57. else if (type == typeof(byte))
  58. {
  59. return new ByteModelType(argName);
  60. }
  61. else if (type == typeof(byte?))
  62. {
  63. return new ByteNullableModelType(argName);
  64. }
  65. else if (type == typeof(void))
  66. {
  67. return new VoidModelType(argName);
  68. }
  69. else if (type.IsEnum)
  70. {
  71. var modelType = new EnumModeType(type, argName);
  72. if (!conflictModelTypeList.ContainsKey(modelType.ParameterType.Name))
  73. {
  74. conflictModelTypeList[modelType.ParameterType.Name] = new Dictionary<ModelType, int>() { { modelType, 1 } };
  75. }
  76. else
  77. {
  78. if (!conflictModelTypeList[modelType.ParameterType.Name].Any(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
  79. {
  80. var index = conflictModelTypeList[modelType.ParameterType.Name].Count() + 1;
  81. modelType.Index = index;
  82. conflictModelTypeList[modelType.ParameterType.Name][modelType] = index;
  83. }
  84. else
  85. {
  86. modelType.Index = conflictModelTypeList[modelType.ParameterType.Name].FirstOrDefault(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
  87. }
  88. }
  89. if (!usedComplexModelTypeList.Any(x => x.ParameterType == type))
  90. {
  91. usedComplexModelTypeList.Add(modelType);
  92. }
  93. return modelType;
  94. }
  95. else if (type.IsArray)
  96. {
  97. var modelType = new ArrayModelType(type, argName);
  98. return modelType;
  99. }
  100. else if (type.IsGenericType)
  101. {
  102. if (type.Name == "List`1" || type.Name == "IList`1" || type.Name == "IEnumerable`1")
  103. {
  104. var modelType = new ListModelType(type, argName);
  105. return modelType;
  106. }
  107. else if (type.Name == "Dictionary`2")
  108. {
  109. var modelType = new DictionaryModelType(type, argName);
  110. return modelType;
  111. }
  112. else if (type.Name == "Nullable`1")
  113. {
  114. var modelType = new EnumNullableModelType(type.GetGenericArguments()[0], argName);
  115. if (!conflictModelTypeList.ContainsKey(modelType.ParameterType.Name))
  116. {
  117. conflictModelTypeList[modelType.ParameterType.Name] = new Dictionary<ModelType, int>() { { modelType, 1 } };
  118. }
  119. else
  120. {
  121. if (!conflictModelTypeList[modelType.ParameterType.Name].Any(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
  122. {
  123. var index = conflictModelTypeList[modelType.ParameterType.Name].Count() + 1;
  124. modelType.Index = index;
  125. conflictModelTypeList[modelType.ParameterType.Name][modelType] = index;
  126. }
  127. else
  128. {
  129. modelType.Index = conflictModelTypeList[modelType.ParameterType.Name].FirstOrDefault(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
  130. }
  131. }
  132. if (!usedComplexModelTypeList.Any(x => x.ParameterType == type))
  133. {
  134. usedComplexModelTypeList.Add(modelType);
  135. }
  136. return modelType;
  137. }
  138. else
  139. {
  140. _tempTypes[type] = duplicatedTime;
  141. var modelType = new UserDefinedGenericModelType(type, argName);
  142. if (!conflictModelTypeList.ContainsKey(modelType.ParameterType.Name))
  143. {
  144. conflictModelTypeList[modelType.ParameterType.Name] = new Dictionary<ModelType, int>() { { modelType, 1 } };
  145. }
  146. else
  147. {
  148. if (!conflictModelTypeList[modelType.ParameterType.Name].Any(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
  149. {
  150. var index = conflictModelTypeList[modelType.ParameterType.Name].Count() + 1;
  151. modelType.Index = index;
  152. conflictModelTypeList[modelType.ParameterType.Name][modelType] = index;
  153. }
  154. else
  155. {
  156. modelType.Index = conflictModelTypeList[modelType.ParameterType.Name].FirstOrDefault(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
  157. }
  158. }
  159. if (!usedComplexModelTypeList.Any(x => x.ParameterType == type))
  160. {
  161. usedComplexModelTypeList.Add(modelType);
  162. }
  163. return modelType;
  164. }
  165. }
  166. else if (type == typeof(object))
  167. {
  168. throw new Exception("Object is not supported, it should be specific type.");
  169. }
  170. else if (type.BaseType != typeof(object))
  171. {
  172. _tempTypes[type] = duplicatedTime;
  173. var modelType = new UserDefinedDerivedModelType(type, argName);
  174. if (!conflictModelTypeList.ContainsKey(modelType.ParameterType.Name))
  175. {
  176. conflictModelTypeList[modelType.ParameterType.Name] = new Dictionary<ModelType, int>() { { modelType, 1 } };
  177. }
  178. else
  179. {
  180. if (!conflictModelTypeList[modelType.ParameterType.Name].Any(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
  181. {
  182. var index = conflictModelTypeList[modelType.ParameterType.Name].Count() + 1;
  183. modelType.Index = index;
  184. conflictModelTypeList[modelType.ParameterType.Name][modelType] = index;
  185. }
  186. else
  187. {
  188. modelType.Index = conflictModelTypeList[modelType.ParameterType.Name].FirstOrDefault(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
  189. }
  190. }
  191. if (!usedComplexModelTypeList.Any(x => x.ParameterType == type))
  192. {
  193. usedComplexModelTypeList.Add(modelType);
  194. }
  195. return modelType;
  196. }
  197. else
  198. {
  199. _tempTypes[type] = duplicatedTime;
  200. var modelType = new UserDefinedModeType(type, argName);
  201. if (!conflictModelTypeList.ContainsKey(modelType.ParameterType.Name))
  202. {
  203. conflictModelTypeList[modelType.ParameterType.Name] = new Dictionary<ModelType, int>() { { modelType, 1 } };
  204. }
  205. else
  206. {
  207. if (!conflictModelTypeList[modelType.ParameterType.Name].Any(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
  208. {
  209. var index = conflictModelTypeList[modelType.ParameterType.Name].Count() + 1;
  210. modelType.Index = index;
  211. conflictModelTypeList[modelType.ParameterType.Name][modelType] = index;
  212. }
  213. else
  214. {
  215. modelType.Index = conflictModelTypeList[modelType.ParameterType.Name].FirstOrDefault(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
  216. }
  217. }
  218. if (!usedComplexModelTypeList.Any(x => x.ParameterType == type))
  219. {
  220. usedComplexModelTypeList.Add(modelType);
  221. }
  222. return modelType;
  223. }
  224. }
  225. else
  226. {
  227. var modelType = new ExtraObjectModelType(type, "");
  228. return modelType;
  229. }
  230. }
  231. }
  232. }