ServiceMap.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace FlutterCodeGenerator
  6. {
  7. internal class ServiceMap
  8. {
  9. public List<MethodMap> MethodMapList;
  10. public static List<ComplexModelType> TemporaryList { get; set; }
  11. private List<ComplexModelType> _usedComplexModelTypeList;
  12. private List<string> _userDefinedComplexReturnTypeList;
  13. private string _serviceName;
  14. public ServiceMap(Type type)
  15. {
  16. _usedComplexModelTypeList = new List<ComplexModelType>();
  17. TemporaryList = new List<ComplexModelType>();
  18. MethodMapList = new List<MethodMap>();
  19. var methodsList = type.GetMethods();
  20. _serviceName = type.Name[1..];
  21. foreach (var method in methodsList)
  22. {
  23. var methodMap = new MethodMap(method);
  24. MethodMapList.Add(methodMap);
  25. }
  26. _usedComplexModelTypeList.AddRange(TemporaryList);
  27. TemporaryList.Clear();
  28. }
  29. public string GetServiceModelDartString()
  30. {
  31. var dartString = new StringBuilder();
  32. var importServiceList = new List<string>();
  33. foreach (var modelType in _usedComplexModelTypeList)
  34. {
  35. if (!CodeGenerator.AlreadyGeneratedList.Any(x => x.Key.ParameterType.Name == modelType.ParameterType.Name && x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
  36. {
  37. CodeGenerator.AlreadyGeneratedList.Add(modelType, _serviceName);
  38. dartString.AppendLine(modelType.GetDartString());
  39. }
  40. else
  41. {
  42. var importService = CodeGenerator.AlreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == modelType.ParameterType.Name && x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
  43. if (importService == null)
  44. {
  45. throw new Exception("Import Service is null");
  46. }
  47. if (importService != _serviceName && !importServiceList.Contains(importService))
  48. {
  49. importServiceList.Add(importService);
  50. }
  51. }
  52. }
  53. if (string.IsNullOrWhiteSpace(dartString.ToString()))
  54. {
  55. return null;
  56. }
  57. var serviceModelDartString = new StringBuilder();
  58. foreach (var importService in importServiceList)
  59. {
  60. serviceModelDartString.AppendLine($"import '{LetterConverterHelper.FirstCharToLower(importService[0..^7])}.m.dart';");
  61. }
  62. if (importServiceList.Count > 0)
  63. {
  64. serviceModelDartString.AppendLine();
  65. }
  66. var dartStringInfo = dartString.ToString();
  67. if (dartStringInfo.Contains("JsonRpcUtils"))
  68. {
  69. serviceModelDartString.AppendLine(CodeGenerator.StringUtils);
  70. serviceModelDartString.AppendLine();
  71. }
  72. if (dartStringInfo.Contains("FJsonConvert"))
  73. {
  74. serviceModelDartString.AppendLine(CodeGenerator.StringJsonConvert);
  75. serviceModelDartString.AppendLine();
  76. }
  77. serviceModelDartString.AppendLine(dartStringInfo);
  78. return serviceModelDartString.ToString();
  79. }
  80. public string GetServiceDartString()
  81. {
  82. var serviceDartString = new StringBuilder();
  83. var importServiceList = new List<string>();
  84. foreach (var methodMap in MethodMapList)
  85. {
  86. string importService = null;
  87. foreach (var modelType in methodMap.ParameterModeTypes)
  88. {
  89. if (modelType is ComplexModelType)
  90. {
  91. importService = CodeGenerator.AlreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == modelType.ParameterType.Name && x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
  92. if (importService == null)
  93. {
  94. throw new Exception("Import Service is null");
  95. }
  96. if (importService != _serviceName && !importServiceList.Contains(importService))
  97. {
  98. importServiceList.Add(importService);
  99. }
  100. }
  101. }
  102. var returnModelType = methodMap.ReturnParameterModelType;
  103. if (returnModelType is ComplexModelType)
  104. {
  105. importService = CodeGenerator.AlreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == returnModelType.ParameterType.Name && x.Key.ParameterType.Namespace == returnModelType.ParameterType.Namespace).Value;
  106. if (importService == null)
  107. {
  108. throw new Exception("Import Service is null");
  109. }
  110. if (importService != _serviceName && !importServiceList.Contains(importService))
  111. {
  112. importServiceList.Add(importService);
  113. }
  114. }
  115. else if (returnModelType is ListModelType listModelType)
  116. {
  117. var subReturnType = listModelType.GenericArgumentModelType;
  118. if (subReturnType is ComplexModelType)
  119. {
  120. importService = CodeGenerator.AlreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == subReturnType.ParameterType.Name && x.Key.ParameterType.Namespace == subReturnType.ParameterType.Namespace).Value;
  121. if (importService == null)
  122. {
  123. throw new Exception("Import Service is null");
  124. }
  125. if (importService != _serviceName && !importServiceList.Contains(importService))
  126. {
  127. importServiceList.Add(importService);
  128. }
  129. }
  130. }
  131. else if (returnModelType is ArrayModelType arrayModelType)
  132. {
  133. var subReturnType = arrayModelType.Child;
  134. if (subReturnType is ComplexModelType)
  135. {
  136. importService = CodeGenerator.AlreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == subReturnType.ParameterType.Name && x.Key.ParameterType.Namespace == subReturnType.ParameterType.Namespace).Value;
  137. if (importService == null)
  138. {
  139. throw new Exception("Import Service is null");
  140. }
  141. if (importService != _serviceName && !importServiceList.Contains(importService))
  142. {
  143. importServiceList.Add(importService);
  144. }
  145. }
  146. }
  147. else if (returnModelType is DictionaryModelType dictionaryModelType)
  148. {
  149. foreach (var subReturnType in dictionaryModelType.GenericArgumentModelTypeList)
  150. {
  151. if (subReturnType is ComplexModelType)
  152. {
  153. importService = CodeGenerator.AlreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == subReturnType.ParameterType.Name && x.Key.ParameterType.Namespace == subReturnType.ParameterType.Namespace).Value;
  154. if (importService == null)
  155. {
  156. throw new Exception("Import Service is null");
  157. }
  158. if (importService != _serviceName && !importServiceList.Contains(importService))
  159. {
  160. importServiceList.Add(importService);
  161. }
  162. }
  163. }
  164. }
  165. }
  166. var complexReturnTypeNameList = GetUserDefinedComplexReturnTypeList();
  167. foreach (var complexReturnTypeName in complexReturnTypeNameList)
  168. {
  169. string importService;
  170. if (complexReturnTypeName.Contains("<"))
  171. {
  172. var returnTypeName = complexReturnTypeName.Substring(0, complexReturnTypeName.IndexOf("<"));
  173. importService = CodeGenerator.AlreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name[0..^2] == returnTypeName).Value;
  174. }
  175. else
  176. {
  177. importService = CodeGenerator.AlreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == complexReturnTypeName).Value;
  178. }
  179. if (importService == null)
  180. {
  181. throw new Exception("Import Service is null");
  182. }
  183. if (importService != _serviceName && !importServiceList.Contains(importService))
  184. {
  185. importServiceList.Add(importService);
  186. }
  187. }
  188. foreach (var importService in importServiceList)
  189. {
  190. serviceDartString.AppendLine($"import '{LetterConverterHelper.FirstCharToLower(importService[0..^7])}.m.dart';");
  191. }
  192. if (importServiceList.Count > 0)
  193. {
  194. serviceDartString.AppendLine();
  195. }
  196. serviceDartString.AppendLine();
  197. serviceDartString.AppendLine($"class {_serviceName} extends JsonRpcClientBase {{");
  198. serviceDartString.AppendLine($"\t{_serviceName}(");
  199. serviceDartString.AppendLine("\t\tString host, {");
  200. serviceDartString.AppendLine($"\t\tString serviceName = \"I{_serviceName}\",");
  201. serviceDartString.AppendLine("\t\tMap<String, String>? headers,");
  202. serviceDartString.AppendLine("\t\tint? timeout,");
  203. serviceDartString.AppendLine("\t}) : super(");
  204. serviceDartString.AppendLine("\t\t\t\t\t\thost,");
  205. serviceDartString.AppendLine("\t\t\t\t\t\tserviceName,");
  206. serviceDartString.AppendLine("\t\t\t\t\t\theaders: headers,");
  207. serviceDartString.AppendLine("\t\t\t\t\t\ttimeout: timeout,");
  208. if (complexReturnTypeNameList.Count() == 0)
  209. {
  210. serviceDartString.AppendLine("\t\t\t\t);");
  211. }
  212. else
  213. {
  214. serviceDartString.AppendLine("\t\t\t\t) {");
  215. serviceDartString.AppendLine("\t\t/// 注册响应实体反序列化处理器");
  216. foreach (var complexRetrunTypeName in complexReturnTypeNameList)
  217. {
  218. serviceDartString.AppendLine($"\t\tFJsonConvert.setDecoder((map) => {complexRetrunTypeName}.fromJson(map));");
  219. }
  220. serviceDartString.AppendLine("\t}");
  221. }
  222. serviceDartString.AppendLine();
  223. foreach (var methodMap in MethodMapList)
  224. {
  225. serviceDartString.AppendLine(methodMap.GetMethodDartString());
  226. }
  227. serviceDartString.AppendLine("}");
  228. return serviceDartString.ToString();
  229. }
  230. public List<string> GetUserDefinedComplexReturnTypeList()
  231. {
  232. _userDefinedComplexReturnTypeList = new List<string>();
  233. foreach (var method in MethodMapList)
  234. {
  235. var veturnParameterModelType = method.ReturnParameterModelType;
  236. AddUserDefinedComplexReturnType(veturnParameterModelType);
  237. }
  238. return _userDefinedComplexReturnTypeList;
  239. }
  240. private void AddUserDefinedComplexReturnType(ModelType modelType)
  241. {
  242. if (modelType is UserDefinedModeType)
  243. {
  244. if (!_userDefinedComplexReturnTypeList.Contains(modelType.GetFlutterTypeName()))
  245. {
  246. _userDefinedComplexReturnTypeList.Add(modelType.GetFlutterTypeName());
  247. }
  248. }
  249. else if (modelType is UserDefinedDerivedModelType)
  250. {
  251. if (!_userDefinedComplexReturnTypeList.Contains(modelType.GetFlutterTypeName()))
  252. {
  253. _userDefinedComplexReturnTypeList.Add(modelType.GetFlutterTypeName());
  254. }
  255. }
  256. else if (modelType is UserDefinedGenericModelType userDefinedGenericComplexModelType)
  257. {
  258. if (!_userDefinedComplexReturnTypeList.Contains(userDefinedGenericComplexModelType.GetFlutterTypeName()))
  259. {
  260. _userDefinedComplexReturnTypeList.Add(userDefinedGenericComplexModelType.GetFlutterTypeName());
  261. }
  262. var argumentModelType = userDefinedGenericComplexModelType.GenericArgumentModelType;
  263. AddUserDefinedComplexReturnType(argumentModelType);
  264. }
  265. else if (modelType is ListModelType listModelType)
  266. {
  267. var argumentModelType = listModelType.GenericArgumentModelType;
  268. AddUserDefinedComplexReturnType(argumentModelType);
  269. }
  270. else if (modelType is DictionaryModelType dictionaryModelType)
  271. {
  272. foreach (var argumentModelType in dictionaryModelType.GenericArgumentModelTypeList)
  273. {
  274. AddUserDefinedComplexReturnType(argumentModelType);
  275. }
  276. }
  277. }
  278. }
  279. }