2 Commits

  1. 16
      Apewer/Source/ColumnAttribute.cs
  2. 21
      Apewer/Source/TableAttribute.cs
  3. 99
      Apewer/TextUtility.cs
  4. 3
      Apewer/_Extensions.cs

16
Apewer/Source/ColumnAttribute.cs

@ -176,7 +176,18 @@ namespace Apewer.Source
if (nu) ca._noupdate = true;
// 检查列名称。
if (TextUtility.IsBlank(ca.Field)) ca._field = property.Name;
if (TextUtility.IsBlank(ca.Field))
{
if (CustomField == null)
{
ca._field = property.Name;
}
else
{
ca._field = CustomField.Invoke(property);
if (string.IsNullOrEmpty(ca._field)) throw new ArgumentException($"CustomField 未返回属性 {property.DeclaringType.Name}.{property.Name} 的字段。");
}
}
// 类型兼容。
if (pt.Equals(typeof(byte[]))) ca._type = ColumnType.Bytes;
@ -265,6 +276,9 @@ namespace Apewer.Source
internal void SetPrimaryKey() => _primarykey = true;
/// <summary>根据属性自定义字段。</summary>
public static Func<PropertyInfo, string> CustomField { get; set; }
}
}

21
Apewer/Source/TableAttribute.cs

@ -65,6 +65,13 @@ namespace Apewer.Source
#endregion
#region 自定义
/// <summary>根据类型自定义表名。</summary>
public static Func<Type, string> CustomTableName { get; set; }
#endregion
#region cache
private static Dictionary<string, TableAttribute> _tac = new Dictionary<string, TableAttribute>();
@ -106,7 +113,19 @@ namespace Apewer.Source
}
ta._model = type;
if (string.IsNullOrEmpty(ta.Name)) ta._name = type.Name;
if (string.IsNullOrEmpty(ta.Name))
{
if (CustomTableName == null)
{
ta._name = type.Name;
}
else
{
ta._name = CustomTableName.Invoke(type);
if (string.IsNullOrEmpty(ta._name)) throw new ArgumentException($"CustomTableName 未返回类型 {type.Name} 的表名。");
}
}
ta.Independent = RuntimeUtility.Contains<IndependentAttribute>(type, true);
ta._primarykey = RuntimeUtility.IsInherits(type, InterfacePrimaryKey);

99
Apewer/TextUtility.cs

@ -2,6 +2,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
@ -982,8 +983,9 @@ namespace Apewer
return Regex.IsMatch(trim, "(^[0-9a-zA-Z_ -;:,~!@#%&=<>~\\(\\)\\.\\$\\^\\`\\'\\\"\\&\\{\\}\\[\\]\\|\\*\\+\\?]{0,80}$)");
}
/// <summary>转换文本为驼峰形式。</summary>
public static string Camel(string text)
/// <summary>转换文本为驼峰形式,首字小写,其它词首字母大写。</summary>
/// <remarks>示例:toCamelCase</remarks>
public static string Camel(this string text)
{
if (string.IsNullOrEmpty(text) || !char.IsUpper(text[0])) return text;
@ -1008,6 +1010,99 @@ namespace Apewer
return new string(chars);
}
/// <summary>转换文本为帕斯卡形式,所有词首字母大写。</summary>
/// <remarks>示例:ToPascalCase</remarks>
public static string Pascal(this string text)
{
if (string.IsNullOrEmpty(text)) return text;
var sb = new StringBuilder();
var snake = Snake(text);
var split = snake.Split('_');
foreach (var word in split)
{
if (word.Length < 1) continue;
var chars = word.ToCharArray();
chars[0] = char.ToUpper(chars[0]);
sb.Append(new string(chars));
}
return sb.ToString();
}
/// <summary>转换文本为串式形式,使用横线连接每个词。</summary>
/// <remarks>示例:to-kebab-case</remarks>
public static string Kebab(this string text) => WordCase(text, '-');
/// <summary>转换文本为蛇形形式,使用下划线连接每个词。</summary>
/// <remarks>示例:to_sname_case</remarks>
public static string Snake(this string text) => WordCase(text, '_');
/// <summary>转换单词风格。</summary>
static string WordCase(string text, char separator)
{
if (string.IsNullOrEmpty(text)) return text;
const int StateStart = 1;
const int StateLower = 2;
const int StateUpper = 3;
const int StateNewWord = 4;
var sb = new StringBuilder();
var state = StateStart;
for (int i = 0; i < text.Length; i++)
{
if (text[i] == ' ')
{
if (state != StateStart)
{
state = StateNewWord;
}
}
else if (char.IsUpper(text[i]))
{
switch (state)
{
case StateUpper:
bool hasNext = (i + 1 < text.Length);
if (i > 0 && hasNext)
{
char nextChar = text[i + 1];
if (!char.IsUpper(nextChar) && nextChar != separator)
{
sb.Append(separator);
}
}
break;
case StateLower:
case StateNewWord:
sb.Append(separator);
break;
}
char c;
c = char.ToLower(text[i], CultureInfo.InvariantCulture);
sb.Append(c);
state = StateUpper;
}
else if (text[i] == separator)
{
sb.Append(separator);
state = StateStart;
}
else
{
if (state == StateNewWord) sb.Append(separator);
sb.Append(text[i]);
state = StateLower;
}
}
return sb.ToString();
}
/// <summary>移除参数 chars 中的每一个字符。</summary>
public static string RemoveChars(string text, string chars)
{

3
Apewer/_Extensions.cs

@ -117,9 +117,6 @@ public static class Extensions
#region String、StringBuilder
/// <summary>转换文本为驼峰形式。</summary>
public static string Camel(this string @this) => TextUtility.Camel(@this);
/// <summary>转换为 Boolean 值。</summary>
public static bool Boolean(this string @this) => NumberUtility.Boolean(@this);

Loading…
Cancel
Save