ServiceMap.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. }
  103. var complexReturnTypeNameList = GetUserDefinedComplexReturnTypeList();
  104. foreach (var complexReturnTypeName in complexReturnTypeNameList)
  105. {
  106. string importService;
  107. if (complexReturnTypeName.Contains("<"))
  108. {
  109. var returnTypeName = complexReturnTypeName.Substring(0, complexReturnTypeName.IndexOf("<"));
  110. importService = CodeGenerator.AlreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name[0..^2] == returnTypeName).Value;
  111. }
  112. else
  113. {
  114. importService = CodeGenerator.AlreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == complexReturnTypeName).Value;
  115. }
  116. if (importService == null)
  117. {
  118. throw new Exception("Import Service is null");
  119. }
  120. if (importService != _serviceName && !importServiceList.Contains(importService))
  121. {
  122. importServiceList.Add(importService);
  123. }
  124. }
  125. foreach (var importService in importServiceList)
  126. {
  127. serviceDartString.AppendLine($"import '{LetterConverterHelper.FirstCharToLower(importService[0..^7]) }.m.dart';");
  128. }
  129. if (importServiceList.Count > 0)
  130. {
  131. serviceDartString.AppendLine();
  132. }
  133. serviceDartString.AppendLine();
  134. serviceDartString.AppendLine($"class {_serviceName} extends JsonRpcClientBase {{");
  135. serviceDartString.AppendLine($"\t{_serviceName}(");
  136. serviceDartString.AppendLine("\t\tString host, {");
  137. serviceDartString.AppendLine($"\t\tString serviceName = \"I{_serviceName}\",");
  138. serviceDartString.AppendLine("\t\tMap<String, String>? headers,");
  139. serviceDartString.AppendLine("\t\tint? timeout,");
  140. serviceDartString.AppendLine("\t}) : super(");
  141. serviceDartString.AppendLine("\t\t\t\t\t\thost,");
  142. serviceDartString.AppendLine("\t\t\t\t\t\tserviceName,");
  143. serviceDartString.AppendLine("\t\t\t\t\t\theaders: headers,");
  144. serviceDartString.AppendLine("\t\t\t\t\t\ttimeout: timeout,");
  145. if (complexReturnTypeNameList.Count() == 0)
  146. {
  147. serviceDartString.AppendLine("\t\t\t\t);");
  148. }
  149. else
  150. {
  151. serviceDartString.AppendLine("\t\t\t\t) {");
  152. serviceDartString.AppendLine("\t\t/// 注册响应实体反序列化处理器");
  153. foreach (var complexRetrunTypeName in complexReturnTypeNameList)
  154. {
  155. serviceDartString.AppendLine($"\t\tFJsonConvert.setDecoder((map) => {complexRetrunTypeName}.fromJson(map));");
  156. }
  157. serviceDartString.AppendLine("\t}");
  158. }
  159. serviceDartString.AppendLine();
  160. foreach (var methodMap in MethodMapList)
  161. {
  162. serviceDartString.AppendLine(methodMap.GetMethodDartString());
  163. }
  164. serviceDartString.AppendLine("}");
  165. return serviceDartString.ToString();
  166. }
  167. public List<string> GetUserDefinedComplexReturnTypeList()
  168. {
  169. _userDefinedComplexReturnTypeList = new List<string>();
  170. foreach (var method in MethodMapList)
  171. {
  172. var veturnParameterModelType = method.ReturnParameterModelType;
  173. AddUserDefinedComplexReturnType(veturnParameterModelType);
  174. }
  175. return _userDefinedComplexReturnTypeList;
  176. }
  177. private void AddUserDefinedComplexReturnType(ModelType modelType)
  178. {
  179. if (modelType is UserDefinedModeType)
  180. {
  181. if (!_userDefinedComplexReturnTypeList.Contains(modelType.GetFlutterTypeName()))
  182. {
  183. _userDefinedComplexReturnTypeList.Add(modelType.GetFlutterTypeName());
  184. }
  185. }
  186. else if (modelType is UserDefinedDerivedModelType)
  187. {
  188. if (!_userDefinedComplexReturnTypeList.Contains(modelType.GetFlutterTypeName()))
  189. {
  190. _userDefinedComplexReturnTypeList.Add(modelType.GetFlutterTypeName());
  191. }
  192. }
  193. else if (modelType is UserDefinedGenericModelType userDefinedGenericComplexModelType)
  194. {
  195. if (!_userDefinedComplexReturnTypeList.Contains(userDefinedGenericComplexModelType.GetFlutterTypeName()))
  196. {
  197. _userDefinedComplexReturnTypeList.Add(userDefinedGenericComplexModelType.GetFlutterTypeName());
  198. }
  199. var argumentModelType = userDefinedGenericComplexModelType.GenericArgumentModelType;
  200. AddUserDefinedComplexReturnType(argumentModelType);
  201. }
  202. else if (modelType is ListModelType listModelType)
  203. {
  204. var argumentModelType = listModelType.GenericArgumentModelType;
  205. AddUserDefinedComplexReturnType(argumentModelType);
  206. }
  207. else if (modelType is DictionaryModelType dictionaryModelType)
  208. {
  209. foreach (var argumentModelType in dictionaryModelType.GenericArgumentModelTypeList)
  210. {
  211. AddUserDefinedComplexReturnType(argumentModelType);
  212. }
  213. }
  214. }
  215. }
  216. }