ServiceMap.cs 13 KB

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