123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- using FlutterCodeGenerator.Helper;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace FlutterCodeGenerator.ModelTypes
- {
- internal class UserDefinedDerivedModelType : ComplexModelType
- {
- public ModelType BaseType;
- public UserDefinedDerivedModelType(Type type, string name) : base(type, name)
- {
- BaseType = ModelTypeGenerator.Create(type.BaseType, "");
- if (!(BaseType is UserDefinedGenericModelType))
- {
- var properties = type.GetProperties();
- foreach (var property in properties)
- {
- var child = ModelTypeGenerator.Create(property.PropertyType, property.Name);
- Children.Add(child);
- }
- }
- }
- public override string GetFlutterTypeName(bool isDefault = true, bool isGenericName = false, bool isSingle = false)
- {
- if (Index > 1)
- {
- return $"{ParameterType.Name}{Index}";
- }
- else
- {
- return $"{ParameterType.Name}";
- }
- }
- public override string GetDartString()
- {
- var dartString = new StringBuilder();
- dartString.AppendLine($"class {GetFlutterTypeName()} extends {BaseType.GetFlutterTypeName()}{{");
- var baseType = BaseType as ComplexModelType;
- foreach (var child in Children)
- {
- if (baseType != null)
- {
- if (!baseType.Children.Any(x => x.ParameterType == child.ParameterType && x.Name_Upper == child.Name_Upper))
- {
- var questionMark = QuestionMarkCheck(child);
- if (child is ListModelType || child is DictionaryModelType || child is UserDefinedGenericModelType)
- {
- dartString.AppendLine($"\t{child.GetFlutterTypeName(false, false)}{questionMark} {child.Name_Lower};");
- }
- else
- {
- dartString.AppendLine($"\t{child.GetFlutterTypeName()}{questionMark} {child.Name_Lower};");
- }
- }
- else
- {
- continue;
- }
- }
- else
- {
- var questionMark = QuestionMarkCheck(child);
- if (child is ListModelType || child is DictionaryModelType || child is UserDefinedGenericModelType)
- {
- dartString.AppendLine($"\t{child.GetFlutterTypeName(false, false)}{questionMark} {child.Name_Lower};");
- }
- else
- {
- dartString.AppendLine($"\t{child.GetFlutterTypeName()}{questionMark} {child.Name_Lower};");
- }
- }
- }
- dartString.AppendLine();
- if (Children.Count == 0)
- {
- dartString.AppendLine($"\t{GetFlutterTypeName()}(");
- if (baseType != null && !(baseType is UserDefinedGenericModelType))
- {
- dartString.AppendLine($"\t) : super(");
- foreach (var child in baseType.Children)
- {
- dartString.AppendLine($"\t\t\t{child.Name_Lower}: {child.Name_Lower},");
- }
- dartString.AppendLine($"\t\t);");
- }
- else
- {
- dartString.AppendLine("\t);");
- }
- }
- else
- {
- dartString.AppendLine($"\t{GetFlutterTypeName()}({{");
- foreach (var child in Children)
- {
- if (baseType != null)
- {
- if (baseType.Children.Any(x => x.ParameterType == child.ParameterType && x.Name_Upper == child.Name_Upper))
- {
- var questionMark = QuestionMarkCheck(child);
- if (child.DefaultValue != null)
- {
- if (child is ListModelType || child is DictionaryModelType || child is UserDefinedGenericModelType)
- {
- dartString.AppendLine($"\t\t{child.GetFlutterTypeName(false, false)}{questionMark} {child.Name_Lower} = {child.DefaultValue},");
- }
- else
- {
- dartString.AppendLine($"\t\t{child.GetFlutterTypeName()}{questionMark} {child.Name_Lower} = {child.DefaultValue},");
- }
- }
- else
- {
- if (child is ListModelType || child is DictionaryModelType || child is UserDefinedGenericModelType)
- {
- dartString.AppendLine($"\t\t{child.GetFlutterTypeName(false, false)}{questionMark} {child.Name_Lower},");
- }
- else
- {
- dartString.AppendLine($"\t\t{child.GetFlutterTypeName()}{questionMark} {child.Name_Lower},");
- }
- }
- }
- else
- {
- if (child.DefaultValue != null)
- {
- dartString.AppendLine($"\t\tthis.{child.Name_Lower} = {child.DefaultValue},");
- }
- else
- {
- dartString.AppendLine($"\t\tthis.{child.Name_Lower},");
- }
- }
- }
- else
- {
- if (child.DefaultValue != null)
- {
- dartString.AppendLine($"\t\tthis.{child.Name_Lower} = {child.DefaultValue},");
- }
- else
- {
- dartString.AppendLine($"\t\tthis.{child.Name_Lower},");
- }
- }
- }
- if (baseType != null)
- {
- dartString.AppendLine($"\t}}) : super(");
- foreach (var child in baseType.Children)
- {
- dartString.AppendLine($"\t\t\t{child.Name_Lower}: {child.Name_Lower},");
- }
- dartString.AppendLine($"\t\t);");
- }
- else
- {
- dartString.AppendLine("\t});");
- }
- }
- dartString.AppendLine();
- dartString.Append(FromJson(Children));
- dartString.Append(ToJson(Children));
- dartString.AppendLine("}");
- return dartString.ToString();
- }
- protected override string ToJson(List<ModelType> Children)
- {
- var source = new StringBuilder();
- source.AppendLine("\tMap<String, dynamic> toJson() {");
- source.AppendLine("\t\tfinal map = super.toJson();");
- foreach (var child in Children)
- {
- if (BaseType is ComplexModelType complexModelType)
- {
- if (!complexModelType.Children.Any(x => x.ParameterType == child.ParameterType && x.Name_Upper == child.Name_Upper))
- {
- if (child is EnumModelType)
- {
- source.AppendLine($"\t\tmap['{child.Name_Upper}'] = {child.Name_Lower}.index;");
- }
- else if (child is EnumNullableModelType)
- {
- source.AppendLine($"\t\tif ({child.Name_Lower} != null)");
- source.AppendLine($"\t\tmap['{child.Name_Upper}'] = {child.Name_Lower}?.index;");
- }
- else if (child.DefaultValue != null)
- {
- source.AppendLine($"\t\tmap['{child.Name_Upper}'] = {child.Name_Lower};");
- }
- else if (child is DateTimeModelType)
- {
- source.AppendLine($"\t\tif ({child.Name_Lower} != null)");
- source.AppendLine($"\t\t\tmap['{child.Name_Upper}'] = JsonRpcUtils.dateFormat({child.Name_Lower}!);");
- }
- else if (child is ListModelType listModelType)
- {
- if (listModelType.GenericArgumentModelType is EnumModelType)
- {
- source.AppendLine($"\t\tif ({child.Name_Lower} != null) {{");
- source.AppendLine($"\t\t\tList<int> {child.Name_Lower}List = [];");
- source.AppendLine($"\t\t\t{child.Name_Lower}!.forEach((e) => {child.Name_Lower}List.add(e.index));");
- source.AppendLine($"\t\t\tmap['{child.Name_Upper}'] = {child.Name_Lower}List;");
- source.AppendLine($"\t\t}}");
- }
- else
- {
- source.AppendLine($"\t\tif ({child.Name_Lower} != null)");
- source.AppendLine($"\t\t\tmap['{child.Name_Upper}'] = {child.Name_Lower};");
- }
- }
- else
- {
- source.AppendLine($"\t\tif ({child.Name_Lower} != null)");
- source.AppendLine($"\t\t\tmap['{child.Name_Upper}'] = {child.Name_Lower};");
- }
- }
- }
- }
- source.AppendLine("\t\treturn map;");
- source.AppendLine("\t}");
- return source.ToString();
- }
- }
- }
|