Skip to content

Commit

Permalink
T #98 Proto Mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
HueByte committed Nov 1, 2022
1 parent af79a49 commit be4904d
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 87 deletions.
2 changes: 1 addition & 1 deletion src/Huppy/Huppy.App/Middlewares/CommandLogMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public async Task AfterAsync(ExtendedShardedInteractionContext context, ICommand
CommandLogModel log = new()
{
CommandName = commandInfo.ToString(),
UnixTime = Miscellaneous.DateTimeToUnixTimestamp(DateTime.UtcNow),
Date = Miscellaneous.DateTimeToUnixTimestamp(DateTime.UtcNow),
IsSuccess = result.IsSuccess,
UserId = context.User.Id,
ExecutionTimeMs = watch.ElapsedMilliseconds,
Expand Down

This file was deleted.

This file was deleted.

4 changes: 1 addition & 3 deletions src/HuppyService/HuppyService.Core/Models/CommandLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ public class CommandLog : DbModel<int>
public override int Id { get; set; }
public string? CommandName { get; set; }
public string? ErrorMessage { get; set; }

[MappableTo(typeof(ulong), "UnixTime")]
public DateTime? Date { get; set; }
public DateTime Date { get; set; }
public bool IsSuccess { get; set; }
public long ExecutionTimeMs { get; set; }
public ulong ChannelId { get; set; }
Expand Down
76 changes: 28 additions & 48 deletions src/HuppyService/HuppyService.Core/Utilities/ReflectionMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,68 +10,48 @@ namespace HuppyService.Core.Utilities
{
public static class ReflectionMapper
{
public static D? Map<T, D>(T input)
where T : class
where D : class, new()
public static T Map<T>(object input) where T : class, new()
{
//var type1 = typeof(T);
//var type2 = typeof(D);
T result = new();

// create instance of D
D result = new();
var tProps = result.GetType().GetProperties();
var inputProps = input.GetType().GetProperties();

// get props of D
var propsOfD = typeof(D).GetProperties();

// get props of T
var propsOfT = input.GetType().GetProperties();

// map props to each other via names
foreach (var tProp in propsOfT)
foreach (var prop in tProps)
{
MappableToAttribute? attributeInfo = tProp.GetCustomAttribute(typeof(MappableToAttribute)) as MappableToAttribute;
//string searchName = attributeInfo is not null ? attributeInfo.AlternativeName : tProp.Name;

// get instance of D prop
PropertyInfo propInfo;
if (attributeInfo != null)
propInfo = propsOfD.First(prop => prop.Name == tProp.Name || prop.Name == attributeInfo.AlternativeName);
else
propInfo = propsOfD.First(prop => prop.Name == tProp.Name);
var matchingProp = inputProps.FirstOrDefault(iprop => iprop.Name == prop.Name);
if (matchingProp is null)
continue;

var inputPropInstance = matchingProp.GetValue(input, null);

//PropertyInfo propInfo = propsOfD.First(prop => prop.Name == searchName);
var propInstance = propInfo.GetValue(result, null);
Console.WriteLine($"Setting {prop.PropertyType} from {inputPropInstance?.GetType()}");

propInfo.SetValue(result, GetFinalProperty(attributeInfo, propInstance));
prop.SetValue(result, GetMappedValue(prop.PropertyType, inputPropInstance));
}

return result;
}

public static object? GetFinalProperty(MappableToAttribute? attributeInfo, object? instance)
public static object? GetMappedValue(Type newValue, object inputValue)
{
// get Mappable attribute
//MappableToAttribute? attributeInfo = propInfo.GetCustomAttribute(typeof(MappableToAttribute)) as MappableToAttribute;
if (inputValue is null) return default;

// get instance of value
//var value = propInfo.GetValue(instance, null);

// perform custom mappings
if (attributeInfo is not null)
switch (inputValue)
{
if (instance is DateTime dateValue && attributeInfo.MappableTo == typeof(ulong))
{
return Miscellaneous.DateTimeToUnixTimeStamp(dateValue);
}
else if (instance is ulong ulongValue && attributeInfo.MappableTo == typeof(DateTime))
{
return Miscellaneous.UnixTimeStampToUtcDateTime(ulongValue);
}

return instance;
}

return instance;
case DateTime:
if (newValue == typeof(ulong))
return Miscellaneous.DateTimeToUnixTimeStamp((DateTime)inputValue);
break;
case ulong:
if (newValue == typeof(DateTime))
return Miscellaneous.UnixTimeStampToUtcDateTime((ulong)inputValue);
break;
default:
return inputValue;
};

return inputValue;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package database.models;
message CommandLogModel {
int32 Id = 1;
string CommandName = 2;
uint64 UnixTime = 3;
uint64 Date = 3;
bool IsSuccess = 4;
uint64 UserId = 5;
int64 ExecutionTimeMs = 6;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ public override async Task<AverageTimeResponse> GetAverageExecutionTime(Protos.V

public override async Task<CommandLogModel> AddCommand(CommandLogModel request, ServerCallContext context)
{
var test = ReflectionMapper.Map<CommandLogModel, CommandLog>(request);
//ReflectionMapper
var q = ReflectionMapper.Map<CommandLog>(request);

Core.Models.CommandLog commandLog = new()
{
ChannelId = request.ChannelId,
UserId = request.UserId,
CommandName = request.CommandName,
Date = Miscellaneous.UnixTimeStampToUtcDateTime(request.UnixTime),
Date = Miscellaneous.UnixTimeStampToUtcDateTime(request.Date),
ErrorMessage = request.ErrorMessage,
ExecutionTimeMs = request.ExecutionTimeMs,
GuildId = request.GuildId,
Expand All @@ -90,7 +90,7 @@ public override async Task<CommonResponse> RemoveCommand(CommandLogModel request
ChannelId = request.ChannelId,
UserId = request.UserId,
CommandName = request.CommandName,
Date = Miscellaneous.UnixTimeStampToUtcDateTime(request.UnixTime),
Date = Miscellaneous.UnixTimeStampToUtcDateTime(request.Date),
ErrorMessage = request.ErrorMessage,
ExecutionTimeMs = request.ExecutionTimeMs,
GuildId = request.GuildId,
Expand Down

0 comments on commit be4904d

Please sign in to comment.