UserDefinedModelType.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using FlutterCodeGenerator.Helper;
  2. using System;
  3. using System.Text;
  4. namespace FlutterCodeGenerator.ModelTypes
  5. {
  6. internal class UserDefinedModelType : ComplexModelType
  7. {
  8. public UserDefinedModelType(Type type, string name) : base(type, name)
  9. {
  10. var properties = type.GetProperties();
  11. foreach (var property in properties)
  12. {
  13. var child = ModelTypeGenerator.Create(property.PropertyType, property.Name);
  14. Children.Add(child);
  15. }
  16. }
  17. public override string GetFlutterTypeName(bool isDefault = true, bool isGenericName = false, bool isSingle = false)
  18. {
  19. if (Index > 1)
  20. {
  21. return $"{ParameterType.Name}{Index}";
  22. }
  23. else
  24. {
  25. return $"{ParameterType.Name}";
  26. }
  27. }
  28. public override string GetDartString()
  29. {
  30. var dartString = new StringBuilder();
  31. dartString.AppendLine($"class {GetFlutterTypeName()} {{");
  32. foreach (var child in Children)
  33. {
  34. var questionMark = QuestionMarkCheck(child);
  35. if (child is ListModelType || child is DictionaryModelType || child is UserDefinedGenericModelType)
  36. {
  37. dartString.AppendLine($"\t{child.GetFlutterTypeName(false, false)}{questionMark} {child.Name_Lower};");
  38. }
  39. else
  40. {
  41. dartString.AppendLine($"\t{child.GetFlutterTypeName()}{questionMark} {child.Name_Lower};");
  42. }
  43. }
  44. dartString.AppendLine();
  45. if (Children.Count == 0)
  46. {
  47. dartString.AppendLine($"\t{GetFlutterTypeName()}();");
  48. }
  49. else
  50. {
  51. dartString.AppendLine($"\t{GetFlutterTypeName()}({{");
  52. foreach (var child in Children)
  53. {
  54. if (child.DefaultValue != null)
  55. {
  56. dartString.AppendLine($"\t\tthis.{child.Name_Lower} = {child.DefaultValue},");
  57. }
  58. else
  59. {
  60. dartString.AppendLine($"\t\tthis.{child.Name_Lower},");
  61. }
  62. }
  63. dartString.AppendLine("\t});");
  64. }
  65. dartString.AppendLine();
  66. dartString.Append(FromJson(Children));
  67. dartString.Append(ToJson(Children));
  68. dartString.AppendLine("}");
  69. return dartString.ToString();
  70. }
  71. }
  72. }