초기 커밋.
This commit is contained in:
92
Assets/Scripts/UVC/Data/DataMapper.cs
Normal file
92
Assets/Scripts/UVC/Data/DataMapper.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
|
||||
namespace UVC.Data
|
||||
{
|
||||
/// <summary>
|
||||
/// 서로 다른 형식이나 구조 간에 데이터를 매핑하는 기능을 제공합니다.
|
||||
/// </summary>
|
||||
/// <remarks>이 클래스는 일반적으로 데이터베이스 레코드를 도메인 객체에 매핑하거나 서로 다른 데이터 모델 간에 변환하는 등 데이터를 한 표현에서 다른 표현으로 변환하거나 매핑하는 데 사용됩니다.
|
||||
///</remarks>
|
||||
public class DataMapper
|
||||
{
|
||||
private JObject source;
|
||||
private JObject guide;
|
||||
|
||||
public DataMapper(JObject source, JObject target)
|
||||
{
|
||||
this.source = source;
|
||||
this.guide = target;
|
||||
}
|
||||
|
||||
public JObject Map()
|
||||
{
|
||||
JObject target = new JObject();
|
||||
foreach (var property in source.Properties())
|
||||
{
|
||||
if (guide.ContainsKey(property.Name))
|
||||
{
|
||||
JToken guideValue = guide[property.Name];
|
||||
string guideType = guideValue?.Type.ToString() ?? "null";
|
||||
if (guideType == "String" && property.Value.Type == JTokenType.String)
|
||||
{
|
||||
target[property.Name] = property.Value.ToObject<string>();
|
||||
}
|
||||
else if (guideType == "Integer" && property.Value.Type == JTokenType.Integer)
|
||||
{
|
||||
target[property.Name] = property.Value.ToObject<int>();
|
||||
}
|
||||
else if (guideType == "Float" && property.Value.Type == JTokenType.Float)
|
||||
{
|
||||
target[property.Name] = property.Value.ToObject<double>();
|
||||
}
|
||||
else if (guideType == "Boolean" && property.Value.Type == JTokenType.Boolean)
|
||||
{
|
||||
target[property.Name] = property.Value.ToObject<bool>();
|
||||
}
|
||||
else if (guideType == "Object" && guideValue.ToObject<object>() is DataValueMapper && property.Value.Type == JTokenType.String)
|
||||
{
|
||||
string strValue = property.Value.ToObject<string>();
|
||||
var dataValueMapper = guideValue.ToObject<DataValueMapper>();
|
||||
if (dataValueMapper != null && dataValueMapper.ContainsKey(strValue))
|
||||
{
|
||||
target[property.Name] = dataValueMapper[strValue];
|
||||
}
|
||||
else
|
||||
{
|
||||
target[property.Name] = strValue;
|
||||
}
|
||||
}
|
||||
else if (guideType == "Date" && property.Value.Type == JTokenType.String)
|
||||
{
|
||||
string dateStr = property.Value.ToObject<string>();
|
||||
if (DateTime.TryParse(dateStr, out DateTime dateValue))
|
||||
{
|
||||
target[property.Name] = JToken.FromObject(dateValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
target[property.Name] = null;
|
||||
}
|
||||
}
|
||||
else if (guideValue.ToObject<object>()?.GetType()?.IsEnum == true && property.Value.Type == JTokenType.String)
|
||||
{
|
||||
Type enumType = guideValue.ToObject<object>().GetType();
|
||||
target[property.Name] = JToken.FromObject(Enum.Parse(enumType, property.Value.ToObject<string>(), true));
|
||||
}
|
||||
else
|
||||
{
|
||||
target[property.Name] = property.Value;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
target[property.Name] = property.Value;
|
||||
}
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user