-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from NewLifeX/pgsql_array
Pgsql array
- Loading branch information
Showing
9 changed files
with
506 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System.Collections; | ||
|
||
namespace XCode; | ||
|
||
/// <remarks> | ||
/// Array 部分的实现,先尽量保持简洁。 | ||
/// 后续更强大的转换,再补充。 | ||
/// </remarks> | ||
public partial class ValidHelper | ||
{ | ||
private static T[]? ToArray<T>(Object? value, Func<object?, T> converter) | ||
{ | ||
if (value is T[] arr) return arr; | ||
if (value is null || Convert.IsDBNull(value)) return default; | ||
if (value is IEnumerable<T> list) return list.ToArray(); | ||
if (value is T v) return new T[] { v }; | ||
if (value is IEnumerable) | ||
{ | ||
var ret = new List<T>(); | ||
foreach (var item in (IEnumerable)value) | ||
{ | ||
ret.Add(converter.Invoke(item)); | ||
} | ||
return ret.ToArray(); | ||
} | ||
return default; | ||
} | ||
public static Int32[]? ToInt32Array(Object? value) => ToArray(value, ToInt32); | ||
public static Int64[]? ToInt64Array(Object? value) => ToArray(value, ToInt64); | ||
public static Double[]? ToDoubleArray(Object? value) => ToArray(value, ToDouble); | ||
public static Boolean[]? ToBooleanArray(Object? value) => ToArray(value, ToBoolean); | ||
public static DateTime[]? ToDateTimeArray(Object? value) => ToArray(value, ToDateTime); | ||
public static String[]? ToStringArray(Object? value) => ToArray(value, ToString); | ||
public static Byte[]? ToByteArray(Object? value) => ToArray(value, ToByte); | ||
public static Decimal[]? ToDecimalArray(Object? value) => ToArray(value, ToDecimal); | ||
public static Int16[]? ToInt16Array(Object? value) => ToArray(value, ToInt16); | ||
public static UInt64[]? ToUInt64Array(Object? value) => ToArray(value, ToUInt64); | ||
public static T[]? ToEnumArray<T>(Object? value) where T : struct => ToArray(value, ToEnum<T>); | ||
public static T[]? ToObjectArray<T>(Object? value) where T : class => ToArray(value, ToObject<T>); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace XCode; | ||
|
||
/// <summary>用户数据类型转换</summary> | ||
public static partial class ValidHelper | ||
{ | ||
/// <summary>转为整数,转换失败时返回默认值。支持字符串、全角、字节数组(小端)、时间(Unix秒不转UTC)</summary> | ||
/// <remarks>Int16/UInt32/Int64等,可以先转为最常用的Int32后再二次处理</remarks> | ||
/// <param name="value">待转换对象</param> | ||
public static Int32 ToInt32(object? value) | ||
{ | ||
return value.ToInt(); | ||
} | ||
|
||
/// <summary>转为长整数,转换失败时返回默认值。支持字符串、全角、字节数组(小端)、时间(Unix毫秒不转UTC)</summary> | ||
/// <param name="value">待转换对象</param> | ||
public static Int64 ToInt64(object? value) | ||
{ | ||
return value.ToLong(); | ||
} | ||
|
||
/// <summary>转为浮点数,转换失败时返回默认值。支持字符串、全角、字节数组(小端)</summary> | ||
/// <remarks>Single可以先转为最常用的Double后再二次处理</remarks> | ||
/// <param name="value">待转换对象</param> | ||
public static Double ToDouble(object? value) | ||
{ | ||
return value.ToDouble(); | ||
} | ||
|
||
/// <summary>转为布尔型,转换失败时返回默认值。支持大小写True/False、0和非零</summary> | ||
/// <param name="value">待转换对象</param> | ||
public static Boolean ToBoolean(object? value) | ||
{ | ||
return value.ToBoolean(); | ||
} | ||
|
||
/// <summary>转为时间日期,转换失败时返回最小时间。支持字符串、整数(Unix秒不考虑UTC转本地)</summary> | ||
/// <remarks> | ||
/// 整数转时间日期时,取1970-01-01加上指定秒数,不考虑UTC时间和本地时间。 | ||
/// 长整数转时间日期时,取1970-01-01加上指定毫秒数,不考虑UTC时间和本地时间。 | ||
/// 在网络中传输时间日期时,特别是物联网设备到云平台的通信,一般取客户端本地UTC时间,转为长整型传输,服务端再转为本地时间。 | ||
/// 因为设备和服务端可能不在同一时区,甚至多个设备也没有处于同一个时区。 | ||
/// </remarks> | ||
/// <param name="value">待转换对象</param> | ||
public static DateTime ToDateTime(object? value) | ||
{ | ||
return value.ToDateTime(); | ||
} | ||
|
||
/// <summary> | ||
/// 将指定的值转换为其等效的字符串表示形式。 | ||
/// </summary> | ||
public static String ToString(object? value) | ||
{ | ||
return Convert.ToString(value); | ||
} | ||
|
||
/// <summary> | ||
/// Convert.ToByte | ||
/// </summary> | ||
public static Byte ToByte(object? value) | ||
{ | ||
return Convert.ToByte(value); | ||
} | ||
|
||
/// <summary> | ||
/// Convert.ToDecimal | ||
/// </summary> | ||
public static Decimal ToDecimal(object? value) | ||
{ | ||
return Convert.ToDecimal(value); | ||
} | ||
|
||
/// <summary> | ||
/// Convert.ToInt16 | ||
/// </summary> | ||
public static Int16 ToInt16(object? value) | ||
{ | ||
return Convert.ToInt16(value); | ||
} | ||
|
||
/// <summary> | ||
/// Convert.ToUInt64 | ||
/// </summary> | ||
public static UInt64 ToUInt64(object? value) | ||
{ | ||
return Convert.ToUInt64(value); | ||
} | ||
|
||
/// <summary> | ||
/// 转换为枚举 | ||
/// </summary> | ||
public static T ToEnum<T>(object? value) where T : struct | ||
{ | ||
if (value is T t) return t; | ||
if (value is null || Convert.IsDBNull(value)) return default; | ||
if (typeof(T).IsEnum) | ||
{ | ||
if (value is string str) return (T)Enum.Parse(typeof(T), str, true); | ||
} | ||
return (T)value; | ||
} | ||
|
||
public static T? ToObject<T>(object? value) where T : class | ||
{ | ||
if (value is T t) return t; | ||
if (value is null || Convert.IsDBNull(value)) return default; | ||
//这里怎么实现呢? | ||
return default; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.