UserDefinedDerivedModelType.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using FlutterCodeGenerator.Helper;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. namespace FlutterCodeGenerator.ModelTypes
  7. {
  8. internal class UserDefinedDerivedModelType : ComplexModelType
  9. {
  10. public ModelType BaseType;
  11. public UserDefinedDerivedModelType(Type type, string name) : base(type, name)
  12. {
  13. BaseType = ModelTypeGenerator.Create(type.BaseType, "");
  14. if (!(BaseType is UserDefinedGenericModelType))
  15. {
  16. var properties = type.GetProperties();
  17. foreach (var property in properties)
  18. {
  19. var child = ModelTypeGenerator.Create(property.PropertyType, property.Name);
  20. Children.Add(child);
  21. }
  22. }
  23. }
  24. public override string GetFlutterTypeName(bool isDefault = true, bool isGenericName = false, bool isSingle = false)
  25. {
  26. if (Index > 1)
  27. {
  28. return $"{ParameterType.Name}{Index}";
  29. }
  30. else
  31. {
  32. return $"{ParameterType.Name}";
  33. }
  34. }
  35. public override string GetDartString()
  36. {
  37. var dartString = new StringBuilder();
  38. dartString.AppendLine($"class {GetFlutterTypeName()} extends {BaseType.GetFlutterTypeName()}{{");
  39. var baseType = BaseType as ComplexModelType;
  40. foreach (var child in Children)
  41. {
  42. if (baseType != null)
  43. {
  44. if (!baseType.Children.Any(x => x.ParameterType == child.ParameterType && x.Name_Upper == child.Name_Upper))
  45. {
  46. var questionMark = QuestionMarkCheck(child);
  47. if (child is ListModelType || child is DictionaryModelType || child is UserDefinedGenericModelType)
  48. {
  49. dartString.AppendLine($"\t{child.GetFlutterTypeName(false, false)}{questionMark} {child.Name_Lower};");
  50. }
  51. else
  52. {
  53. dartString.AppendLine($"\t{child.GetFlutterTypeName()}{questionMark} {child.Name_Lower};");
  54. }
  55. }
  56. else
  57. {
  58. continue;
  59. }
  60. }
  61. else
  62. {
  63. var questionMark = QuestionMarkCheck(child);
  64. if (child is ListModelType || child is DictionaryModelType || child is UserDefinedGenericModelType)
  65. {
  66. dartString.AppendLine($"\t{child.GetFlutterTypeName(false, false)}{questionMark} {child.Name_Lower};");
  67. }
  68. else
  69. {
  70. dartString.AppendLine($"\t{child.GetFlutterTypeName()}{questionMark} {child.Name_Lower};");
  71. }
  72. }
  73. }
  74. dartString.AppendLine();
  75. if (Children.Count == 0)
  76. {
  77. dartString.AppendLine($"\t{GetFlutterTypeName()}(");
  78. if (baseType != null && !(baseType is UserDefinedGenericModelType))
  79. {
  80. dartString.AppendLine($"\t) : super(");
  81. foreach (var child in baseType.Children)
  82. {
  83. dartString.AppendLine($"\t\t\t{child.Name_Lower}: {child.Name_Lower},");
  84. }
  85. dartString.AppendLine($"\t\t);");
  86. }
  87. else
  88. {
  89. dartString.AppendLine("\t);");
  90. }
  91. }
  92. else
  93. {
  94. dartString.AppendLine($"\t{GetFlutterTypeName()}({{");
  95. foreach (var child in Children)
  96. {
  97. if (baseType != null)
  98. {
  99. if (baseType.Children.Any(x => x.ParameterType == child.ParameterType && x.Name_Upper == child.Name_Upper))
  100. {
  101. var questionMark = QuestionMarkCheck(child);
  102. if (child.DefaultValue != null)
  103. {
  104. if (child is ListModelType || child is DictionaryModelType || child is UserDefinedGenericModelType)
  105. {
  106. dartString.AppendLine($"\t\t{child.GetFlutterTypeName(false, false)}{questionMark} {child.Name_Lower} = {child.DefaultValue},");
  107. }
  108. else
  109. {
  110. dartString.AppendLine($"\t\t{child.GetFlutterTypeName()}{questionMark} {child.Name_Lower} = {child.DefaultValue},");
  111. }
  112. }
  113. else
  114. {
  115. if (child is ListModelType || child is DictionaryModelType || child is UserDefinedGenericModelType)
  116. {
  117. dartString.AppendLine($"\t\t{child.GetFlutterTypeName(false, false)}{questionMark} {child.Name_Lower},");
  118. }
  119. else
  120. {
  121. dartString.AppendLine($"\t\t{child.GetFlutterTypeName()}{questionMark} {child.Name_Lower},");
  122. }
  123. }
  124. }
  125. else
  126. {
  127. if (child.DefaultValue != null)
  128. {
  129. dartString.AppendLine($"\t\tthis.{child.Name_Lower} = {child.DefaultValue},");
  130. }
  131. else
  132. {
  133. dartString.AppendLine($"\t\tthis.{child.Name_Lower},");
  134. }
  135. }
  136. }
  137. else
  138. {
  139. if (child.DefaultValue != null)
  140. {
  141. dartString.AppendLine($"\t\tthis.{child.Name_Lower} = {child.DefaultValue},");
  142. }
  143. else
  144. {
  145. dartString.AppendLine($"\t\tthis.{child.Name_Lower},");
  146. }
  147. }
  148. }
  149. if (baseType != null)
  150. {
  151. dartString.AppendLine($"\t}}) : super(");
  152. foreach (var child in baseType.Children)
  153. {
  154. dartString.AppendLine($"\t\t\t{child.Name_Lower}: {child.Name_Lower},");
  155. }
  156. dartString.AppendLine($"\t\t);");
  157. }
  158. else
  159. {
  160. dartString.AppendLine("\t});");
  161. }
  162. }
  163. dartString.AppendLine();
  164. dartString.Append(FromJson(Children));
  165. dartString.Append(ToJson(Children));
  166. dartString.AppendLine("}");
  167. return dartString.ToString();
  168. }
  169. protected override string ToJson(List<ModelType> Children)
  170. {
  171. var source = new StringBuilder();
  172. source.AppendLine("\tMap<String, dynamic> toJson() {");
  173. source.AppendLine("\t\tfinal map = super.toJson();");
  174. foreach (var child in Children)
  175. {
  176. if (BaseType is ComplexModelType complexModelType)
  177. {
  178. if (!complexModelType.Children.Any(x => x.ParameterType == child.ParameterType && x.Name_Upper == child.Name_Upper))
  179. {
  180. if (child is EnumModelType)
  181. {
  182. source.AppendLine($"\t\tmap['{child.Name_Upper}'] = {child.Name_Lower}.index;");
  183. }
  184. else if (child is EnumNullableModelType)
  185. {
  186. source.AppendLine($"\t\tif ({child.Name_Lower} != null)");
  187. source.AppendLine($"\t\tmap['{child.Name_Upper}'] = {child.Name_Lower}?.index;");
  188. }
  189. else if (child.DefaultValue != null)
  190. {
  191. source.AppendLine($"\t\tmap['{child.Name_Upper}'] = {child.Name_Lower};");
  192. }
  193. else if (child is DateTimeModelType)
  194. {
  195. source.AppendLine($"\t\tif ({child.Name_Lower} != null)");
  196. source.AppendLine($"\t\t\tmap['{child.Name_Upper}'] = JsonRpcUtils.dateFormat({child.Name_Lower}!);");
  197. }
  198. else if (child is ListModelType listModelType)
  199. {
  200. if (listModelType.GenericArgumentModelType is EnumModelType)
  201. {
  202. source.AppendLine($"\t\tif ({child.Name_Lower} != null) {{");
  203. source.AppendLine($"\t\t\tList<int> {child.Name_Lower}List = [];");
  204. source.AppendLine($"\t\t\t{child.Name_Lower}!.forEach((e) => {child.Name_Lower}List.add(e.index));");
  205. source.AppendLine($"\t\t\tmap['{child.Name_Upper}'] = {child.Name_Lower}List;");
  206. source.AppendLine($"\t\t}}");
  207. }
  208. else
  209. {
  210. source.AppendLine($"\t\tif ({child.Name_Lower} != null)");
  211. source.AppendLine($"\t\t\tmap['{child.Name_Upper}'] = {child.Name_Lower};");
  212. }
  213. }
  214. else
  215. {
  216. source.AppendLine($"\t\tif ({child.Name_Lower} != null)");
  217. source.AppendLine($"\t\t\tmap['{child.Name_Upper}'] = {child.Name_Lower};");
  218. }
  219. }
  220. }
  221. }
  222. source.AppendLine("\t\treturn map;");
  223. source.AppendLine("\t}");
  224. return source.ToString();
  225. }
  226. }
  227. }