Data Model Code
0Hancock posted on 2022/02/22 16:18:01
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace AS.Core.Common
{
static class DataGenerator
{
const string CHARACTER_POOL = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
const string NUMBER_POOL = "0123456789";
static DateTime ParseDate(string date)
{
return DateTime.ParseExact(date, "yyyy/MM/dd", CultureInfo.InvariantCulture);
}
static string RandomString(Random rnd, int maxWords = 7)
{
StringBuilder builder = new StringBuilder();
int len = rnd.Next(1, maxWords);
for(int w = 0; w < len; w++)
{
int wordSize = rnd.Next(3, 7);
for (int i = 0; i < wordSize; i++) builder.Append(CHARACTER_POOL[rnd.Next(0, CHARACTER_POOL.Length)]);
builder.Append(" ");
}
return builder.ToString().Trim();
}
static object RandomDate(Random rnd, string config)
{
if (string.IsNullOrEmpty(config))
{
DateTime fromDate = DateTime.Now;
return fromDate.AddDays((rnd.Next() % 2 == 0 ? 1 : -1) * rnd.Next(0, 256 * 256));
}
else if (config.Contains(";"))
{
return ParseDate(RandomValues(rnd, config));
}
else if (config.Contains("-"))
{
string[] ranges = config.Split("-");
DateTime fromDate = ParseDate(ranges[0]);
DateTime toDate = ParseDate(ranges[1]);
int countDay = (int)Math.Floor(toDate.Subtract(fromDate).TotalDays);
return fromDate.AddDays(rnd.Next(countDay));
}
else return ParseDate(config);
}
static object RandomInt(Random rnd, string config)
{
if (string.IsNullOrEmpty(config))
{
return rnd.Next();
}
else if (config.Contains(";"))
{
return int.Parse(RandomValues(rnd, config));
}
else if (config.Contains("-"))
{
string[] ranges = config.Split("-");
return rnd.Next(int.Parse(ranges[0]), int.Parse(ranges[1]) + 1);
}
else return int.Parse(config);
}
static object RandomDecimal(Random rnd, string config)
{
if (string.IsNullOrEmpty(config))
{
return rnd.NextDouble() * rnd.Next();
}
else if (config.Contains(";"))
{
return double.Parse(RandomValues(rnd, config));
}
else if (config.Contains("-"))
{
string[] ranges = config.Split("-");
return double.Parse(ranges[0]) + rnd.NextDouble() * (double.Parse(ranges[1]) - double.Parse(ranges[0]));
}
else return double.Parse(config);
}
static object RandomString(Random rnd, string config)
{
if (string.IsNullOrEmpty(config)) return RandomString(rnd);
else return RandomValues(rnd, config);
}
static object RandomMerchant(Random rnd, string config)
{
if (string.IsNullOrEmpty(config))
{
string rndStr = "";
for (int i = 0; i < 8; i++)
{
rndStr += NUMBER_POOL[rnd.Next(NUMBER_POOL.Length)];
}
return rndStr;
}
else if (config.Contains(";"))
{
return RandomValues(rnd, config);
}
else return config;
}
static object RandomCard(Random rnd,string config)
{
if (string.IsNullOrEmpty(config))
{
string rndStr = "";
for (int i = 0; i < 16; i++)
{
rndStr += NUMBER_POOL[rnd.Next(NUMBER_POOL.Length)];
}
return rndStr;
}
else if (config.Contains(";"))
{
return RandomValues(rnd, config);
}
else return config;
}
static string RandomValues(Random rnd, string pool)
{
const string SPLIT_SYMBOL = "[0x1]";
pool = pool.Replace(";;", SPLIT_SYMBOL);
string[] items = pool.Split(';');
return items[rnd.Next(items.Length)].Replace(SPLIT_SYMBOL, ";");
}
static string RandomEmail(Random rnd, string config)
{
if (string.IsNullOrEmpty(config))
{
string rndStr = "";
int wordSize = rnd.Next(3, 7);
for (int i = 0; i < wordSize; i++) rndStr += CHARACTER_POOL[rnd.Next(0, CHARACTER_POOL.Length)];
rndStr += "@fake-mail.com";
return rndStr;
}
else if (config.Contains(";"))
{
return RandomValues(rnd, config);
}
else return config;
}
public static object GenerateValue(Type type, Random rnd, string mockData, string dataType)
{
Type nullableType = Nullable.GetUnderlyingType(type);
if (nullableType != null) type = nullableType;
object val = null;
if (typeof(Enum).IsAssignableFrom(type))
{
var vals = Enum.GetValues(type);
val = vals.GetValue((int)DataGenerator.RandomInt(rnd, null) % vals.Length);
}
else switch (Type.GetTypeCode(type))
{
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
{
val = Convert.ChangeType(DataGenerator.RandomInt(rnd, mockData), type);
}
break;
case TypeCode.Decimal:
case TypeCode.Double:
case TypeCode.Single:
{
val = Convert.ChangeType(DataGenerator.RandomDecimal(rnd, mockData), type);
}
break;
case TypeCode.DateTime:
{
val = DataGenerator.RandomDate(rnd, mockData);
}
break;
case TypeCode.Boolean:
{
val = string.IsNullOrEmpty(mockData) ? rnd.Next() % 2 == 0 : mockData.ToLower() == "true";
}
break;
case TypeCode.Byte:
{
val = (byte)rnd.Next(0, 255);
}
break;
default:
{
if(type == typeof(System.Guid))
{
if (string.IsNullOrEmpty(mockData)) val = Guid.NewGuid();
else val = new Guid(DataGenerator.RandomString(rnd, mockData) as string);
}
else
switch (dataType)
{
case "cardnumber":
val = DataGenerator.RandomCard(rnd, mockData);
break;
case "merchantnumber":
val = DataGenerator.RandomMerchant(rnd, mockData);
break;
case "email":
val = DataGenerator.RandomEmail(rnd, mockData);
break;
default:
val = DataGenerator.RandomString(rnd, mockData);
break;
}
}
break;
}
return Convert.ChangeType(val, type);
}
}
}