12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using Luticate2.Auth.Utils.Dbo;
- using Luticate2.Auth.Utils.Dbo.Fields;
- using Luticate2.Auth.Utils.Dbo.Result;
- using Luticate2.Auth.Utils.Interfaces;
-
- namespace Luticate2.Auth.Utils.Business.ObjectConverter
- {
- public class LuObjectConverterLists : ILuObjectConverter
- {
- protected IServiceProvider ServiceProvider { get; }
-
- public LuObjectConverterLists(IServiceProvider serviceProvider)
- {
- ServiceProvider = serviceProvider;
- }
-
- public LuResult<object> Convert(object srcObj, object dstObj, LuFieldDbo path, LuPartialFieldsDbo fields, ILuObjectConverterOptions options)
- {
- if (srcObj is IEnumerable enumerable)
- {
- foreach (var subSrcObj in enumerable)
- {
- object convertedObj = null;
- if (subSrcObj != null)
- {
- var typeFrom = subSrcObj.GetType();
- var typeTo = options.TypeConverter.ConvertType(typeFrom);
- // TODO Handle converter == null
- var converter = LuObjectConverterUtils.GetObjectConverter(ServiceProvider, typeFrom, typeTo);
- var subDstObj = options.Allocator.GetInstance(typeTo);
- var convertResult = converter.Convert(subSrcObj, subDstObj, path, fields, options);
- if (!convertResult)
- {
- return convertResult;
- }
-
- convertedObj = convertResult.Data;
- }
-
- var methodInfo = dstObj.GetType().GetMethod(nameof(ICollection<object>.Add));
- methodInfo.Invoke(dstObj, new []{convertedObj});
- }
-
- return LuResult<object>.Ok(dstObj);
- }
- else
- {
- return LuResult<object>.Error(LuStatus.InputError.ToInt(), $"Could not convert {nameof(srcObj)} to {nameof(IEnumerable)}");
- }
- }
- }
- }
|