12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace FlutterCodeGenerator
- {
- internal class OtherServiceMap : IServiceMap
- {
- private List<Type> _typeList;
- private string _serviceName;
- public List<ComplexModelType> UsedComplexModelTypeList { get; }
- public OtherServiceMap(List<Type> types)
- {
- _typeList = types;
- UsedComplexModelTypeList = new List<ComplexModelType>();
- GenerateDataCache.Instance.SetCurrentServiceMap(this);
- _serviceName = "OtherService";
- foreach (var type in _typeList)
- {
- try
- {
- var parameterModelType = ModelTypeGenerator.Create(type, type.Name, true);
- }
- catch (Exception ex)
- {
- Console.WriteLine($"Type is {type.FullName},Error:{ex}");
- throw;
- }
- }
- }
- public string GetServiceDartString()
- {
- return null;
- }
- public string GetServiceModelDartString()
- {
- var dartString = new StringBuilder();
- var importServiceList = new List<string>();
- var alreadyGeneratedList = GenerateDataCache.Instance.AlreadyGeneratedList;
- foreach (var modelType in UsedComplexModelTypeList)
- {
- if (!alreadyGeneratedList.Any(x => x.Key.ParameterType.Name == modelType.ParameterType.Name && x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
- {
- alreadyGeneratedList.Add(modelType, _serviceName);
- dartString.AppendLine(modelType.GetDartString());
- }
- else
- {
- var importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == modelType.ParameterType.Name && x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
- if (importService == null)
- {
- throw new Exception("Import Service is null");
- }
- if (importService != _serviceName && !importServiceList.Contains(importService))
- {
- importServiceList.Add(importService);
- }
- }
- }
- if (string.IsNullOrWhiteSpace(dartString.ToString()))
- {
- return null;
- }
- var serviceModelDartString = new StringBuilder();
- foreach (var importService in importServiceList)
- {
- serviceModelDartString.AppendLine($"import '{LetterConverterHelper.FirstCharToLower(importService[0..^7])}.m.dart';");
- }
- if (importServiceList.Count > 0)
- {
- serviceModelDartString.AppendLine();
- }
- var dartStringInfo = dartString.ToString();
- if (dartStringInfo.Contains("JsonRpcUtils"))
- {
- serviceModelDartString.AppendLine(CodeGenerator.StringUtils);
- serviceModelDartString.AppendLine();
- }
- if (dartStringInfo.Contains("FJsonConvert"))
- {
- serviceModelDartString.AppendLine(CodeGenerator.StringJsonConvert);
- serviceModelDartString.AppendLine();
- }
- serviceModelDartString.AppendLine(dartStringInfo);
- return serviceModelDartString.ToString();
- }
- }
- }
|