diff --git a/.github/workflows/cd.yaml b/.github/workflows/cd.yaml index a51a9b9..d9e90c8 100644 --- a/.github/workflows/cd.yaml +++ b/.github/workflows/cd.yaml @@ -6,7 +6,7 @@ on: - main env: - PACKAGE_VERSION: 6.3.1 + PACKAGE_VERSION: 6.3.2 PACKAGE_PROJECT: https://nlib.enbiso.com PACKAGE_REPO: https://github.com/enbiso/Enbiso.NLib PACKAGE_COPYRIGHT: Copyright 2021 (c) enbiso. All rights reserved. diff --git a/Enbiso.NLib.EventInfo/EventInfoController.cs b/Enbiso.NLib.EventInfo/EventInfoController.cs index cc4849c..86d73f5 100644 --- a/Enbiso.NLib.EventInfo/EventInfoController.cs +++ b/Enbiso.NLib.EventInfo/EventInfoController.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Mvc; +using Enbiso.NLib.EventInfo.Models; +using Microsoft.AspNetCore.Mvc; namespace Enbiso.NLib.EventInfo; diff --git a/Enbiso.NLib.EventInfo/EventInfoService.cs b/Enbiso.NLib.EventInfo/EventInfoService.cs index 76cdf87..0630c46 100644 --- a/Enbiso.NLib.EventInfo/EventInfoService.cs +++ b/Enbiso.NLib.EventInfo/EventInfoService.cs @@ -1,6 +1,9 @@ using System; +using System.Collections.Generic; using System.Linq; using Enbiso.NLib.EventBus; +using Enbiso.NLib.EventInfo.Models; +using Microsoft.Extensions.Options; namespace Enbiso.NLib.EventInfo; @@ -12,18 +15,27 @@ public interface IEventInfoService public class EventInfoService: IEventInfoService { private EventInfoListResponse _response; - + private readonly EventInfoOption _option; + + public EventInfoService(IOptions option) + { + _option = option.Value; + } + public EventInfoListResponse List() { if (_response != null) return _response; - - var type = typeof(IEvent); + + var parentTypes = _option.SearchTypes ?? new List(); + parentTypes.Add(typeof(IEvent)); + var assemblies = AppDomain.CurrentDomain.GetAssemblies() .Where(p => !(p.FullName?.Contains("NLib") ?? false)); var types = assemblies .SelectMany(s => s.GetTypes()) - .Where(p => type.IsAssignableFrom(p)); + .Where(t => t.IsClass) + .Where(t => parentTypes.Any(pt => pt.IsAssignableFrom(t))); var records = types.Select(t => new EventRecord(t)).ToList(); _response = new EventInfoListResponse diff --git a/Enbiso.NLib.EventInfo/EventInfoServiceExtensions.cs b/Enbiso.NLib.EventInfo/EventInfoServiceExtensions.cs deleted file mode 100644 index e3bf741..0000000 --- a/Enbiso.NLib.EventInfo/EventInfoServiceExtensions.cs +++ /dev/null @@ -1,12 +0,0 @@ -using Microsoft.Extensions.DependencyInjection; - -namespace Enbiso.NLib.EventInfo; - -public static class EventInfoServiceExtensions -{ - public static IServiceCollection AddEventInfo(this IServiceCollection collection) - { - collection.AddSingleton(); - return collection; - } -} \ No newline at end of file diff --git a/Enbiso.NLib.EventInfo/EventRecord.cs b/Enbiso.NLib.EventInfo/EventRecord.cs deleted file mode 100644 index 5f85192..0000000 --- a/Enbiso.NLib.EventInfo/EventRecord.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; - -namespace Enbiso.NLib.EventInfo; - -public class EventInfoListResponse -{ - public List Records { get; set; } = new(); - public int Count { get; set; } -} - -public class EventRecord -{ - public EventRecord(Type type) - { - Name = type.Name; - Props = type.GetProperties().Select(p => new EventRecordProp(p.Name, p.PropertyType.Name)).ToList(); - } - - public string Name { get; set; } - public List Props { get; set; } -} - -public record EventRecordProp(string Name, string Type); \ No newline at end of file diff --git a/Enbiso.NLib.EventInfo/Models/EventInfoListResponse.cs b/Enbiso.NLib.EventInfo/Models/EventInfoListResponse.cs new file mode 100644 index 0000000..0cdf33f --- /dev/null +++ b/Enbiso.NLib.EventInfo/Models/EventInfoListResponse.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace Enbiso.NLib.EventInfo.Models; + +public class EventInfoListResponse +{ + public List Records { get; set; } = new(); + public int Count { get; set; } +} \ No newline at end of file diff --git a/Enbiso.NLib.EventInfo/Models/EventRecord.cs b/Enbiso.NLib.EventInfo/Models/EventRecord.cs new file mode 100644 index 0000000..28f5dbe --- /dev/null +++ b/Enbiso.NLib.EventInfo/Models/EventRecord.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Enbiso.NLib.EventInfo.Models; + +public class EventRecord +{ + public string Name { get; } + public List Props { get; } + + public EventRecord(Type type) + { + Name = type.Name; + Props = type.GetProperties().Select(EventRecordMapper.Map).ToList(); + } +} \ No newline at end of file diff --git a/Enbiso.NLib.EventInfo/Models/EventRecordMapper.cs b/Enbiso.NLib.EventInfo/Models/EventRecordMapper.cs new file mode 100644 index 0000000..d9d70b1 --- /dev/null +++ b/Enbiso.NLib.EventInfo/Models/EventRecordMapper.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; + +namespace Enbiso.NLib.EventInfo.Models; + +public static class EventRecordMapper +{ + public static EventRecordProp Map(PropertyInfo property) => + new(property) + { + Props = property.PropertyType.GetProperties() + .Where(p => ShouldExtract(p.PropertyType)) + .Select(p => new EventRecordProp(p)).ToList() + }; + + private static bool ShouldExtract(Type type) + { + if (IsSimpleType(type)) return false; + var excludeTypes = new List + { + typeof(List<>), + typeof(IEnumerable<>), + typeof(Dictionary<,>), + typeof(IDictionary<,>) + }; + return excludeTypes.All(p => p == type); + } + + private static bool IsSimpleType(Type type) + { + return + type.IsPrimitive || + type.IsValueType || + new[] + { + typeof(string), + typeof(decimal), + typeof(DateTime), + typeof(DateTimeOffset), + typeof(TimeSpan), + typeof(Guid) + }.Contains(type) || + type.IsEnum || + Convert.GetTypeCode(type) != TypeCode.Object || + (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>) && + IsSimpleType(type.GetGenericArguments()[0])); + } +} \ No newline at end of file diff --git a/Enbiso.NLib.EventInfo/Models/EventRecordProp.cs b/Enbiso.NLib.EventInfo/Models/EventRecordProp.cs new file mode 100644 index 0000000..2017a90 --- /dev/null +++ b/Enbiso.NLib.EventInfo/Models/EventRecordProp.cs @@ -0,0 +1,17 @@ +using System.Collections.Generic; +using System.Reflection; + +namespace Enbiso.NLib.EventInfo.Models; + +public class EventRecordProp +{ + public EventRecordProp(PropertyInfo property) + { + Name = property.Name; + Type = property.PropertyType.Name; + } + + public string Name { get; } + public string Type { get; } + public List Props { get; set; } +} \ No newline at end of file diff --git a/Enbiso.NLib.EventInfo/ServiceExtensions.cs b/Enbiso.NLib.EventInfo/ServiceExtensions.cs new file mode 100644 index 0000000..7332dfe --- /dev/null +++ b/Enbiso.NLib.EventInfo/ServiceExtensions.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using Microsoft.Extensions.DependencyInjection; + +namespace Enbiso.NLib.EventInfo; + +public static class ServiceExtensions +{ + public static IServiceCollection AddEventInfo(this IServiceCollection collection, Action optBuilder = null) + { + collection.AddOptions(); + optBuilder ??= _ => {}; + collection.Configure(optBuilder); + + collection.AddSingleton(); + return collection; + } +} + +public class EventInfoOption +{ + public List SearchTypes { get; set; } = new(); +} \ No newline at end of file diff --git a/Enbiso.NLib.OpenApi/ServiceExtensions.cs b/Enbiso.NLib.OpenApi/ServiceExtensions.cs index 332798d..ec2d1b9 100644 --- a/Enbiso.NLib.OpenApi/ServiceExtensions.cs +++ b/Enbiso.NLib.OpenApi/ServiceExtensions.cs @@ -21,7 +21,7 @@ public static void AddOpenApi(this IServiceCollection services, Action {}; + optBuilder ??= _ => {}; services.Configure(optBuilder); var opts = new OpenApiOptions();