Skip to content

Commit

Permalink
Addng updated event info with inner fields
Browse files Browse the repository at this point in the history
  • Loading branch information
farajfarook committed Mar 8, 2023
1 parent 159b8d3 commit 03a0f72
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cd.yaml
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion 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;

Expand Down
20 changes: 16 additions & 4 deletions 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;

Expand All @@ -12,18 +15,27 @@ public interface IEventInfoService
public class EventInfoService: IEventInfoService
{
private EventInfoListResponse _response;

private readonly EventInfoOption _option;

public EventInfoService(IOptions<EventInfoOption> option)
{
_option = option.Value;
}

public EventInfoListResponse List()
{
if (_response != null) return _response;

var type = typeof(IEvent);

var parentTypes = _option.SearchTypes ?? new List<Type>();
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
Expand Down
12 changes: 0 additions & 12 deletions Enbiso.NLib.EventInfo/EventInfoServiceExtensions.cs

This file was deleted.

25 changes: 0 additions & 25 deletions Enbiso.NLib.EventInfo/EventRecord.cs

This file was deleted.

9 changes: 9 additions & 0 deletions Enbiso.NLib.EventInfo/Models/EventInfoListResponse.cs
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace Enbiso.NLib.EventInfo.Models;

public class EventInfoListResponse
{
public List<EventRecord> Records { get; set; } = new();
public int Count { get; set; }
}
17 changes: 17 additions & 0 deletions 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<EventRecordProp> Props { get; }

public EventRecord(Type type)
{
Name = type.Name;
Props = type.GetProperties().Select(EventRecordMapper.Map).ToList();
}
}
50 changes: 50 additions & 0 deletions 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<Type>
{
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]));
}
}
17 changes: 17 additions & 0 deletions 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<EventRecordProp> Props { get; set; }
}
23 changes: 23 additions & 0 deletions 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<EventInfoOption> optBuilder = null)
{
collection.AddOptions();
optBuilder ??= _ => {};
collection.Configure(optBuilder);

collection.AddSingleton<IEventInfoService, EventInfoService>();
return collection;
}
}

public class EventInfoOption
{
public List<Type> SearchTypes { get; set; } = new();
}
2 changes: 1 addition & 1 deletion Enbiso.NLib.OpenApi/ServiceExtensions.cs
Expand Up @@ -21,7 +21,7 @@ public static void AddOpenApi(this IServiceCollection services, Action<OpenApiOp
{
services.AddOptions();

optBuilder ??= options => {};
optBuilder ??= _ => {};
services.Configure(optBuilder);

var opts = new OpenApiOptions();
Expand Down

0 comments on commit 03a0f72

Please sign in to comment.