12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- using FlutterCodeGenerator.Helper;
- using System;
- using System.Text;
- namespace FlutterCodeGenerator.ModelTypes
- {
- internal class UserDefinedModelType : ComplexModelType
- {
- public UserDefinedModelType(Type type, string name) : base(type, name)
- {
- 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()} {{");
- foreach (var child in Children)
- {
- 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()}();");
- }
- else
- {
- dartString.AppendLine($"\t{GetFlutterTypeName()}({{");
- foreach (var child in Children)
- {
- if (child.DefaultValue != null)
- {
- dartString.AppendLine($"\t\tthis.{child.Name_Lower} = {child.DefaultValue},");
- }
- else
- {
- dartString.AppendLine($"\t\tthis.{child.Name_Lower},");
- }
- }
- dartString.AppendLine("\t});");
- }
- dartString.AppendLine();
- dartString.Append(FromJson(Children));
- dartString.Append(ToJson(Children));
- dartString.AppendLine("}");
- return dartString.ToString();
- }
- }
- }
|